src/Security/Voter/Garages/GarageAppointmentVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Garages;
  3. use App\Entity\Garages\GarageAppointment;
  4. use App\Entity\User;
  5. use App\Enum\MenuRolesAssociatedEnum;
  6. use App\Enum\MenuRolesManagerEnum;
  7. use App\Enum\UserRolesEnum;
  8. use App\Enum\VotersEnum;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Security;
  12. class GarageAppointmentVoter extends Voter
  13. {
  14.     private Security $security;
  15.     private array $voters;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.         $this->voters = [
  20.             VotersEnum::LIST_GARAGE_APPOINTMENT,
  21.             VotersEnum::CREATE_GARAGE_APPOINTMENT,
  22.             VotersEnum::READ,
  23.             VotersEnum::UPDATE,
  24.             VotersEnum::DELETE,
  25.             VotersEnum::EXPORT_GARAGE_APPOINTMENT,
  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 GarageAppointment) {
  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.         // you know $subject is a GarageAppointment object, thanks to `supports()`
  50.         /** @var GarageAppointment $learningCourse */
  51.         $garageAppointment $subject;
  52.         switch ($attribute) {
  53.             case VotersEnum::LIST_GARAGE_APPOINTMENT:
  54.                 return $this->canList();
  55.             case VotersEnum::CREATE_GARAGE_APPOINTMENT:
  56.                 return $this->canCreate();
  57.             case VotersEnum::READ:
  58.                 return $this->canRead($garageAppointment$user);
  59.             case VotersEnum::UPDATE:
  60.                 return $this->canUpdate($garageAppointment$user);
  61.             case VotersEnum::EXPORT_GARAGE_APPOINTMENT:
  62.                 return $this->canExport();
  63.         }
  64.         throw new \LogicException('This code should not be reached!');
  65.     }
  66.     private function canList(): bool
  67.     {
  68.         return $this->isAdminUser() || $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_GARAGE_APPOINTMENTS) || $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_GARAGE_APPOINTMENTS_ASSOCIATED);
  69.     }
  70.     private function canCreate(): bool
  71.     {
  72.         return $this->isAdminUser();
  73.     }
  74.     private function canRead(GarageAppointment $garageAppointmentUser $user): bool
  75.     {
  76.         return $this->isAdminUser() || $this->isOwner($garageAppointment$user);
  77.     }
  78.     private function canUpdate(GarageAppointment $garageAppointmentUser $user): bool
  79.     {
  80.         return $this->isAdminUser() || $this->isOwner($garageAppointment$user);
  81.     }
  82.     private function canExport(): bool
  83.     {
  84.         return $this->isAdminUser();
  85.     }
  86.     private function isOwner(GarageAppointment $garageAppointmentUser $user): bool
  87.     {
  88.         if ($this->isAdminUser()) {
  89.             return true;
  90.         } elseif ($this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
  91.             foreach ($user->getWorkProvinces() as $province) {
  92.                 if ($garageAppointment->getGarage()->getAddress()->getProvince()->getId() === $province->getId()) {
  93.                     return true;
  94.                 }
  95.             }
  96.         } else {
  97.             if ($garageAppointment->getGarage()->getOwner()->getId() === $user->getId()) {
  98.                 return true;
  99.             }
  100.         }
  101.         return false;
  102.     }
  103.     private function isAdminUser(): bool
  104.     {
  105.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_GARAGE_APPOINTMENTS)
  106.             || $this->security->isGranted(UserRolesEnum::ROLE_QUALITY_ADVISOR_LONG)
  107.             ;
  108.     }
  109. }