php 我如何检查 symfony2 中的用户角色是否不属于定义的 security.yml 模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12287740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check for user role in symfony2 for urls not falling under patterns defined security.yml?
提问by aditya
I have a admin panel and I have defined a role for it ROLE_ADMIN. In my security.yml file I am using a pattern ^/admin/*so every thing under /admin requires ROLE_ADMIN. Now in frontend of my app I need to check user role and if role is ROLE_ADMINrender one file and otherwise render another file. This url does not fall under the pattern defined in security.yml.
我有一个管理面板,我已经为它定义了一个角色ROLE_ADMIN。在我的 security.yml 文件中,我使用了一个模式,^/admin/*所以 /admin 下的每件事都需要ROLE_ADMIN. 现在在我的应用程序的前端,我需要检查用户角色,以及角色是否ROLE_ADMIN呈现一个文件,否则呈现另一个文件。此 url 不属于 security.yml 中定义的模式。
So how do I check whether the user is admin or a normal user on the homepage which does not fall under the pattern defined in security.yml ?
那么如何检查主页上的用户是管理员还是普通用户,不属于 security.yml 中定义的模式?
回答by Elnur Abdurrakhimov
Enable the firewall on the whole app using the ^/pattern, permit anonymous access and use access_controlto restrict access:
使用^/模式在整个应用程序上启用防火墙,允许匿名访问并用于access_control限制访问:
security:
firewalls:
secured_area:
pattern: ^/
anonymous: ~
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
As @itsmequinn suggested, use the isGranted()method of the security context:
正如@itsmequinn 建议的那样,使用isGranted()安全上下文的方法:
if ($this->get('security.context')->isGranted('ROLE_BRAND')) {
// the user has the ROLE_BRAND role, so act accordingly
}
In Symfony 2.6, security.contexthas been split into two separate services. Hence you need to use the security.authorization_checkerservice to solve the problem:
在Symfony 2.6 中,security.context已经拆分为两个独立的服务。因此,您需要使用该security.authorization_checker服务来解决问题:
if ($this->get('security.authorization_checker')->isGranted('ROLE_BRAND')) {
// the user has the ROLE_BRAND role, so act accordingly
}
回答by Anil
SecurityContext will be deprecated in Symfony 3.0
SecurityContext 将被弃用 Symfony 3.0
Prior to Symfony 2.6you would use SecurityContext.SecurityContextwill be deprecated in Symfony 3.0in favour of the AuthorizationChecker.
之前Symfony 2.6你会使用SecurityContext. SecurityContext将被弃用以Symfony 3.0支持AuthorizationChecker.
For Symfony 2.6+& Symfony 3.0use AuthorizationChecker.
供Symfony 2.6+&Symfony 3.0使用AuthorizationChecker。
Symfony 2.5 (and below)
Symfony 2.5(及以下)
if ($this->get('security.context')->isGranted('ROLE_ADMIN')) {
# User is a ROLE_ADMIN
}
Symfony 2.6 (and above)
Symfony 2.6(及以上)
if ($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
# User is a ROLE_ADMIN
}
Similar Question: How to check if an user is logged in Symfony2 inside a controller?
类似问题:如何检查用户是否在控制器内登录 Symfony2?
Read more the docs here: AuthorizationChecker
在此处阅读更多文档: AuthorizationChecker
回答by itsmequinn
Are you in the controller for the page? If so, use the isGrantedmethod of the security context: Access Controls for Controllers
你在页面的控制器中吗?如果是这样,请使用isGranted安全上下文的方法:控制器的访问控制
回答by Franky238
Easiest solution for this are annotations. Instead of this:
最简单的解决方案是注释。取而代之的是:
if ($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
# User is a ROLE_ADMIN
}
.. try use this:
.. 尝试使用这个:
/**
* ...
* @Security("has_role('ROLE_ADMIN')")
*/
.. or :
.. 或者 :
/**
* ...
* @Security("is_granted('POST_ADD', post)")
*/
public function addAction(Post $post){...}
You can read more about Security annotations here. Annotations are best practice in Symfony 2 look hereEnjoy!
回答by P. Piotr
In Symfony 4 and aboveyou should use code like below, instead of using services like $this->get('security.authorization_checker'):
在Symfony 4 及更高版本中,您应该使用如下代码,而不是使用 $this->get('security.authorization_checker') 之类的服务:
$hasAccess = $this->isGranted('ROLE_ADMIN');
$this->denyAccessUnlessGranted('ROLE_ADMIN');

