src/Security/PosteRouteVoter.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\AppActions;
  4. use App\Entity\AppUser;
  5. use App\Entity\Employee;
  6. use App\Entity\Poste;
  7. use App\Entity\RoleAction;
  8. use App\Entity\UserRole;
  9. use App\Utils\Consts;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. class PosteRouteVoter extends Voter
  16. {
  17.     public const CREATE 'CREATE';
  18.     public const VIEW 'VIEW';
  19.     public const EDIT 'EDIT';
  20.     protected function supports(string $attribute$subject): bool
  21.     {
  22.         // replace with your own logic
  23.         // https://symfony.com/doc/current/security/voters.html
  24.         return in_array($attribute, [self::CREATEself::VIEWself::EDIT])
  25.             && $subject instanceof \App\Entity\Poste;
  26.     }
  27.     private $entityManager;
  28.     private $security;
  29.     public function __construct(EntityManagerInterface $entityManager,Security $security)
  30.     {
  31.         $this->entityManager $entityManager;
  32.         $this->security $security;
  33.     }
  34.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  35.     {
  36.         $user $token->getUser();
  37.         if (!$user instanceof AppUser) {
  38.             // the user must be logged in; if not, deny access
  39.             return false;
  40.         }
  41.         // you know $subject is a Post object, thanks to `supports()`
  42.         /** @var Poste $poste */
  43.         $poste $subject;
  44.         $isCentral false;
  45.         $userPosteDirector $this->entityManager->getRepository(Employee::class)->findOneBy(['partner' => $user->getPartner()])->getPosteDirector();
  46.         $subjectPosteDirector $subject->getPosteDirector();
  47.         if ($this->entityManager->getRepository(RoleAction::class)->hasRoleAction($user,'isCentrale')) {
  48.             $isCentral true;
  49.         }
  50.         switch ($attribute) {
  51.             case self::VIEW:
  52.                 return $this->canView($poste$user$userPosteDirector$subjectPosteDirector$isCentral);
  53.             case self::EDIT:
  54.                 return $this->canEdit($poste$user$userPosteDirector$subjectPosteDirector$isCentral);
  55.             case self::CREATE:
  56.                 return $this->canCreate($poste$user);
  57.         }
  58.         return false;
  59.     }
  60.     private function canView(Poste $posteAppUser $user$userPosteDirector$subjectPosteDirector$isCentral): bool
  61.     {
  62.         // if they can edit, they can view
  63.         if ($this->entityManager->getRepository(RoleAction::class)->hasRoleActionWithReadPermission($user,'Poste')) {
  64.             if (($userPosteDirector == $subjectPosteDirector) || ($isCentral)) {
  65.                 return true;
  66.             }
  67.             else{ return false; }
  68.         }
  69.         else{ return false; }
  70.     }
  71.     private function canEdit(Poste $posteAppUser $user$userPosteDirector$subjectPosteDirector$isCentral): bool
  72.     {
  73.         // this assumes that the Poste object has a `getOwner()` method
  74.         if ($this->entityManager->getRepository(RoleAction::class)->hasRoleActionWithEditPermission($user,'Poste')) {
  75.             if (($userPosteDirector == $subjectPosteDirector) || ($isCentral)) {
  76.                 return true;
  77.             }
  78.             else{ return false; }
  79.         }
  80.         else{ return false; }
  81.     }
  82.     private function canCreate(Poste $posteAppUser $user): bool
  83.     {
  84.         // this assumes that the Poste object has a `getOwner()` method
  85.         return $this->entityManager->getRepository(RoleAction::class)->
  86.         hasRoleActionWithCreatePermission($user,'Poste');
  87.     }
  88. }