src/Voter/ContactVoter.php line 8

Open in your IDE?
  1. <?php
  2. namespace App\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. class ContactVoter extends Voter
  7. {
  8.     const LIST = "CONTACT_LIST";
  9.     const CREATE "CONTACT_CREATE";
  10.     const UPDATE "CONTACT_UPDATE";
  11.     const DETAIL "CONTACT_DETAIL";
  12.     protected function supports($attribute$subject): bool
  13.     {
  14.         return in_array($attribute, [
  15.             self::LIST,
  16.             self::CREATE,
  17.             self::UPDATE,
  18.             self::DETAIL,
  19.         ]);
  20.     }
  21.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  22.     {
  23.         $loggedUser $token->getUser();
  24.         if (!$loggedUser instanceof UserInterface) {
  25.             return false;
  26.         }
  27.         return in_array($attribute$loggedUser->getRoles());
  28.     }
  29. }