src/EventSubscriber/Core/PrivacySubscriber.php line 34

Open in your IDE?
  1. <?php
  2. /*
  3.  * @since 1.0.0
  4.  * @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
  5.  * @website http://artmedia.biz.pl
  6.  * @author Arkadiusz Tobiasz
  7.  * @email kontakt@artmedia.biz.pl
  8.  */
  9. namespace App\EventSubscriber\Core;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use App\Events\Core\PrivacyCheckEvent;
  12. use Symfony\Component\Security\Core\Security;
  13. use App\Entity\User;
  14. class PrivacySubscriber implements EventSubscriberInterface
  15. {
  16.     protected $security;
  17.     public function __construct(
  18.         Security $security
  19.     ) {
  20.         $this->security $security;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             PrivacyCheckEvent::NAME => 'onPrivacyCheckEvent',
  26.         ];
  27.     }
  28.     public function onPrivacyCheckEvent(PrivacyCheckEvent $event): void
  29.     {
  30.         $user $this->security->getUser();
  31.         if ($user instanceof User) {
  32.             $event->addPrivacy(PrivacyCheckEvent::USERS);
  33.             if($user->getGroups()) {
  34.                 $event->addPrivacy(PrivacyCheckEvent::GROUPS);
  35.                 foreach ($user->getGroups() as $group) {
  36.                     $event->addGroup($group->getId());
  37.                 }
  38.             }
  39.             if (in_array(User::ROLE_ADMIN$user->getRoles())) {
  40.                 $event->addPrivacy(PrivacyCheckEvent::ADMINS);
  41.             }
  42.         } else {
  43.             $event->addPrivacy(PrivacyCheckEvent::GUESTS);
  44.         }
  45.     }
  46. }