FOSUserBundle:根据角色设置不同的角色和身份验证URL

时间:2014-09-19 18:38:36

标签: php symfony

我应该如何修改security.yml以实现这种情况?

情景:

  • mysitessss.com - > studentlecturermanager可以访问
  • mysitessss.com/admin - > lecturermanager可以访问
  • mysitessss.com/admin/private - > manager可以访问

我应该运行哪个确切的命令来创建相应的用户?

  • 学生:php app / console fos:user:create ?????????
  • 教师:php app / console fos:user:create ?????????
  • 经理:php app / console fos:user:create ?????????

我读了documentationthis post,但我感到困惑!

的routing.yml

home:
    path: /

admin:
    path: /admin

admin_private:
    path: /admin/private

应用/配置/ security.yml

security:
    role_hierarchy:
        ROLE_STUDENT: ?????????
        ROLE_LECTURER: ????????????
        ROLE_MANAGER: ???????

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, role: ????????? }
        - { path: ^/admin, role: ????????? }
        - { path: ^/admin/private, role: ?????????? }

1 个答案:

答案 0 :(得分:1)

我认为您的security.yml看起来像这样:

security:
    role_hierarchy:
        ROLE_LECTURER: [ROLE_STUDENT]
        ROLE_MANAGER: [ROLE_LECTURER]

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, role: ROLE_STUDENT }
        - { path: ^/admin, role: ROLE_LECTURER }
        - { path: ^/admin/private, role: ROLE_MANAGER }

您可以创建每个用户并为其提升角色:

$ php app/console fos:user:create student
$ php app/console fos:user:promote student ROLE_STUDENT
$ php app/console fos:user:create lecturer
$ php app/console fos:user:promote lecturer ROLE_LECTURER
# ... etc.