src/Security/Voter/BookingVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Booking;
  4. use App\Entity\Solicitation;
  5. use App\Entity\User;
  6. use App\Service\BookingService;
  7. use LogicException;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. class BookingVoter extends Voter
  12. {
  13.     public const CREATE 'create_booking';
  14.     public const EDIT 'edit_booking';
  15.     public const SHOW 'show_booking';
  16.     /** @var Security $security */
  17.     private $security;
  18.     /** @var BookingService $bookingService */
  19.     private $bookingService;
  20.     /**
  21.      * BookingVoter constructor.
  22.      * @param Security $security
  23.      * @param BookingService $bookingService
  24.      */
  25.     public function __construct(Security $securityBookingService $bookingService)
  26.     {
  27.         $this->security $security;
  28.         $this->bookingService $bookingService;
  29.     }
  30.     /**
  31.      * @inheritDoc
  32.      */
  33.     protected function supports($attribute$subject): bool
  34.     {
  35.         // if the attribute isn't one we support, return false
  36.         if (!in_array($attribute, [self::CREATEself::EDITself::SHOW])) {
  37.             return false;
  38.         }
  39.         // only vote on Booking objects inside this voter
  40.         if (!$subject instanceof Booking && !$subject instanceof Solicitation) {
  41.             return false;
  42.         }
  43.         return true;
  44.     }
  45.     /**
  46.      * @inheritDoc
  47.      */
  48.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  49.     {
  50.         $user $token->getUser();
  51.         if (!$user instanceof User) {
  52.             // the user must be logged in; if not, deny access
  53.             return false;
  54.         }
  55.         switch ($attribute) {
  56.             case self::CREATE:
  57.                 return $this->canCreate($subject);
  58.             case self::EDIT:
  59.                 return $this->canEdit($subject);
  60.             case self::SHOW:
  61.                 return $this->canShow();
  62.         }
  63.         throw new LogicException('This code should not be reached!');
  64.     }
  65.     /**
  66.      * @param Solicitation $solicitation
  67.      * @return bool
  68.      */
  69.     private function canCreate(Solicitation $solicitation): bool
  70.     {
  71.         $beneficiary $solicitation->getBeneficiary();
  72.         $collectiveLine $solicitation->getContractLine();
  73.         // Quantity 0 is unlimited, so we can create another solicitation
  74.         $availableQuantities $this->bookingService->getAvailableQuantity($collectiveLine$beneficiarynull);
  75.         return (
  76.             $collectiveLine->getEnabledRelevantPeriod() &&
  77.             $this->security->isGranted('ROLE_ATCLIENTE') &&
  78.             $availableQuantities
  79.         );
  80.     }
  81.     /**
  82.      * @param Booking $booking
  83.      * @return bool
  84.      */
  85.     private function canEdit(Booking $booking): bool
  86.     {
  87.         $serviceBag $booking->getSolicitation()->getContractLine();
  88.         // Quantity 0 is unlimited, so we can create another solicitation
  89.         return ($serviceBag->getEnabledRelevantPeriod() && $this->security->isGranted('ROLE_ATCLIENTE'));
  90.     }
  91.     /**
  92.      * @return bool
  93.      */
  94.     private function canShow(): bool
  95.     {
  96.         return $this->security->isGranted('ROLE_ATCLIENTE');
  97.     }
  98. }