src/Security/Voter/CompanyVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Company;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class CompanyVoter extends Voter
  10. {
  11.     public const USER_EDIT 'USER_EDIT';
  12.     public const USER_DELETE 'USER_DELETE';
  13.     public const COMPANY_SHOW 'COMPANY_SHOW';
  14.     private $security;
  15.     public function __construct(Security $security)
  16.     {
  17.         $this->security $security;
  18.     }
  19.     protected function supports(string $attribute$subject): bool
  20.     {
  21.         return in_array($attribute, [self::USER_DELETEself::USER_EDIT,self::COMPANY_SHOW])
  22.             && ( $subject instanceof User || $subject instanceof Company);
  23.     }
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         //utilisateur connecté
  27.         $user $token->getUser();
  28.         // if the user is anonymous, do not grant access
  29.         if (!$user instanceof UserInterface) {
  30.             return false;
  31.         }
  32.         switch ($attribute) {
  33.             case self::USER_EDIT:
  34.                 return $this->canUserEdit($subject$user);
  35.             case self::USER_DELETE:
  36.                 return $this->canUserDelete($subject$user);
  37.             case self::COMPANY_SHOW:
  38.                 return $this->canCompanyShow$subject$user);
  39.         }
  40.         return false;
  41.     }
  42.     private function canUserEdit(User $userTargetUserInterface $user): bool
  43.     {
  44.         if($this->security->isGranted('ROLE_ADMIN')){
  45.             return true;
  46.         }
  47.         if($this->security->isGranted('ROLE_RH')){
  48.             if($userTarget->getCompany() === $user->getCompany()){
  49.                 return true;
  50.             }
  51.         }
  52.         return false;
  53.     }
  54.     private function canUserDelete(User $userTargetUserInterface $user): bool
  55.     {
  56.         if($this->security->isGranted('ROLE_ADMIN')){
  57.             return true;
  58.         }
  59.         if($this->security->isGranted('ROLE_RH')){
  60.             if($userTarget->getCompany() === $user->getCompany()){
  61.                 return true;
  62.             }
  63.         }
  64.         return false;
  65.     }
  66.     private function canCompanyShow(Company $companyUserInterface $user): bool
  67.     {
  68.         if($this->security->isGranted('ROLE_ADMIN')){
  69.             return true;
  70.         }
  71.         if($this->security->isGranted('ROLE_RH')){
  72.             if($company === $user->getCompany()){
  73.                 return true;
  74.             }
  75.         }
  76.         return false;
  77.     }
  78. }