src/Security/Voter/PersonVoter.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 PersonVoter extends Voter
  8. {
  9.     protected function supports($attribute$subject)
  10.     {
  11.         // replace with your own logic
  12.         // https://symfony.com/doc/current/security/voters.html
  13.         return in_array($attribute, ['FROM_WLPGA'])
  14.             && $subject instanceof User;
  15.     }
  16.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  17.     {
  18.         $user $token->getUser();
  19.         // if the user is anonymous, do not grant access
  20.         if (!$user instanceof UserInterface) {
  21.             return false;
  22.         }
  23.         // ... (check conditions and return true to grant permission) ...
  24.         switch ($attribute) {
  25.             case 'FROM_WLPGA':
  26.                 return $this->checkFromWlpga($subject);
  27.         }
  28.         return false;
  29.     }
  30.     /**
  31.      * @param User $user
  32.      * @return bool
  33.      */
  34.     private function checkFromWlpga(User $user) : bool
  35.     {
  36.         if(in_array(User::ROLE_PREVIOUS_ADMIN$user->getRoles(), TRUE)) {
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41. }