src/Security/Voter/Messenger/CollectionVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Messenger;
  3. use App\Entity\Collection;
  4. use App\Security\ApiUser;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class CollectionVoter extends Voter
  8. {
  9.     public const READ 'read';
  10.     public const WRITE 'write';
  11.     public const DELETE 'delete';
  12.     protected function supports(string $attribute$subject): bool
  13.     {
  14.         if (!$subject instanceof Collection) {
  15.             return false;
  16.         }
  17.         return in_array($attribute, [
  18.             self::READ,
  19.             self::WRITE,
  20.             self::DELETE,
  21.         ], true);
  22.     }
  23.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  24.     {
  25.         $user $token->getUser();
  26.         if (!$user instanceof ApiUser) {
  27.             return false;
  28.         }
  29.         /*
  30.          * @var $subject Collection
  31.          */
  32.         return $user->getIsAmateur() || $user->getIsAdmin();
  33.     }
  34. }