src/Security/Voter/SolicitationVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Beneficiary;
  4. use App\Entity\ContractLine;
  5. use App\Entity\Solicitation;
  6. use App\Entity\User;
  7. use App\Service\BookingService;
  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 SolicitationVoter extends Voter
  13. {
  14.     public const CREATE 'create_solicitation';
  15.     public const EDIT 'edit_solicitation';
  16.     public const SHOW 'show_solicitation';
  17.     /** @var Security $security */
  18.     private $security;
  19.     /** @var BookingService $bookingService */
  20.     private $bookingService;
  21.     /**
  22.      * SolicitationVoter 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.         return true;
  41.     }
  42.     /**
  43.      * @inheritDoc
  44.      */
  45.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  46.     {
  47.         $user $token->getUser();
  48.         if (!$user instanceof User) {
  49.             // the user must be logged in; if not, deny access
  50.             return false;
  51.         }
  52.         switch ($attribute) {
  53.             case self::CREATE:
  54.                 return $this->canCreate($subject);
  55.             case self::EDIT:
  56.                 return $this->canEdit($subject);
  57.             case self::SHOW:
  58.                 return $this->canShow();
  59.         }
  60.         throw new LogicException('This code should not be reached!');
  61.     }
  62.     /**
  63.      * @param $subject
  64.      * @return bool
  65.      */
  66.     private function canCreate($subject): bool
  67.     {
  68.         /** @var Beneficiary $beneficiary */
  69.         $beneficiary $subject['beneficiary'];
  70.         /** @var ContractLine $ontractLine */
  71.         $ontractLine $subject['contractLine'];
  72.         // Quantity 0 is unlimited, so we can create another solicitation
  73.         $availableQuantities $this->bookingService->getAvailableQuantity($ontractLine$beneficiarynull);
  74.         return (
  75.             $ontractLine->getEnabledRelevantPeriod() &&
  76.             $this->security->isGranted('ROLE_ATCLIENTE') &&
  77.             $availableQuantities
  78.         );
  79.     }
  80.     /**
  81.      * @param Solicitation $solicitation
  82.      * @return bool
  83.      */
  84.     private function canEdit(Solicitation $solicitation): bool
  85.     {
  86.         $ontractLine $solicitation->getContractLine();
  87.         // Quantity 0 is unlimited, so we can create another solicitation
  88.         return ($ontractLine->getEnabledRelevantPeriod() && $this->security->isGranted('ROLE_ATCLIENTE'));
  89.     }
  90.     /**
  91.      * @return bool
  92.      */
  93.     private function canShow(): bool
  94.     {
  95.         return $this->security->isGranted('ROLE_ATCLIENTE');
  96.     }
  97. }