我尝试根据用户角色配置路由。我是这样做的:
服务:
admin.group:
...
calls:
- [ setTokenStorage, [ "@security.token_storage" ] ]
Admin类:
public function setTokenStorage (TokenStorageInterface $tokenStorage) {
$this->tokenStorage = $tokenStorage;
$this->user = null;
$token = $this->tokenStorage->getToken();
if (null !== $token && is_object($token->getUser())) {
$this->user = $token->getUser();
}
}
protected function configureRoutes(RouteCollection $collection)
{
if ($this->user && $this->user->hasRole('ROLE_SUPER_ADMIN')) {
$collection->remove('delete');
}
else {
$collection->clearExcept(array('list'));
}
}
I get a crash:
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "sonata_group_edit" as such route does not exist.") in SonataAdminBundle:CRUD:base_list_field.html.twig at line 23.
似乎我们在configureRoutes中传递了两次,第一次user是null,第二次user不是null。$this->user->hasRole('ROLE_SUPER_ADMIN')第二次为true,但它会导致崩溃。
我没有使用SonataUserBundle,因为它不适合Symfony 3。
我该怎么做?泰
我的错,我以为Sonata Security不会做这项工作,但它做了!
我创建了特定的角色:
ROLE_THEME_VIEWER:
- ROLE_ADMIN_THEME_LIST
ROLE_GROUP_VIEWER:
- ROLE_ADMIN_GROUP_LIST
ROLE_RULES_VIEWER:
- ROLE_ADMIN_RULES_LIST
And:
ROLE_ADMIN: [..., ROLE_THEME_VIEWER, ROLE_GROUP_VIEWER, ROLE_RULES_VIEWER]
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_SONATA_ADMIN, ROLE_ALLOWED_TO_SWITCH]
https://sonata-project.org/bundles/admin/master/doc/reference/security.html 如果你仍然想使用"configureRoutes"来检查访问权限,你应该读这个:
https://github.com/sonata-project/SonataAdminBundle/issues/2590正如pulzarraider所说,这不是一个好主意。为了使我的代码工作,您必须每次都清除缓存。