src/Security/Voters/Olympiad/OnlineCategoryVoter.php line 10
<?php
namespace App\Security\Voters\Olympiad;
use App\Entity\Olympiad\Online\Category;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class OnlineCategoryVoter extends Voter
{
public const REGISTRATION_OPEN = 'online_category_registration_open';
/**
* Determines if the attribute and subject are supported by this voter.
*
* @param string $attribute An attribute
* @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
*
* @return bool True if the attribute and subject are supported, false otherwise
*/
protected function supports($attribute, $subject): bool
{
if (!in_array($attribute, [self::REGISTRATION_OPEN])) {
return false;
}
// only vote on Post objects inside this voter
if (!$subject instanceof Category) {
return false;
}
return true;
}
/**
* Perform a single access check operation on a given attribute, subject and token.
* It is safe to assume that $attribute and $subject already passed the "supports()" method check.
*
* @param string $attribute
* @param Category $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::REGISTRATION_OPEN:
return $this->isRegistrationOpen($subject, $token);
}
throw new \LogicException('This code should not be reached!');
}
private function isRegistrationOpen(Category $subject, TokenInterface $token): bool
{
if (false === $subject->isRegistrationEnable()) return false;
$max_date = $subject->getRegistrationOpen()->getTo();
$min_date = $subject->getRegistrationOpen()->getFrom();
$now = new \DateTime();
if (!empty($max_date) && $max_date < $now) {
return false;
}
if (!empty($min_date) && $min_date > $now) {
return false;
}
// dump($max_date, $min_date, $now);
return true;
}
}