<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\Show\Session;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class SessionVoter extends Voter
{
/** @required */
public Security $securityChecker;
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, ['SESSION_NOT_EXPIRED', 'SESSION_IS_EXPIRED'], true) && $subject instanceof Session;
}
/**
* @param string $attribute
* @param Session $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if ($attribute === 'SESSION_NOT_EXPIRED') {
if ($this->securityChecker->isGranted('ROLE_ORDER_WRITE')) {
return true;
}
return $subject->getFrom() > new \DateTimeImmutable();
}
if ($attribute === 'SESSION_IS_EXPIRED') {
return $subject->getFrom() < new \DateTimeImmutable();
}
}
}