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

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