src/Security/Voter/BackUserVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\BackUser;
  4. use App\Utils\VoterHelper;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class BackUserVoter extends Voter
  8. {
  9.     private const EDIT           'edit';
  10.     private const CREATE         'create';
  11.     private const DELETE         'delete';
  12.     private const RESET_PASSWORD 'reset-password';
  13.     private const SWITCH_STATUS  'switch-status';
  14.     private const IMPERSONATE    'impersonate';
  15.     protected function supports(string $attributemixed $subject): bool
  16.     {
  17.         return $subject instanceof BackUser && in_array($attribute, [self::EDITself::CREATEself::DELETEself::RESET_PASSWORDself::SWITCH_STATUSself::IMPERSONATE], true);
  18.     }
  19.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  20.     {
  21.         switch ($attribute) {
  22.             case self::IMPERSONATE:
  23.                 if (!$subject->getEnabled()) {
  24.                     return false;
  25.                 }
  26.                 break;
  27.             case self::SWITCH_STATUS:
  28.                 if ($subject->getId() == $token->getUser()->getId()) {
  29.                     return false;
  30.                 }
  31.                 break;
  32.         }
  33.         return VoterHelper::voteOnAttribute($attribute$subject$token'back_user');
  34.     }
  35. }