<?php
namespace App\Security;
use App\Entity\ExamFillEntry;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ExamFillEntryVoter extends Voter
{
const CREATE = 'create';
const UPDATE = 'update';
protected function supports(string $attribute, $subject)
{
if (!in_array($attribute, [self::CREATE, self::UPDATE])) {
return false;
}
if (!$subject instanceof ExamFillEntry) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
/** @var ExamFillEntry $examFillEntry */
$examFillEntry = $subject;
switch ($attribute) {
case self::CREATE:
case self::UPDATE:
return $this->canCreate($examFillEntry, $user);
}
}
private function canCreate(ExamFillEntry $examFillEntry, User $user)
{
return $examFillEntry->canUserCreate($user);
}
}