becklyn / static-roles-bundle
此包提供了一种简单的方法来定义所有可用的角色及其层次结构,并配置它们
Requires
- php: >=8.1
- symfony/config: ^6.1 || ^5.0
- symfony/dependency-injection: ^6.1 || ^5.0
- symfony/form: ^6.1 || ^5.0
- symfony/http-kernel: ^6.1 || ^5.0
- symfony/options-resolver: ^6.1 || ^5.0
- symfony/security-core: ^6.1 || ^5.0
- twig/twig: ^2.10 || ^3.0
Requires (Dev)
- symfony/phpunit-bridge: ^5.0
This package is auto-updated.
Last update: 2024-09-12 19:02:02 UTC
README
由于用户角色直接与应用程序代码绑定,并且我们希望使用现有的版本控制系统(而不是数据库)来配置我们的角色,因此此包实现了一个简单的角色系统。
您在security.yml中定义您的角色,包括层次结构,系统提供验证、列出和选择这些角色的方法。
安装
您可以通过composer安装它
$ composer require becklyn/static-roles-bundle
之后,您需要激活bundle,在您的app/AppKernel.php
public function registerBundles() { $bundles = array( // ... new \Becklyn\StaticRolesBundle\BecklynStaticRolesBundle(), // ... ); // ... }
配置
打开app/config/security.yml
,首先删除symfony自动提供的role_hierarchy
部分。
然后在文件顶部添加您自己的角色配置
becklyn_static_roles: roles: ROLE_ADMIN: title: "Admin" included_roles: [ROLE_USER] ROLE_USER: title: "User" description: "The default frontend user" tags: [tag1, tag2]
将角色分配给用户实体
此包提供了一种表单类型,可用于用户表单
$builder ->add("roles", "static_role", [ "label" => "User roles", "multiple" => true, "expanded" => true, ]);
实体中将获得一个包含角色的数组值:["ROLE_ADMIN", "ROLE_USER"]
。
这些值的映射可以使用doctrine的simple_array
类型完成。您需要将其设置为可空,以正确支持没有任何角色的用户。
class User implements UserInterface { // ... /** * @var string[] * * @ORM\Column(name="roles", type="simple_array", nullable=true) * */ private $userRoles = null; // ... /** * @inheritdoc */ public function getRoles () { return $this->roles; } /** * @param string[]|null $roles */ public function setRoles (array $roles = null) { $this->roles = $roles; } // ... }
隐藏角色
如果您正在使用应内部使用但不应在表单类型中显示的角色,您可以在角色定义中添加hidden: true
becklyn_static_roles: roles: ROLE_ADMIN: title: "Admin" included_roles: [ROLE_ALLOWED_TO_SWITCH] ROLE_ALLOWED_TO_SWITCH: title: "Internal: The user is allowed to switch roles" hidden: true
在此示例中,只有ROLE_ADMIN
将被用户选择。
标签
您可以给角色打标签。这是一种在表单类型中过滤可见角色的方法。所有至少有一个定义的标签的角色都将被包含在内。
$builder ->add("roles", "static_role", [ "label" => "User roles", "multiple" => true, "expanded" => true, "roles_with_tags" => ["tag1", "tag2"], // only include roles with either "tag1" or "tag2" ]);
becklyn_static_roles: roles: ROLE_USER_1: title: "User 1" tags: [tag1, tag3] # will be included, as it has at least one of the defined roles ROLE_USER_2: title: "User 2" tags: [tag3, tag4] # will not be included, as it has none of the defined roles
如果您在表单中未定义任何标签,所有角色都将被包含。
$builder ->add("roles", "static_role", [ "label" => "User roles", "multiple" => true, "expanded" => true, "roles_with_tags" => [], // includes all roles ]); // This is also the default value, so you can omit it: $builder ->add("roles", "static_role", [ "label" => "User roles", "multiple" => true, "expanded" => true, ]);
Twig辅助函数
staticRoleTitle(key)
根据键返回角色标题。如果未找到角色键,则返回null。
注意
如果您正在转换敏感数据,请记住,更新用户实体的角色不会自动更新认证用户令牌的角色。您需要刷新此令牌。
您可以在app/config/security.yml
中添加此配置来解决此问题
security: always_authenticate_before_granting: true