src/Security/Voter/ContractVoter.php line 12

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