src/Security/VacationVoter.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 VacationVoter 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.         $userPosteDirector $this->entityManager->getRepository(Employee::class)->findOneBy(['partner' => $userConnected->getPartner()])->getPosteDirector();
  35.         $subjectPosteDirector $subject->getPoste()->getPosteDirector();
  36.         // if the user is anonymous, do not grant access
  37.         if (!$userConnected instanceof AppUser) {
  38.             return false;
  39.         }
  40.         switch ($attribute) {
  41.             case self::REJECTED:
  42.             case self::ACCEPTED:
  43.                 if($userPosteDirector == $subjectPosteDirector){
  44.                    return true;
  45.                 }
  46.                 break;
  47.         }
  48.         return false;
  49.     }
  50. }