src/Security/Voter/ParentVoter.php line 11
<?php
namespace App\Security\Voter;
use App\Enum\RoleType;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class ParentVoter extends Voter
{
public const EDIT = 'PARENT_EDIT';
public const SHOW = 'PARENT_SHOW';
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [self::EDIT, self::SHOW]);
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
$accessIsGranted = match ($attribute) {
'PARENT_SHOW' =>
$user->getId() === $subject['user_id']
||
$this->security->isGranted(RoleType::ROLE_EMPLOYEE) ||
$this->security->isGranted(RoleType::ROLE_PEDAGOGICAL_DIRECTOR),
'PARENT_EDIT' =>
$user === $subject->getUser()
||
$this->security->isGranted('ROLE_ADMIN'),
};
return $accessIsGranted;
}
}