<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class UserVoter extends Voter
{
public const LIST = 'LIST_USERS';
public const EDIT = 'EDIT_USERS';
public function __construct(public Security $security, private ParameterBagInterface $Serviceparams)
{
}
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::EDIT, self::LIST]);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
// ... (check conditions and return true to grant permission) ...
$accessGranted = match($attribute){
self::LIST => $this->security->isGranted($this->Serviceparams->get('role_admin')),
self::EDIT => $this->security->isGranted($this->Serviceparams->get('role_admin'))
};
return $accessGranted;
}
}