src/App/Security/Voter/ReservationVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Enums\User\RolesType;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class ReservationVoter extends Voter
  9. {
  10.     public const VIEW 'view';
  11.     public function __construct(
  12.         private RequestStack $requestStack
  13.     ) {
  14.     }
  15.     protected function supports(string $attribute$subject): bool
  16.     {
  17.         return in_array($attribute, [self::VIEW])
  18.             && is_array($subject);
  19.     }
  20.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  21.     {
  22.         $request $this->requestStack->getCurrentRequest();
  23.         $backOfficeRoles = (new RolesType())->getBackOfficeRoles();
  24.         $user $token->getUser();
  25.         if (!$user instanceof UserInterface) {
  26.             return false;
  27.         }
  28.         return match ($attribute) {
  29.             self::VIEW => (int) $subject['company_id'] === $user->getCompany()->getId() ||
  30.                 in_array($request->attributes->get('userRole'), $backOfficeRoles),
  31.             default => false,
  32.         };
  33.     }
  34. }