Доброе утро, я хочу обновить свое приложение symfony 2.8 до версии 5.4.
У меня проблемы с функцией is_granted в symfony.
У моего подключенного пользователя есть все роли в токене (классические роли + пользовательские роли). Пользовательские роли связаны с компанией, и каждый пользователь компании наследует роли компании.
Я добавляю все роли в токен, однако, когда я помещаю is_granted в остальной класс, Я получаю отказ в доступе. Не могли бы вы сказать мне, если я что-то забыл?
Мой класс REST с функцией is_granted:
Код: Выделить всё
/**
* TrainingSession controller.
* @RouteResource("Training")
* @Security("is_granted('MODULE_TRAINING')")
*/
class TrainingSessionRESTController extends BaseRESTController
{
Код: Выделить всё
"ROLE_HR"
"ROLE_DEBUG"
"ROLE_PANEL"
"ROLE_USER"
"ROLE_MANAGER"
"MODULE_LIBRARY"
"MODULE_EXPENSE_REPORT"
"MODULE_TRAINING"
"MODULE_ANNUAL_REVIEW"
"MODULE_PRO_REVIEW"
"MODULE_COMPANY_CHART"
"MODULE_COMPANY_CONTACT"
"MODULE_COMPANY_DOCUMENT"
"MODULE_CALENDAR"
"MODULE_FOLDER"
"MODULE_TIME_MANAGEMENT"
"MODULE_EMPLOYEE_MEDICAL"
"MODULE_REPORT"
Это мой собственный RoleVoter, и я пытался установить пустой префикс, но безуспешно:
Код: Выделить всё
namespace WORD\CoreBundle\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
/**
* RoleVoter votes if any attribute starts with a given prefix.
*
* @author Fabien Potencier
*/
class CustomVoter implements CacheableVoterInterface
{
private $prefix;
public function __construct(string $prefix = 'ROLE_')
{
$this->prefix = 'ROLE_';
}
/**
* {@inheritdoc}
*/
public function vote(TokenInterface $token, $subject, array $attributes)
{
$result = VoterInterface::ACCESS_ABSTAIN;
$roles = $this->extractRoles($token);
foreach ($attributes as $attribute) {
if (!\is_string($attribute) || !str_starts_with($attribute, $this->prefix)) {
continue;
}
if ('ROLE_PREVIOUS_ADMIN' === $attribute) {
trigger_deprecation('symfony/security-core', '5.1', 'The ROLE_PREVIOUS_ADMIN role is deprecated and will be removed in version 6.0, use the IS_IMPERSONATOR attribute instead.');
}
$result = VoterInterface::ACCESS_DENIED;
foreach ($roles as $role) {
if ($attribute === $role) {
return VoterInterface::ACCESS_GRANTED;
}
}
}
return $result;
}
public function supportsAttribute(string $attribute): bool
{
return str_starts_with($attribute, $this->prefix);
}
public function supportsType(string $subjectType): bool
{
return true;
}
protected function extractRoles(TokenInterface $token)
{
return $token->getRoleNames();
}
}
Источник: https://stackoverflow.com/questions/745 ... refix-role