src/Security/Voter/NotificationVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Notification;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class NotificationVoter extends Voter
  8. {
  9.     public const VIEW 'NOTIFICATION_VIEW';
  10.     public const EDIT 'NOTIFICATION_EDIT';
  11.     protected function supports(string $attributemixed $subject): bool
  12.     {
  13.         if (!$subject instanceof Notification) {
  14.             return false;
  15.         }
  16.         return in_array($attribute, [self::VIEWself::EDIT], true);
  17.     }
  18.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  19.     {
  20.         $user $token->getUser();
  21.         if (!$user instanceof User) {
  22.             return false;
  23.         }
  24.         /** @var Notification $notification */
  25.         $notification $subject;
  26.         $owner $notification->getUser();
  27.         return null !== $owner && $owner->getId() === $user->getId();
  28.     }
  29. }