<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class StudentVoter extends Voter
{
private $security;
public function __construct( Security $security)
{
$this->security = $security;
}
protected function supports(string $attribute, $subject): bool
{
if(!$this->security->getUser())
return false;
if(in_array("ROLE_STUDENT", $this->security->getUser()->getRoles()))
return true;
return false;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
if (!(in_array("ROLE_STUDENT", $user->getRoles()))) {
return false;
}
return true;
}
}