src/Security/Voter/SwitchToAnotherClientVoter.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\Security;
  7. class SwitchToAnotherClientVoter extends Voter
  8. {
  9.     private $security;
  10.     public function __construct(Security $security)
  11.     {
  12.         $this->security $security;
  13.     }
  14.     protected function supports($attribute$subject): bool
  15.     {
  16.         return $attribute === 'CAN_SWITCH_CLIENT'
  17.             && $subject instanceof User;
  18.     }
  19.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  20.     {
  21.         $user $token->getUser();
  22.         // if the user is anonymous or if the subject is not a user, do not grant access
  23.         if (!$user instanceof User || !$subject instanceof User) {
  24.             return false;
  25.         }
  26.         if ($this->security->isGranted('ROLE_ADMIN')) {
  27.             return true;
  28.         }
  29.         if ( $this->security->isGranted('ROLE_PERSONAL_MANAGER') && $user->getDependents()->contains($subject) ) {
  30.             return true;
  31.         }
  32.         if ($this->isNotValidRole($user) || $this->isNotValidRole($subject)) {
  33.             return false;
  34.         }
  35.         if ( $user->getAllowedLoginAs()->contains($subject) ){
  36.             return true;
  37.         }
  38.         return false;
  39.     }
  40.     private function isNotValidRole(User $user){
  41.         if ( count(array_intersect(User::NOT_VALID_ROLES$user->getRoles())) > )
  42.         {
  43.             return true;
  44.         }
  45.         return false;
  46.     }
  47. }