src/Security/Voter/UserVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Security;
  4. use Symfony\Component\Security\Core\User\UserInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. class UserVoter extends Voter
  9. {
  10.     public const LIST = 'LIST_USERS';
  11.     public const EDIT 'EDIT_USERS';
  12.     public function __construct(public Security $security, private ParameterBagInterface $Serviceparams)
  13.     {
  14.     }
  15.     protected function supports(string $attribute$subject): bool
  16.     {
  17.         return in_array($attribute, [self::EDITself::LIST]);
  18.     }
  19.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  20.     {
  21.         $user $token->getUser();
  22.         // if the user is anonymous, do not grant access
  23.         if (!$user instanceof UserInterface) {
  24.             return false;
  25.         }
  26.         // ... (check conditions and return true to grant permission) ...
  27.         $accessGranted = match($attribute){
  28.             self::LIST => $this->security->isGranted($this->Serviceparams->get('role_admin')),
  29.             self::EDIT => $this->security->isGranted($this->Serviceparams->get('role_admin'))
  30.         };
  31.         return $accessGranted;
  32.     }
  33. }