src/Security/Voter/ClientOrderVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\ClientOrder;
  4. use App\Entity\UserAccess;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. class ClientOrderVoter extends AbstractVoter
  7. {
  8.     public const VIEW 'ClientOrder:View';
  9.     public const EDIT 'ClientOrder:Edit';
  10.     private ?ClientOrder $clientOrder null;
  11.     protected function supports(string $attributemixed $subject): bool
  12.     {
  13.         return in_array($attribute, [self::VIEWself::EDIT])
  14.             && $subject instanceof ClientOrder;
  15.     }
  16.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  17.     {
  18.         $this->user $token->getUser();
  19.         $this->clientOrder $subject;
  20.         return match ($attribute) {
  21.             self::VIEW => $this->canViewClientOrder(),
  22.             self::EDIT => $this->canEditClientOrder(),
  23.             default => false,
  24.         };
  25.     }
  26.     private function canViewClientOrder(): bool
  27.     {
  28.         // Read-only superadmins can view all client orders
  29.         if ($this->isReadOnlySuperAdmin()) {
  30.             return true;
  31.         }
  32.         if ($this->isSuperAdmin()) {
  33.             return true;
  34.         }
  35.         // Check if user has access to the project's groupement or the project itself
  36.         $project $this->clientOrder->getProject();
  37.         if (!$project) {
  38.             return false;
  39.         }
  40.         $magasin $project->getMagasin();
  41.         if (!$magasin) {
  42.             return false;
  43.         }
  44.         // Check groupement access
  45.         if ($this->user->canAccessResource($magasin->getGroupement(), [UserAccess::ROLE_ADMIN])) {
  46.             return true;
  47.         }
  48.         // Check direct project/magasin access
  49.         return $this->isAdmin()
  50.             && $this->user->canAccessResource($magasin, [UserAccess::ROLE_ADMIN]);
  51.     }
  52.     private function canEditClientOrder(): bool
  53.     {
  54.         // Full superadmins can always edit (not read-only)
  55.         if ($this->isSuperAdmin() && !$this->isReadOnlySuperAdmin()) {
  56.             return true;
  57.         }
  58.         // Check if user has admin access to the project's groupement or the project itself
  59.         $project $this->clientOrder->getProject();
  60.         if (!$project) {
  61.             return false;
  62.         }
  63.         $magasin $project->getMagasin();
  64.         if (!$magasin) {
  65.             return false;
  66.         }
  67.         // Check groupement admin access
  68.         if ($this->user->canAccessResource($magasin->getGroupement(), [UserAccess::ROLE_ADMIN])) {
  69.             return true;
  70.         }
  71.         // Check direct project/magasin admin access
  72.         return $this->isAdmin()
  73.             && $this->user->canAccessResource($magasin, [UserAccess::ROLE_ADMIN]);
  74.     }
  75. }