src/Security/Voter/ProjectChangeVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\{Project\ProjectUser};
  5. use Symfony\Component\Security\Core\{Authentication\Token\TokenInterfaceAuthorization\Voter\VoterSecurity};
  6. /**
  7.  * Does user can change the project?
  8.  */
  9. class ProjectChangeVoter extends Voter
  10. {
  11.     private Security $security;
  12.     public function __construct(Security $security)
  13.     {
  14.         $this->security $security;
  15.     }
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         return $attribute === 'PROJECT_EDIT' && $subject instanceof Project;
  19.     }
  20.     /**
  21.      * @param string        $attribute
  22.      * @param Project|mixed $subject
  23.      */
  24.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $user $token->getUser();
  27.         $manager $user->getManager();
  28.         if ($this->security->isGranted('ROLE_MASTER_MANAGER')) {
  29.             return true;
  30.         }
  31.         if ($this->security->isGranted('ROLE_MANAGER') && $subject->getManagers()->contains($manager)) {
  32.             return true;
  33.         }
  34.         return false;
  35.     }
  36. }