[英]How to manage roles in symfony2 application
I have a web application where i have 5 level of users like 我有一个Web应用程序,其中有5个级别的用户,例如
Now every userwill have different landing page or with some more or less tabs or features. 现在,每个用户将具有不同的登录页面,或具有更多或更少的选项卡或功能。
Now i want to know that is it i only make it as one Entity and then Roles in different entity so that i can simplify the login process 现在我想知道的是,我只是将其作为一个实体,然后将其作为不同的实体,这样我才能简化登录过程
You should use the twig function is_granted()
instead and let the Security component handle the roles. 您应该改用树枝函数
is_granted()
并让安全组件处理角色。 This have a few advances such as nested roles definitions. 这有一些进步,例如嵌套角色定义。 You can find a lot more information at http://symfony.com/doc/current/book/security.html#roles .
您可以在http://symfony.com/doc/current/book/security.html#roles中找到更多信息。
I strongly urge you to go that way! 我强烈建议您采取这种方式!
{% if is_granted('ROLE_ADMINISTRATOR') %}
Do something
{% endif %}
Here is an example of how you would go about it 这是您将如何处理的示例
Your controller: 您的控制器:
public function indexAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
return $this->render('YourBundle:Page:index.html.twig', array('user_role' => $user->getRole()));
}
Twig: 枝条:
{% if user_role == "administrator" %}
Menu 1, menu 2, menu3
{% elseif user_role == "organizationhead" %}
Menu 1, Menu 4
{% elseif user_role == "etc" %}
Menu 5
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.