承接 becklyn/static-roles-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

becklyn/static-roles-bundle

Composer 安装命令:

composer require becklyn/static-roles-bundle

包简介

This bundle provides a simple way to define all available roles and their hierarchy in your configuration

README 文档

README

As user roles are directly coupled to the application code and we would like to configure our roles using an existing VCS (instead of the DB) this bundle implements a simple role system.

You define your roles including the hierarchy in your security.yml and the system provides ways to validated that, list them and for you to select them.

Installation

You can install it via composer:

$ composer require becklyn/static-roles-bundle

Afterwards, you need to activate the bundle in your app/AppKernel.php:

public function registerBundles()
{
    $bundles = array(
        // ...
        new \Becklyn\StaticRolesBundle\BecklynStaticRolesBundle(),
        // ...
    );

    // ...
}

Configuration

Open up app/config/security.yml and first remove the section role_hierarchy that is automatically provided by symfony.

Then add your own role configuration on top of the file:

becklyn_static_roles:
    roles:
        ROLE_ADMIN:
            title: "Admin"
            included_roles: [ROLE_USER]
        ROLE_USER:
            title: "User"
            description: "The default frontend user"
            tags: [tag1, tag2]

Assigning roles to a user entity

The bundle provides a form type to be used in user forms:

$builder
    ->add("roles", "static_role", [
        "label" => "User roles",
        "multiple" => true,
        "expanded" => true,
    ]);

You will receive an array of roles in the entity as values: ["ROLE_ADMIN", "ROLE_USER"].

The mapping of these values can be done using the simple_array type of doctrine. You need to set it nullable to properly support a user without any roles.

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 roles

If you are using roles, that should be used internally, but shouldn't be presented in the form type, you can add hidden: true to the role definition:

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

In this example, only ROLE_ADMIN will be selectable by the user.

Tagging

You can tag roles. This is a way to filter the visible roles in the form type. All roles that have at least one of your defined tags will be included.

$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

If you don't define any tags in your form, all roles will be included.

$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 helper functions

staticRoleTitle(key)

Returns the role title by key. Returns null if the role key is not found.

Note

If you are transforming sensitive data, please keep in mind that updating the roles of the user entity won't automatically update the roles of the authenticated user token. You need to refresh this token.

You can fix this issue by adding this configuration in your app/config/security.yml:

security:
    always_authenticate_before_granting: true

becklyn/static-roles-bundle 适用场景与选型建议

becklyn/static-roles-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.06k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 07 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「security」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 becklyn/static-roles-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 becklyn/static-roles-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 becklyn/static-roles-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-07-09