<?php
/*
* @since 1.0.0
* @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
* @website http://artmedia.biz.pl
* @author Arkadiusz Tobiasz
* @email kontakt@artmedia.biz.pl
*/
namespace App\EventSubscriber\Core;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Events\Core\PrivacyCheckEvent;
use Symfony\Component\Security\Core\Security;
use App\Entity\User;
class PrivacySubscriber implements EventSubscriberInterface
{
protected $security;
public function __construct(
Security $security
) {
$this->security = $security;
}
public static function getSubscribedEvents(): array
{
return [
PrivacyCheckEvent::NAME => 'onPrivacyCheckEvent',
];
}
public function onPrivacyCheckEvent(PrivacyCheckEvent $event): void
{
$user = $this->security->getUser();
if ($user instanceof User) {
$event->addPrivacy(PrivacyCheckEvent::USERS);
if($user->getGroups()) {
$event->addPrivacy(PrivacyCheckEvent::GROUPS);
foreach ($user->getGroups() as $group) {
$event->addGroup($group->getId());
}
}
if (in_array(User::ROLE_ADMIN, $user->getRoles())) {
$event->addPrivacy(PrivacyCheckEvent::ADMINS);
}
} else {
$event->addPrivacy(PrivacyCheckEvent::GUESTS);
}
}
}