我有一個很大的問題,我role_hierarchy,role_hierarchy與Symfony2的
security:
role_hierarchy:
ROLE_ADMIN:[ROLE_USER,ROLE_AUTHOR,ROLE_MODERATOR]
ROLE_SUPER_ADMIN:[ROLE_ADMIN,ROLE_ALLOWED_TO_SWITCH]
用,如果我得到了超級管理員的角色,我就得到了ROLE_AUTHOR,ROLE_MODERATOR,ROLE_USER和ROLE_ADMIN。但我的問題是,當我在我的網站上登錄時,如果我檢查分析器,我可以看到我只有ROLE_SUPER_ADMIN,而不是其他角色,所以,你能幫我嗎?
我的觀點(base.html.twig
)
<h3>Blog</h3>
<ul class="nav nav-pills nav-stacked">
<li><a href="{{ path('dom_home') }}">Home Page</a></li>
{% if is_granted('ROLE_AUTHOR') %}
<li><a href="{{ path('dom_add') }}">Add a post</a></li>
{% endif %}
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
<li><a href="{{ path('fos_user_security_logout') }}">Logout</a></li>
{% else %}
<li><a href="{{ path('fos_user_security_login') }}">login</a></li>
<li><a href="{{ path('fos_user_registration_register') }}">register</a></li>
{% endif %}
</ul>
我security.yml
(應用程序/配置)
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_ADMIN: [ROLE_USER,ROLE_AUTHOR,ROLE_MODERATOR]
ROLE_SUPER_ADMIN: [ROLE_ADMIN,ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
fos_userbundle:
id: fos_user.user_manager
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/(login$|register|resetting)
anonymous: true
main:
pattern: ^/
form_login:
provider: fos_userbundle
remember_me: true
always_use_default_target_path: true
default_target_path: /dom/
remember_me:
key: %secret%
anonymous: false
logout: true
編輯:
我的觀點(base.html.twig)
<h3>Blog</h3>
<ul class="nav nav-pills nav-stacked">
<li><a href="{{ path('dom_home') }}">Home Page</a></li>
{% if is_granted('ROLE_AUTHOR') %}
<li><a href="{{ path('dom_add') }}">Add a post</a></li>
{% endif %}
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
<li><a href="{{ path('fos_user_security_logout') }}">Logout</a></li>
{% else %}
<li><a href="{{ path('fos_user_security_login') }}">login</a></li>
<li><a href="{{ path('fos_user_registration_register') }}">register</a></li>
{% endif %}
</ul>
我security.yml(應用程序/配置)
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_ADMIN: [ROLE_USER,ROLE_AUTHOR,ROLE_MODERATOR]
ROLE_SUPER_ADMIN: [ROLE_ADMIN,ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
fos_userbundle:
id: fos_user.user_manager
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/(login$|register|resetting)
anonymous: true
main:
pattern: ^/
form_login:
provider: fos_userbundle
remember_me: true
always_use_default_target_path: true
default_target_path: /dom/
remember_me:
key: %secret%
anonymous: false
logout: true
請回答:)
層級並不意味着,你是明確添加到這些角色,即當您檢查分析器你沒有看到所有的角色。這隻意味着訪問是「繼承的」。換句話說,即使您沒有分配此角色,但當您將某個操作的訪問限制爲ROLE_MODERATOR時,仍然可以通過ROLE_SUPER_ADMIN繼承訪問權限來訪問該操作。 – dbrumann
好的,但如果我在做is_granted('ROLE_MODERATOR'),我將能夠訪問它?像:{%if is_granted('ROLE_MODERATOR')%}主持人{%else%}沒有版主{%endif%},在一個樹枝模板中,我可以看到版主? – Stickly
我剛剛嘗試這個,它不起作用 – Stickly