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 Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class NotificationVoter extends Voter
  8. {
  9.     public const DELETE 'delete';
  10.     public const EDIT   'edit';
  11.     protected function supports(string $attribute$subject): bool
  12.     {
  13.         return in_array($attribute, [self::DELETEself::EDIT])
  14.             && $subject instanceof Notification;
  15.     }
  16.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  17.     {
  18.         $user $token->getUser();
  19.         if (!$user instanceof UserInterface) {
  20.             return false;
  21.         }
  22.         return match ($attribute) {
  23.             self::DELETEself::EDIT => $subject->getProfile() === $user->getProfile(),
  24.             default => false,
  25.         };
  26.     }
  27. }