src/Security/Voters/Olympiad/DirectionVoter.php line 9

  1. <?php
  2. namespace App\Security\Voters\Olympiad;
  3. use App\Entity\Olympiad\Online\Direction;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class DirectionVoter extends Voter
  7. {
  8.     public const DIRECTION_REGISTRATION_OPEN 'direction_registration_open';
  9.     public const DIRECTION_WORKS_SHOW 'direction_works_show';
  10.     public const DIRECTION_APPEAL_SHOW 'direction_appeal_show';
  11.     /**
  12.      * Determines if the attribute and subject are supported by this voter.
  13.      *
  14.      * @param string $attribute An attribute
  15.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  16.      *
  17.      * @return bool True if the attribute and subject are supported, false otherwise
  18.      */
  19.     protected function supports($attribute$subject): bool
  20.     {
  21.         if (!in_array($attribute, [self::DIRECTION_REGISTRATION_OPENself::DIRECTION_WORKS_SHOWself::DIRECTION_APPEAL_SHOW])) {
  22.             return false;
  23.         }
  24.         // only vote on Post objects inside this voter
  25.         if (!$subject instanceof Direction) {
  26.             return false;
  27.         }
  28.         return true;
  29.     }
  30.     /**
  31.      * Perform a single access check operation on a given attribute, subject and token.
  32.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  33.      *
  34.      * @param string $attribute
  35.      * @param Direction $subject
  36.      * @param TokenInterface $token
  37.      *
  38.      * @return bool
  39.      */
  40.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  41.     {
  42.         switch ($attribute) {
  43.             case self::DIRECTION_REGISTRATION_OPEN:
  44.                 return $this->isRegistrationOpen($subject$token);
  45.             case self::DIRECTION_WORKS_SHOW:
  46.                 return $this->isWorksShow($subject$token);
  47.             case self::DIRECTION_APPEAL_SHOW:
  48.                 return $this->isAppealShow($subject$token);
  49.         }
  50.         throw new \LogicException('This code should not be reached!');
  51.     }
  52.     private function isRegistrationOpen(Direction $subjectTokenInterface $token): bool
  53.     {
  54.         $max_date $subject->getCategory()->getRegistrationOpen()->getTo();
  55.         $min_date $subject->getCategory()->getRegistrationOpen()->getFrom();
  56.         $now = new \DateTime();
  57.         if (!empty($max_date) && $max_date $now) {
  58.             return false;
  59.         }
  60.         if (!empty($min_date) && $min_date $now) {
  61.             return false;
  62.         }
  63. //        dump($max_date, $min_date, $now);
  64.         return true;
  65.     }
  66.     private function isWorksShow(Direction $subjectTokenInterface $token): bool
  67.     {
  68.         $max_date $subject->getWorkShow()->getTo();
  69.         $min_date $subject->getWorkShow()->getFrom();
  70.         $now = new \DateTime();
  71.         if (!empty($min_date) && $min_date $now) {
  72.             return false;
  73.         }
  74.         if (!empty($max_date) && $max_date $now) {
  75.             return false;
  76.         }
  77.         if (empty($min_date)) {
  78.             return false;
  79.         }
  80. //        dump($max_date, $min_date, $now);
  81.         return true;
  82.     }
  83.     private function isAppealShow(Direction $subjectTokenInterface $token): bool
  84.     {
  85.         $max_date $subject->getAppealShow()->getTo();
  86.         $min_date $subject->getAppealShow()->getFrom();
  87.         $now = new \DateTime();
  88.         if (!empty($min_date) && $min_date $now) {
  89.             return false;
  90.         }
  91.         if (!empty($max_date) && $max_date $now) {
  92.             return false;
  93.         }
  94.         if (empty($min_date)) {
  95.             return false;
  96.         }
  97. //        dump($max_date, $min_date, $now);
  98.         return true;
  99.     }
  100. }