问题描述
在 Sonata admin 中,角色基于具有 CRUD 导出和主权限的管理服务名称,但考虑到 Sonata admin post 模块的示例,为其他用户创建组或为用户分配特定权限的方式看起来不太友好admin 的服务被命名为 sonata.news.admin.post
并且为该服务生成的角色看起来像
In sonata admin the roles are based on admin services name with CRUD export and master permissions but the way it looks not so user friendly to create groups for others user or assign specific permissions to a user considering an example for sonata admin post module the service for admin is named as sonata.news.admin.post
and for that service generated roles will look like
ROLE_SONATA_NEWS_ADMIN_POST_EDIT
ROLE_SONATA_NEWS_ADMIN_POST_LIST
ROLE_SONATA_NEWS_ADMIN_POST_CREATE
ROLE_SONATA_NEWS_ADMIN_POST_VIEW
ROLE_SONATA_NEWS_ADMIN_POST_DELETE
ROLE_SONATA_NEWS_ADMIN_POST_EXPORT
ROLE_SONATA_NEWS_ADMIN_POST_OPERATOR
ROLE_SONATA_NEWS_ADMIN_POST_MASTER
对于普通用户来说,难以读取/分配角色是否有更好的方法来表示生成的角色?
For a normal user its difficult to read/assign roles is there a better way for representation of generated roles ?
推荐答案
在 Sonata 管理员中,如果您希望将显示安全角色更改为用户友好的视图,您必须覆盖 Sonata 的服务下方
In Sonata admin if you wish to change display security roles as a user friendly view you have to override below sonata's services
- sonata.user.editable_role_builder
- sonata.user.form.type.security_roles
定义如下
<services>
<service id="sonata.user.editable_role_builder" class="Acme\DemoBundle\Security\EditableRolesBuilder">
<argument type="service" id="security.context" />
<argument type="service" id="sonata.admin.pool" />
<argument>%security.role_hierarchy.roles%</argument>
</service>
<service id="sonata.user.form.type.security_roles" class="Acme\DemoBundle\Form\Type\SecurityRolesType">
<tag name="form.type" alias="sonata_security_roles" />
<argument type="service" id="sonata.user.editable_role_builder" />
</service>
</services>
并在这些服务中定义您的类,我使用的演示代码 Acme\DemoBundle
现在 SecurityRolesType
类依赖于 Sonata 的 EditableRolesBuilder
你必须让它依赖于你自己的 EditableRolesBuilder
类,所以以同样的方式覆盖依赖奏鸣曲的 RestoreRolesTransformer
到你的班级
Now SecurityRolesType
class is dependendant of Sonata's EditableRolesBuilder
you have to make it dependent of your own EditableRolesBuilder
Class so in same way override dependency of sonata's RestoreRolesTransformer
to your class
我已将所有角色转换为 SecurityRolesType.php
中的模块智能角色数组,并将其传递以查看您可以在此文件中查看的所有自定义
I have transformed all roles to an array of module wise roles in SecurityRolesType.php
and passed it to view all customization you can view in this file
还可以覆盖角色的树枝模板,您可以通过处理 vendor\sonata-project\user-bundle\Resources\views
中的 form_admin_fields.html.twig
和添加 app\Resources\SonataUserBundle\views\Form
路径它将覆盖父树枝文件,在树枝文件中,我尝试使用手风琴控制引导程序来显示角色模块明智并具有适当的权限
Also override the twig template for roles you can overrride it by coping form_admin_fields.html.twig
from vendor\sonata-project\user-bundle\Resources\views
and adding app\Resources\SonataUserBundle\views\Form
path it will override parent twig file,In twig file i have tried to use accordion control bootstrap to display roles module wise and with appropriate permissions
注意:此代码只会显示权限[Create,Edit,View,List,Export,Delete,Master] 不处理自定义权限
最后一步在主配置文件 config.yml
Last step import your service file in main configuration file that is config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: @AcmeDemoBundle/Resources/config/admin.xml }
对于完整的代码演示,您可以在下面的存储库中找到它
For full code demo you can find it in below repository
这篇关于Sonata 管理员增强的安全角色视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!