<?php
namespace App\Security\Voter;
use App\Constant\ClassNameRoleActionConstant;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class AllEntityVoter extends Voter
{
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [
ClassNameRoleActionConstant::VIEW,
ClassNameRoleActionConstant::CREATE,
ClassNameRoleActionConstant::EDIT,
ClassNameRoleActionConstant::DELETE,
ClassNameRoleActionConstant::API,
]);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
/** @var User $user */
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
$className = strtolower((new \ReflectionClass($subject))->getShortName());
$settingArray = [
'reproduct',
'season',
'period',
'culture',
];
if(in_array($className,$settingArray)){
$className = 'settings';
}
// ... (check conditions and return true to grant permission) ...
$roleGroup = $user->getRoleGroup();
if (!empty($roleGroup)) {
$roleActions = $roleGroup->getRoleActions();
if ($roleActions->containsKey($className)) {
$roleAction = $roleActions->get($className);
return $roleAction->$attribute();
}
}
return false;
}
}