src/Security/VacationCentraleVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\AppUser;
  4. use App\Entity\Employee;
  5. use App\Entity\RoleAction;
  6. use App\Entity\UserRole;
  7. use App\Utils\Consts;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. class VacationCentraleVoter extends Voter
  14. {
  15.     public const ACCEPTED 'ACCEPTED';
  16.     public const REJECTED 'REJECTED';
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         // replace with your own logic
  20.         // https://symfony.com/doc/current/security/voters.html
  21.         return in_array($attribute, [self::ACCEPTEDself::REJECTED])
  22.             && $subject instanceof \App\Entity\Vacation;
  23.     }
  24.     private $entityManager;
  25.     private $security;
  26.     public function __construct(EntityManagerInterface $entityManager,Security $security)
  27.     {
  28.         $this->entityManager $entityManager;
  29.         $this->security $security;
  30.     }
  31.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  32.     {
  33.         $userConnected $token->getUser();
  34.         // if the user is anonymous, do not grant access
  35.         if (!$userConnected instanceof AppUser) {
  36.             return false;
  37.         }
  38.         $canSetActions false;
  39.         $isCentral false;
  40.         if ($this->entityManager->getRepository(RoleAction::class)->hasRoleAction($userConnected,'canSetActions')) {
  41.             $canSetActions true;
  42.         } if ($this->entityManager->getRepository(RoleAction::class)->hasRoleAction($userConnected,'isCentrale')) {
  43.             $isCentral true;
  44.         }
  45.         switch ($attribute) {
  46.             case self::REJECTED:
  47.             case self::ACCEPTED:
  48.                 if($isCentral && $canSetActions){
  49.                    return true;
  50.                 }
  51.                 break;
  52.         }
  53.         return false;
  54.     }
  55. }