<?php
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\User;
use App\Entity\Batch;
/**
*
* @author "wendell.zheng <wxzheng@ustc.edu.cn>"
*/
class BatchVoter extends Voter
{
const APPLICANT_ACTION = 'applicant_action';
const COLLEGE_ACTION = 'college_action';
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
protected function supports($attribute, $subject): bool
{
if (! in_array($attribute, [
self::APPLICANT_ACTION,
self::COLLEGE_ACTION
])) {
return false;
}
if (! $subject instanceof Batch) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (! $user instanceof User) {
return false;
}
if ($user->getUserIdentifier() == 'P0336') {
return true;
}
/** @var Batch $batch */
$batch = $subject;
switch ($attribute) {
case self::APPLICANT_ACTION:
return $this->canApplicantAction($batch, $user);
case self::COLLEGE_ACTION:
return $this->canCollegeAction($batch, $user);
}
throw new \LogicException('This code should not be reached!');
}
protected function canApplicantAction(Batch $batch, User $user): bool
{
return $batch->isApplyActive();
}
protected function canCollegeAction(Batch $batch, User $user): bool
{
return $batch->isCollegeActive();
}
}