src/Security/Voter/Savills/WorkValidationRequestVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Savills;
  3. use App\Entity\Authorization;
  4. use App\Entity\Savills\WorkValidationRequest;
  5. use App\Entity\User;
  6. use App\Manager\Savills\WorkValidationRequestManager;
  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 WorkValidationRequestVoter 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 WorkValidationRequestManager $workValidationRequestManager;
  18.     public function __construct(Security $securityWorkValidationRequestManager $workValidationRequestManager)
  19.     {
  20.         $this->security $security;
  21.         $this->workValidationRequestManager $workValidationRequestManager;
  22.     }
  23.     protected function supports($attribute$subject): bool
  24.     {
  25.         $supportsAttribute in_array($attribute, [self::CREATEself::DELETEself::EDITself::READ]);
  26.         $supportsSubject $subject instanceof WorkValidationRequest;
  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(WorkValidationRequest $workValidationRequestUser $user): bool
  51.     {
  52.         if ($this->security->isGranted(Authorization::ROLE_ADMIN) || $this->security->isGranted(Authorization::ROLE_SAVILLS_TECHNICAL_MANAGER)) {
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57.     private function canRead(WorkValidationRequest $workValidationRequestUser $user): bool
  58.     {
  59.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  60.             return true;
  61.         }
  62.         if ($this->security->isGranted(Authorization::ROLE_SAVILLS_RENTAL_MANAGER) || $this->security->isGranted(Authorization::ROLE_SAVILLS_TECHNICAL_ASSISTANT)) {
  63.             if ($user->getCompany()->getId() === $workValidationRequest->getOwnerCompany()->getId()) {
  64.                 return true;
  65.             }
  66.         }
  67.         /*if ($this->security->isGranted(Authorization::ROLE_SERVICE_PROVIDER_ADMIN)) {
  68.             if ($user->getCompany()->getId() === $workValidationRequest->getServiceProviderCompany()->getId()) {
  69.                 return true;
  70.             }
  71.         }*/
  72.         return $this->workValidationRequestManager->hasAccess($workValidationRequest$this->security->getUser());
  73.     }
  74.     private function canEdit(WorkValidationRequest $workValidationRequestUser $user): bool
  75.     {
  76.         if ($this->security->isGranted(Authorization::ROLE_SAVILLS_RENTAL_MANAGER) || $this->security->isGranted(Authorization::ROLE_SAVILLS_TECHNICAL_ASSISTANT)) {
  77.             if ($user->getCompany()->getId() === $workValidationRequest->getOwnerCompany()->getId()) {
  78.                 return true;
  79.             }
  80.         }
  81.         return $this->canRead($workValidationRequest$user);
  82.     }
  83.     private function canDelete(workValidationRequest $workValidationRequestUser $user): bool
  84.     {
  85.         if ($this->security->isGranted(Authorization::ROLE_SAVILLS_OWNER_ADMIN)) {
  86.             return true;
  87.         }
  88.         return false;
  89.     }
  90. }