src/Voter/__Booking/BookingContractVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Voter\__Booking;
  3. use App\Entity\Booking\Booking;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class BookingContractVoter extends Voter
  8. {
  9.     const DETAILS "BOOKING_CONTRACT_DETAILS";
  10.     const GET_CONTRACT "BOOKING_CONTRACT_GET_CONTRACT";
  11.     protected function supports($attribute$subject): bool
  12.     {
  13.         return in_array($attribute, [
  14.                 self::DETAILS,
  15.                 self::GET_CONTRACT,
  16.             ]) && ($subject === null || $subject instanceof Booking);
  17.     }
  18.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  19.     {
  20.         $loggedUser $token->getUser();
  21.         if (!$loggedUser instanceof UserInterface) {
  22.             return false;
  23.         }
  24.         if ($subject === null) {
  25.             return false;
  26.         }
  27.         switch ($attribute) {
  28.             case self::DETAILS:
  29.                 return $this->voteOnDetails($subject$loggedUser);
  30.             case self::GET_CONTRACT:
  31.                 return $this->voteOnGetContract($subject$loggedUser);
  32.         }
  33.         return false;
  34.     }
  35.     private function voteOnDetails(?Booking $bookingUserInterface $loggedUser): bool
  36.     {
  37.         return in_array(self::DETAILS$loggedUser->getRoles());
  38.     }
  39.     private function voteOnGetContract(?Booking $bookingUserInterface $loggedUser): bool
  40.     {
  41.         return in_array(self::GET_CONTRACT$loggedUser->getRoles());
  42.     }
  43. }