src/Security/Voter/UserVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  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 UserVoter extends Voter
  8. {
  9.     protected function supports(string $attributemixed $subject): bool
  10.     {
  11.         return in_array($attribute, ['USER_EDIT''USER_VIEW''USER_POST''USER_DELETE'])
  12.             && $subject instanceof \App\Entity\User;
  13.     }
  14.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  15.     {
  16.         /** @var User $user */
  17.         $user $token->getUser();
  18.         // if the user is anonymous, do not grant access
  19.         if (!$user instanceof UserInterface) {
  20.             return false;
  21.         }
  22.         //return true;
  23.         // ... (check conditions and return true to grant permission) ...
  24.         switch ($attribute) {
  25.             case 'USER_EDIT':
  26.             case 'USER_POST':
  27.             case 'USER_DELETE':
  28.                 if ($user->getProfile() === 0) {
  29.                     return true;
  30.                 }
  31.                 break;
  32.             case 'USER_VIEW':
  33.                 if ($user->getProfile() <= 1) {
  34.                     return true;
  35.                 }
  36.                 break;
  37.         }
  38.         return false;
  39.     }
  40. }