简体   繁体   English

Symfony2 FosUserBundle角色ROLE_ADMIN

[英]Symfony2 FosUserBundle Roles ROLE_ADMIN

I've got this problem. 我有这个问题。

Have configured my Sf2 env with FosUserBundle and FacebookBundle. 使用FosUserBundle和FacebookBundle配置了我的Sf2 env。

I've managed to to some ADMIN only section on the frontend (meaning moderator thing). 我设法在前端的一些ADMIN only部分(意思是主持人的事情)。 By default, every user created in my project has ROLE_USER only. 默认情况下,在我的项目中创建的每个用户都只有ROLE_USER。

The "moderator" thing can only by accessed by having ROLE_ADMIN. “主持人”事物只能通过拥有ROLE_ADMIN来访问。 The problem is that even I add the role by " $user->addRole('ROLE_ADMIN') , checking if user has this role failed. I would like to show some stuff if user will have this kind of role, but I cannot. 问题是,即使我通过“ $user->addRole('ROLE_ADMIN')添加角色,检查用户是否有此角色失败。我想展示一些东西,如果用户将有这种角色,但我不能。

Neither " {% if is_granted('ROLE_ADMIN') %} ", nor " $this->container->get('security.context')->isGranted('ROLE_ADMIN') " succedded. 既不是“ {% if is_granted('ROLE_ADMIN') %} ”,也不是“ $this->container->get('security.context')->isGranted('ROLE_ADMIN') ”已成功。

Everytime I'm getting FALSE or nothing when it comes to TWIG. 每当我在TWIG上获得FALSE或什么都没有。

Checking if user has ROLE_USER works. 检查用户是否有ROLE_USER工作。

Just to be sure I'm adding my config stuff. 只是为了确保我添加我的配置内容。

security:
encoders:
    FOS\UserBundle\Model\UserInterface: sha512
    Symfony\Component\Security\Core\User\User: plaintext

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    chain_provider:
        chain:
          providers: [fos_userbundle, my_fos_facebook_provider]
    fos_userbundle:
        id: user_provider
    my_fos_facebook_provider:
        id: my.facebook.user


firewalls:
    public:
        pattern: ^/
        form_login:
            login_path: /login
            check_path: /login_check
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            use_referer: true
        fos_facebook:
            app_url: "http://www.facebook.com/apps/application.php?id={{APPID}}"
            server_url: "http://l.local/app_dev.php/"
            login_path: /login
            check_path: /login_fb_check
            default_target_path: /
            provider: my_fos_facebook_provider
            use_referer: true
        logout:
            path:   /logout
            invalidate_session: false
        anonymous:    true

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/, role: ROLE_ADMIN }
    - { path: ^/secured/.*, role: IS_AUTHENTICATED_FULLY }
    - { path: ^/facebook/,  role: [ROLE_FACEBOOK] }
    - { path: ^/dodaj$, role: ROLE_USER }
    - { path: ^/.*, role: [IS_AUTHENTICATED_ANONYMOUSLY] }

Please, if anyone could help, cause I do not know what to do. 请,如果有人可以提供帮助,因为我不知道该怎么做。

I'm always checking to be sure if my role("ROLE_ADMIN") is added in my database, and in fact, it is. 我总是在检查以确定我的角色(“ROLE_ADMIN”)是否已添加到我的数据库中,事实上,它是。

The role is process at the session generation (connection, login ..). 角色是会话生成过程(连接,登录..)。 So you get it from the security context (session). 所以你从安全上下文(会话)中得到它。 Directly in a twig template or from the securityContext object elsewhere : 直接在twig模板中或从其他地方的securityContext对象:

In twig template use is_granted('ROLE_ADMIN') is_granted('ROLE_ADMIN')模板中使用is_granted('ROLE_ADMIN')

In controller (with security context) use $securityContext->isGranted('ROLE_ADMIN') 在控制器(带安全上下文)中使用$securityContext->isGranted('ROLE_ADMIN')

Don't forget to re-signing after a role change. 角色更改后不要忘记重新签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM