src/Security/Voter/SessionVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Show\Session;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class SessionVoter extends Voter
  9. {
  10.     /** @required */
  11.     public Security $securityChecker;
  12.     protected function supports(string $attribute$subject): bool
  13.     {
  14.         return in_array($attribute, ['SESSION_NOT_EXPIRED''SESSION_IS_EXPIRED'], true) && $subject instanceof Session;
  15.     }
  16.     /**
  17.      * @param string $attribute
  18.      * @param Session $subject
  19.      * @param TokenInterface $token
  20.      * @return bool
  21.      */
  22.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  23.     {
  24.         if ($attribute === 'SESSION_NOT_EXPIRED') {
  25.             if ($this->securityChecker->isGranted('ROLE_ORDER_WRITE')) {
  26.                 return true;
  27.             }
  28.             return $subject->getFrom() > new \DateTimeImmutable();
  29.         }
  30.         if ($attribute === 'SESSION_IS_EXPIRED') {
  31.             return $subject->getFrom() < new \DateTimeImmutable();
  32.         }
  33.     }
  34. }