使用Symfony2的role_hierarchy [英] role_hierarchy with Symfony2
问题描述
我的role_hierarchy问题很大,
I have a big problem with my role_hierarchy,
security:
role_hierarchy:
ROLE_ADMIN:[ROLE_USER,ROLE_AUTHOR,ROLE_MODERATOR]
ROLE_SUPER_ADMIN:[ROLE_ADMIN,ROLE_ALLOWED_TO_SWITCH]
有了这个,如果我获得了SUPER_ADMIN角色,我将获得ROLE_AUTHOR,ROLE_MODERATOR,ROLE_USER和ROLE_ADMIN.但是我的问题是当我登录网站时,如果我检查分析器,我可以看到我只有ROLE_SUPER_ADMIN,而没有其他角色,所以,你能帮我吗?
with that, if i got the SUPER_ADMIN role, I will got the ROLE_AUTHOR, ROLE_MODERATOR, ROLE_USER AND ROLE_ADMIN. But my problem it's when I login on my website, if I check the profiler, i can see i got only the ROLE_SUPER_ADMIN, not the others roles, so, can you help me?
我的观点(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)
my view (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(应用程序/配置)
my security.yml (app/config)
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
请回答:)
推荐答案
我无法从您提供的代码片段中看出问题所在,因此我制作了一个小示例应用程序来逐步指导您,这可能会导致您问题的根源.
I cannot see what's wrong from the code snippets you provided, so I made a little example application to give you a step by step-instruction which might lead you to the source of the problem.
- 克隆的symfony标准版(主版)(并删除了Acme \ DemoBundle)
- 将
"friendsofsymfony/user-bundle": "dev-master"
添加到composer.json - 创建了新的包Mahok \ SecurityBundle(
php app/console generate:bundle
) - 创建了新实体
php app/console doctrine:generate:entity
- 根据FOS \ UserBundle文档修改的实体(第3步;重要:将表名更改为"user"以外的其他名称,因为这是保留字,可能会造成麻烦!)
-
根据FOS \ UserBundle文档修改了
app/AppKernel.php
,app/config/config.yml
,app/config/routing.yml
和app/config/security.yml
.供参考:这是我使用的security.yml:
- Cloned symfony-standard (master) (and removed Acme\DemoBundle)
- Added
"friendsofsymfony/user-bundle": "dev-master"
to composer.json - Created new bundle Mahok\SecurityBundle (
php app/console generate:bundle
) - Created new Entity
php app/console doctrine:generate:entity
- Modified Entity according to FOS\UserBundle documentation (step 3; Important: Change the table name to something other than "user", as this is a reserved word and might cause trouble!)
Modified
app/AppKernel.php
,app/config/config.yml
,app/config/routing.yml
andapp/config/security.yml
according to FOS\UserBundle documentation. For reference: This is the security.yml I use:
jms_security_extra:
secure_all_services: false
expressions: true
security:
encoders:
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_AUTHOR: [ROLE_USER]
ROLE_MODERATOR: [ROLE_AUTHOR]
ROLE_ADMIN: [ROLE_MODERATOR]
ROLE_SUPER_ADMIN: [ROLE_ADMIN]
providers:
fos_userbundle:
id: fos_user.user_manager
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
auth:
pattern: (^/login$|^/register|^/resetting)
anonymous: true
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
logout: true
anonymous: true
access_control:
- { path: ^/admin, role: ROLE_ADMIN }
使用`php app/console fos:user:create sa --super-admin
Created user with `php app/console fos:user:create sa --super-admin
在Mahok \ SecurityBundle中修改了DefaultController:default.html.twig,检查{% is_granted('ROLE_MODERATOR') %}
:
Modified DefaultController:default.html.twig in Mahok\SecurityBundle, checking for {% is_granted('ROLE_MODERATOR') %}
:
Hello {{ name }}!
{% if is_granted('ROLE_MODERATOR') %}
<ul>
{% for role in app.user.roles %}
<li>{{ role }}</li>
{% endfor %}
</ul>
{% else %}
oh noes!
{% endif %}
编辑:当转到localhost/example/app_dev.php/hello/User(以"sa"身份登录后)时,得到以下输出:
edit: When going to localhost/example/app_dev.php/hello/User (after logging in as "sa"), I get the following output:
Hello User!
* ROLE_SUPER_ADMIN
* ROLE_USER
这篇关于使用Symfony2的role_hierarchy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!