src/Security/Voter/InterventionRequestVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Authorization;
  4. use App\Entity\InterventionRequest;
  5. use App\Entity\User;
  6. use App\Manager\InterventionRequestManager;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class InterventionRequestVoter extends Voter
  11. {
  12.     public const CREATE 'CAN_CREATE';
  13.     public const READ 'CAN_READ';
  14.     public const EDIT 'CAN_EDIT';
  15.     public const DELETE 'CAN_DELETE';
  16.     private Security $security;
  17.     private InterventionRequestManager $interventionRequestManager;
  18.     public function __construct(Security $securityInterventionRequestManager $interventionRequestManager)
  19.     {
  20.         $this->security $security;
  21.         $this->interventionRequestManager $interventionRequestManager;
  22.     }
  23.     protected function supports($attribute$subject): bool
  24.     {
  25.         $supportsAttribute in_array($attribute, [self::CREATEself::DELETEself::EDITself::READ]);
  26.         $supportsSubject $subject instanceof InterventionRequest;
  27.         return $supportsAttribute && $supportsSubject;
  28.     }
  29.     /**
  30.      * @param mixed $subject
  31.      */
  32.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  33.     {
  34.         $user $this->security->getUser();
  35.         if (!$user) {
  36.             return false;
  37.         }
  38.         switch ($attribute) {
  39.             case self::CREATE:
  40.                 return $this->canCreate($subject$user);
  41.             case self::READ:
  42.                 return $this->canRead($subject$user);
  43.             case self::EDIT:
  44.                 return $this->canEdit($subject$user);
  45.             case self::DELETE:
  46.                 return $this->canDelete($subject$user);
  47.         }
  48.         return false;
  49.     }
  50.     private function canCreate(InterventionRequest $interventionRequestUser $user): bool
  51.     {
  52.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)
  53.             || $this->security->isGranted(Authorization::ROLE_OWNER_REQUESTER)
  54.         ) {
  55.             return true;
  56.         }
  57.         return false;
  58.     }
  59.     private function canRead(InterventionRequest $interventionRequestUser $user): bool
  60.     {
  61.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  62.             return true;
  63.         }
  64.         if ($this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)) {
  65.             if ($user->getCompany()->getId() === $interventionRequest->getOwnerCompany()->getId()) {
  66.                 return true;
  67.             }
  68.         }
  69.         if ($this->security->isGranted(Authorization::ROLE_SERVICE_PROVIDER_ADMIN)) {
  70.             if ($user->getCompany()->getId() === $interventionRequest->getServiceProviderCompany()->getId()) {
  71.                 return true;
  72.             }
  73.         }
  74.         return $this->interventionRequestManager->hasAccess($interventionRequest$this->security->getUser());
  75.     }
  76.     private function canEdit(InterventionRequest $interventionRequestUser $user): bool
  77.     {
  78.         if (!$this->security->isGranted(Authorization::ROLE_OWNER)
  79.             && !$this->security->isGranted(Authorization::ROLE_SERVICE_PROVIDER)) {
  80.             return false;
  81.         }
  82.         return $this->canRead($interventionRequest$user);
  83.     }
  84.     private function canDelete(InterventionRequest $interventionRequestUser $user): bool
  85.     {
  86.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  87.             return true;
  88.         }
  89.         return false;
  90.     }
  91. }