src/Security/Voter/UserAccessVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\UserAccess;
  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 UserAccessVoter extends Voter
  8. {
  9.     public const EDIT 'POST_EDIT';
  10.     public const VIEW 'POST_VIEW';
  11.     protected function supports(string $attribute$subject): bool
  12.     {
  13.         // replace with your own logic
  14.         // https://symfony.com/doc/current/security/voters.html
  15.         return in_array($attribute, [self::EDITself::VIEW])
  16.             && $subject instanceof UserAccess;
  17.     }
  18.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  19.     {
  20.         $user $token->getUser();
  21.         // if the user is anonymous, do not grant access
  22.         if (!$user instanceof UserInterface) {
  23.             return false;
  24.         }
  25.         // ... (check conditions and return true to grant permission) ...
  26.         switch ($attribute) {
  27.             case self::EDIT:
  28.                 // logic to determine if the user can EDIT
  29.                 // return true or false
  30.                 break;
  31.             case self::VIEW:
  32.                 // logic to determine if the user can VIEW
  33.                 // return true or false
  34.                 break;
  35.         }
  36.         return false;
  37.     }
  38. }