src/Security/Voter/EquipmentVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Service\VoterSecurityService;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class EquipmentVoter extends Voter
  7. {
  8.     public const EDIT 'EDIT';
  9.     public const VIEW 'VIEW';
  10.     public const ADD_FILE 'ADD_FILE';
  11.     public const VIEW_PREMIUM 'VIEW_PREMIUM';
  12.     public function __construct(private readonly VoterSecurityService $voterSecurityService)
  13.     {
  14.     }
  15.     protected function supports(string $attribute$subject): bool
  16.     {
  17.         // replace with your own logic
  18.         // https://symfony.com/doc/current/security/voters.html
  19.         return in_array($attribute, [self::EDITself::VIEWself::ADD_FILEself::VIEW_PREMIUM])
  20.             && $subject instanceof \App\Entity\Equipment;
  21.     }
  22.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  23.     {
  24.         $user $token->getUser();
  25.         if (!$this->voterSecurityService->isUserAllowed($user)) {
  26.             return false;
  27.         }
  28.         if (!$this->voterSecurityService->isUserInElement($user$subject)) {
  29.             return false;
  30.         }
  31.         return match ($attribute) {
  32.             self::VIEW => true,
  33.             self::VIEW_PREMIUM => $user->isPremium(),
  34.             self::EDIT => $this->voterSecurityService->allowedEditElement($subject),
  35.             self::ADD_FILE => (bool) $subject->isEnabled() && $this->voterSecurityService->isUserManagerCompany(),
  36.             default => false,
  37.         };
  38.     }
  39. }