src/Security/Voter/Garages/GarageVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Garages;
  3. use App\Entity\Garages\Garage;
  4. use App\Entity\User;
  5. use App\Enum\UserRolesEnum;
  6. use App\Enum\VotersEnum;
  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. final class GarageVoter extends Voter
  11. {
  12.     private Security $security;
  13.     private array $voters;
  14.     public function __construct(Security $security)
  15.     {
  16.         $this->security $security;
  17.         $this->voters = [
  18.             VotersEnum::LIST_GARAGE,
  19.             VotersEnum::CREATE_GARAGE,
  20.             VotersEnum::READ,
  21.             VotersEnum::UPDATE,
  22.             VotersEnum::DELETE,
  23.             VotersEnum::EXPORT_GARAGE,
  24.             VotersEnum::PURCHASE_TRACKING_READ,
  25.             VotersEnum::UPDATE_GARAGE_OWNER,
  26.         ];
  27.     }
  28.     protected function supports(string $attribute$subject): bool
  29.     {
  30.         // first check the $subject and last if the $attribute is supported,
  31.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  32.         if ($subject && !$subject instanceof Garage) {
  33.             // only vote on these objects
  34.             return false;
  35.         }
  36.         if (in_array($attribute$this->voters)) {
  37.             // if the attribute is one we support
  38.             return true;
  39.         }
  40.         return false;
  41.     }
  42.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  43.     {
  44.         $user $token->getUser();
  45.         if (!$user instanceof User) {
  46.             // the user must be logged in; if not, deny access
  47.             return false;
  48.         }
  49.         switch ($attribute) {
  50.             case VotersEnum::LIST_GARAGE:
  51.                 return $this->canList();
  52.             case VotersEnum::CREATE_GARAGE:
  53.                 return $this->canCreate();
  54.             case VotersEnum::READ:
  55.                 return $this->canRead($subject$user);
  56.             case VotersEnum::UPDATE:
  57.                 return $this->canUpdate($subject$user);
  58.             case VotersEnum::UPDATE_GARAGE_OWNER:
  59.                 return $this->canUpdateGarageOwner($subject$user);
  60.             case VotersEnum::DELETE:
  61.                 return $this->canDelete();
  62.             case VotersEnum::EXPORT_GARAGE:
  63.                 return $this->canExport();
  64.             case VotersEnum::PURCHASE_TRACKING_READ:
  65.                 return $this->canReadPurchaseTracking($subject$user);
  66.         }
  67.         throw new \LogicException('This code should not be reached!');
  68.     }
  69.     private function canList(): bool
  70.     {
  71.         return $this->isAdminUser() || $this->isQualityAdvisorUser() || $this->isAssociatedManagerUser();
  72.     }
  73.     private function canCreate(): bool
  74.     {
  75.         return $this->isAdminUser();
  76.     }
  77.     private function canRead(Garage $garageUser $user): bool
  78.     {
  79.         if ($this->isAdminUser() || $this->isAssociatedManagerUser() || $garage->isOwner($user) || $garage->isCoordinator($user)) {
  80.             return true;
  81.         }
  82.         return false;
  83.     }
  84.     private function canUpdate(Garage $garageUser $user): bool
  85.     {
  86.         if (($this->isAdminUser() || $garage->isOwner($user) || $garage->isCoordinator($user)) && !$this->isQualityAdvisorUser() && !$this->isAssociatedManagerUser()) {
  87.             return true;
  88.         }
  89.         return false;
  90.     }
  91.     private function canUpdateGarageOwner(Garage $garageUser $user): bool
  92.     {
  93.         if (($this->isAdminUser() || $garage->isOwner($user) || $garage->isCoordinator($user)) && !$this->isAssociatedManagerUser()) {
  94.             return true;
  95.         }
  96.         return false;
  97.     }
  98.     private function canDelete(): bool
  99.     {
  100.         return $this->isAdminUser();
  101.     }
  102.     private function canExport(): bool
  103.     {
  104.         return $this->isAdminUser();
  105.     }
  106.     private function canReadPurchaseTracking(Garage $garageUser $user): bool
  107.     {
  108.         if (($this->isAdminUser() || $garage->isOwner($user) || $garage->isCoordinator($user)) || $this->isAssociatedManagerUser()) {
  109.             return true;
  110.         }
  111.         return false;
  112.     }
  113.     private function isAdminUser(): bool
  114.     {
  115.         return $this->security->isGranted(UserRolesEnum::ROLE_ADMIN_LONG);
  116.     }
  117.     private function isAssociatedManagerUser(): bool
  118.     {
  119.         return $this->security->isGranted(UserRolesEnum::ROLE_ASSOCIATED_MANAGER_LONG);
  120.     }
  121.     private function isQualityAdvisorUser(): bool
  122.     {
  123.         return $this->security->isGranted(UserRolesEnum::ROLE_QUALITY_ADVISOR_LONG);
  124.     }
  125. }