<?php
namespace App\Security;
use App\Entity\FacultyInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class FacultyVoter extends Voter
{
private const VIEW = 'VIEW';
private AuthorizationCheckerInterface $authorizationChecker;
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
{
$this->authorizationChecker = $authorizationChecker;
}
protected function supports(string $attribute, mixed $subject): bool
{
return $subject instanceof FacultyInterface &&
$attribute === self::VIEW;
}
/**
* @param string $attribute
* @param FacultyInterface $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$subjectsFaculty = $subject?->getFacultyIdentifier();
return $this->authorizationChecker->isGranted("ROLE_FACULTY_".$subjectsFaculty);
}
}