简体   繁体   English

如何在symfony2应用程序中管理角色

[英]How to manage roles in symfony2 application

I have a web application where i have 5 level of users like 我有一个Web应用程序,其中有5个级别的用户,例如

  1. Administrator 管理员
  2. OrganisationHead OrganisationHead
  3. LocalCOmpany head 本地公司负责人
  4. Teacher 老师
  5. parents 父母
  6. students 学生们

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.

相关问题 带有FOSUserBundle的Symfony2 - 如何将没有层次结构的角色添加到我的应用程序中 - Symfony2 with FOSUserBundle - how to add roles without hierarchy to my application 如何部署symfony2应用程序? - How to deploy symfony2 application? 如何在Symfony2中的单独表中存储用户角色? - How to store user roles in separate table in Symfony2? 如何在 Symfony2 中动态地将“角色”添加到“路径”? - How I can add a “roles” to a “path” dynamically in Symfony2? 如何在使用symfony2(和fosUserBundle)登录时动态添加用户角色? - how to add user roles dynamically upon login with symfony2 (and fosUserBundle)? 如何在Symfony2 FOSUserBundle中向某些用户角色添加自定义字段? - How to add custom fields to some user roles in the Symfony2 FOSUserBundle? 管理角色并为角色分配权限 - Symfony - Manage Roles and Assign Permissions to Roles - Symfony Symfony2 / JMS Serializer - 如何管理不同的对象结构? - Symfony2 / JMS Serializer - How to manage differents object structure? 如何动态处理Symfony2中的角色|权限:使用动态角色限制功能 - How to dynamic handle roles|permissions in Symfony2: restrict functions with dynamic roles 如何在symfony2中获取应用程序实例 - how to get application instance in symfony2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM