src/Security/Voters/Olympiad/OnlineCategoryVoter.php line 10

  1. <?php
  2. namespace App\Security\Voters\Olympiad;
  3. use App\Entity\Olympiad\Online\Category;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class OnlineCategoryVoter extends Voter
  7. {
  8.     public const REGISTRATION_OPEN 'online_category_registration_open';
  9.     /**
  10.      * Determines if the attribute and subject are supported by this voter.
  11.      *
  12.      * @param string $attribute An attribute
  13.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  14.      *
  15.      * @return bool True if the attribute and subject are supported, false otherwise
  16.      */
  17.     protected function supports($attribute$subject): bool
  18.     {
  19.         if (!in_array($attribute, [self::REGISTRATION_OPEN])) {
  20.             return false;
  21.         }
  22.         // only vote on Post objects inside this voter
  23.         if (!$subject instanceof Category) {
  24.             return false;
  25.         }
  26.         return true;
  27.     }
  28.     /**
  29.      * Perform a single access check operation on a given attribute, subject and token.
  30.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  31.      *
  32.      * @param string $attribute
  33.      * @param Category $subject
  34.      * @param TokenInterface $token
  35.      *
  36.      * @return bool
  37.      */
  38.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  39.     {
  40.         switch ($attribute) {
  41.             case self::REGISTRATION_OPEN:
  42.                 return $this->isRegistrationOpen($subject$token);
  43.         }
  44.         throw new \LogicException('This code should not be reached!');
  45.     }
  46.     private function isRegistrationOpen(Category $subjectTokenInterface $token): bool
  47.     {
  48.         if (false === $subject->isRegistrationEnable()) return false;
  49.         $max_date $subject->getRegistrationOpen()->getTo();
  50.         $min_date $subject->getRegistrationOpen()->getFrom();
  51.         $now = new \DateTime();
  52.         if (!empty($max_date) && $max_date $now) {
  53.             return false;
  54.         }
  55.         if (!empty($min_date) && $min_date $now) {
  56.             return false;
  57.         }
  58. //        dump($max_date, $min_date, $now);
  59.         return true;
  60.     }
  61. }