src/Security/Voter/MagasinVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Magasin;
  4. use App\Entity\UserAccess;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. class MagasinVoter extends AbstractVoter
  7. {
  8.     public const VIEW 'Magasin:View';
  9.     public const EDIT 'Magasin:Edit';
  10.     private ?Magasin $magasin null;
  11.     protected function supports(string $attributemixed $subject): bool
  12.     {
  13.         return in_array($attribute, [self::EDITself::VIEW])
  14.             && $subject instanceof Magasin;
  15.     }
  16.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  17.     {
  18.         $this->user $token->getUser();
  19.         $this->magasin $subject;
  20.         return match ($attribute) {
  21.             self::VIEW => $this->canViewMagasin(),
  22.             self::EDIT => $this->canEditMagasin(),
  23.             default => false,
  24.         };
  25.     }
  26.     private function canViewMagasin(): bool
  27.     {
  28.         // Read-only superadmins can view all magasins
  29.         if ($this->isReadOnlySuperAdmin()) {
  30.             return true;
  31.         }
  32.         if ($this->isSuperAdmin()) {
  33.             return true;
  34.         }
  35.         if ($this->user->canAccessResource($this->magasin->getGroupement(), [UserAccess::ROLE_ADMIN])) {
  36.             return true;
  37.         }
  38.         return $this->isAdmin()
  39.             && $this->user->canAccessResource($this->magasin, [UserAccess::ROLE_ADMIN]);
  40.     }
  41.     private function canEditMagasin(): bool
  42.     {
  43.         // Only full superadmins can edit, not read-only
  44.         return parent::canEdit();
  45.     }
  46. }