src/Voter/__Accounting/AccountingDepartureVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Voter\__Accounting;
  3. use App\Entity\__Marketing\Departure\Departure;
  4. use App\Voter\VoterTrait;
  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 AccountingDepartureVoter extends Voter
  9. {
  10.     use VoterTrait;
  11.     const LIST = "ACCOUNTING_DEPARTURE_LIST";
  12.     const DETAILS "ACCOUNTING_DEPARTURE_DETAILS";
  13.     protected function supports($attribute$subject): bool
  14.     {
  15.         return in_array($attribute, [
  16.                 self::LIST,
  17.                 self::DETAILS,
  18.             ]) && ($subject === null || $subject instanceof Departure);
  19.     }
  20.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  21.     {
  22.         $loggedUser $token->getUser();
  23.         if (!$loggedUser instanceof UserInterface) {
  24.             return false;
  25.         }
  26.         switch ($attribute) {
  27.             case self::LIST:
  28.                 return $this->voteOnList($loggedUser);
  29.             case self::DETAILS:
  30.                 return $this->voteOnDetails($subject$loggedUser);
  31.         }
  32.         return false;
  33.     }
  34.     private function voteOnList(UserInterface $loggedUser): bool
  35.     {
  36.         return in_array(self::LIST, $loggedUser->getRoles());
  37.     }
  38.     private function voteOnDetails(?Departure $departureUserInterface $loggedUser): bool
  39.     {
  40.         if (null === $departure) {
  41.             return false;
  42.         }
  43.         return in_array(self::DETAILS$loggedUser->getRoles());
  44.     }
  45. }