src/Security/Voter/BookingIntervalVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Booking;
  4. use App\Entity\BookingInterval;
  5. use App\Entity\User;
  6. use App\Service\BookingService;
  7. use App\Service\SolicitationService;
  8. use LogicException;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Security;
  12. class BookingIntervalVoter extends Voter
  13. {
  14.     public const CREATE 'create_booking_interval';
  15.     public const EDIT 'edit_booking_interval';
  16.     public const SHOW 'show_booking_interval';
  17.     /** @var Security $security */
  18.     private $security;
  19.     /** @var SolicitationService $solicitationService */
  20.     private $bookingService;
  21.     /**
  22.      * BookingIntervalVoter constructor.
  23.      * @param Security $security
  24.      * @param BookingService $bookingService
  25.      */
  26.     public function __construct(Security $securityBookingService $bookingService)
  27.     {
  28.         $this->security $security;
  29.         $this->bookingService $bookingService;
  30.     }
  31.     /**
  32.      * @inheritDoc
  33.      */
  34.     protected function supports($attribute$subject): bool
  35.     {
  36.         // if the attribute isn't one we support, return false
  37.         if (!in_array($attribute, [self::CREATEself::EDITself::SHOW])) {
  38.             return false;
  39.         }
  40.         if (!$subject instanceof Booking && !$subject instanceof BookingInterval) {
  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 Booking $booking
  67.      * @return bool
  68.      */
  69.     private function canCreate(Booking $booking): bool
  70.     {
  71.         $beneficiary $booking->getSolicitation()->getBeneficiary();
  72.         $collectiveLine $booking->getSolicitation()->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 BookingInterval $bookingInterval
  83.      * @return bool
  84.      */
  85.     private function canEdit(BookingInterval $bookingInterval): bool
  86.     {
  87.         $serviceBag $bookingInterval->getBooking()->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. }