Symfony Security和FOSUserBundle:ROLE_ADMIN权限出现问题

时间:2018-09-18 15:03:05

标签: symfony fosuserbundle symfony-security

如果用户将div归因于他,我想显示一个特定的ROLE_ADMIN。在数据库中,用户具有角色ROLE_ADMINROLE_USER

a:1:{i:0;s:10:"ROLE_ADMIN";}

但是,当我使用以下代码时,未授予用户权限,并且未显示div

{% if is_granted('ROLE_ADMIN') %}
     <div class="settings">...</div>
{% endif %}

如果我改用is_granted('ROLE_USER'),则会显示div。

Twig转储向我显示用户确实将两个角色都归因于他。

关于为什么此代码无法按预期运行的任何想法?

其他代码信息:

security.yml:

 security:
        encoders:
            FOS\UserBundle\Model\UserInterface: bcrypt

        role_hierarchy:
            ROLE_ADMIN:       ROLE_ADMIN
            ROLE_SUPER_ADMIN: ROLE_ADMIN

        providers:
           fos_userbundle:
                id: fos_user.user_provider.username_email

        firewalls:
            dev:
                pattern: ^/(_(profiler|wdt)|css|images|js)/
                security: false
            main:
                pattern: ^/
                form_login:
                    provider: fos_userbundle
                    csrf_token_generator: security.csrf.token_manager
                    login_path: /login
                    check_path: /login_check
                oauth:
                    resource_owners:
                        facebook:           "/login/check-facebook"
                        google:             "/login/check-google"
                    login_path:        /login
                    failure_path:      /login
                    oauth_user_provider:
                        service: my_user_provider
                logout:       true
                anonymous:    true

            login:
                pattern:  ^/login$
                security: false

                remember_me:
                    secret: "%env(APP_SECRET)%"
                    always_remember_me: true
                    path: /
                    domain: ~

        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 }

getRoles()功能通过FOSUserBundle中的User实体进行处理:

public function getRoles()
    {
        $roles = $this->roles;

        foreach ($this->getGroups() as $group) {
            $roles = array_merge($roles, $group->getRoles());
        }

        // we need to make sure to have at least one role
        $roles[] = static::ROLE_DEFAULT;

        return array_unique($roles);
    }

0 个答案:

没有答案