deadmantfa/yii2-rbac 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

deadmantfa/yii2-rbac

Composer 安装命令:

composer require deadmantfa/yii2-rbac

包简介

Yii2 RBAC Module, updated for PHP 8 and above

README 文档

README

Yii2

This is a fork of the Yii 2 RBAC extension by JustCoded. It extends the RBAC Manager with a route-based access control system, offering additional features and compatibility with modern PHP versions.

Features

Pre-defined Roles and Permissions

The extension provides the following pre-defined roles and permissions:

Permissions:

  • * (master permission): A parent permission for all other permissions.
  • administer: Permission to check access to the admin panel.

Roles:

  • Guest: Not authenticated users.
  • Authenticated: Authenticated users (add this to your users manually).
  • Administrator: Users with administer permission, granting access to the admin panel.
  • Master: Superuser role with access to everything via * permission.

Routes Scanner

The extension includes a feature to scan your project files and automatically import permissions for:

  • Controller-wide permissions: {controller->uniqueId}/*
  • Action-specific permissions: {controller->uniqueId}/{action->id}

You can create or assign roles and permissions to configure your application's high-level access control.

Route Access Filter

Easily restrict access to specific parts of your site based on roles or permissions. The extension provides a filter similar to Yii's AccessControl, enabling route-based permissions checks. If access is denied, a 403 Forbidden error is triggered.

GUI for Managing Roles and Permissions

A simple GUI is included to manage roles and permissions directly from the application.

Note: The GUI is in alpha. Avoid sharing access to this interface with end-users.

Installation

Install the extension via Composer:

composer require deadmantfa/yii2-rbac

Alternatively, add the following to your composer.json:

"deadmantfa/yii2-rbac": "*"

Configuration

Component Setup

Add the RBAC module and authManager configuration in your application:

'modules' => [
    'rbac' => [
        'class' => 'deadmantfa\yii2\rbac\Module',
    ],
],
'components' => [
    'authManager' => [
        'class' => 'deadmantfa\yii2\rbac\components\DbManager',
        //'class' => 'deadmantfa\yii2\rbac\components\PhpManager',
    ],
],

Bootstrap 4 Themes Support

By default, the views use Bootstrap 3 via yii2-bootstrap. For Bootstrap 4 support, update the container configuration:

'container' => [
    'definitions' => [
        'deadmantfa\yii2\rbac\widgets\RbacGridView' => [
            'class' => \app\modules\admin\widgets\RbacGridView::class,
        ],
        'deadmantfa\yii2\rbac\widgets\RbacActiveForm' => [
            'class' => \yii\bootstrap4\ActiveForm::class,
        ],
    ],
],

Note: Add yiisoft/yii2-bootstrap4 to your composer.json.

Basic RBAC Configuration

Follow the official Yii 2 RBAC documentation to configure RBAC storage (e.g., create necessary files or database tables).

For DbManager, initialize the database tables with the following migration command:

yii migrate --migrationPath=@yii/rbac/migrations

Initialize Base Roles

Run the following commands to set up default roles and permissions:

# Initialize base roles and permissions
php yii rbac/init

# Assign the master role to a user (replace 1 with the user ID)
php yii rbac/assign-master 1

# Scan application routes for permissions
php yii rbac/scan

For Advanced Template:

php yii rbac/scan -p='@vendor/deadmantfa/yii2-rbac' -b='rbac/'

For Basic Template:

php yii rbac/scan -p='@vendor/deadmantfa/yii2-rbac' -b='admin/rbac/'

Usage

GUI Interface

Access the RBAC GUI by navigating to the module's configured route. Use the GUI to manage roles and permissions.

Note: The role-permission selector is a temporary solution and may not display a proper tree structure. This will be addressed in future updates.

Route Access Filter

Use the RouteAccessControl filter to enforce route-based access control. The filter checks permissions during each request and throws a 403 Forbidden error for unauthorized routes.

Per Controller

public function behaviors()
{
    return [
        'routeAccess' => [
            'class' => 'deadmantfa\yii2\rbac\filters\RouteAccessControl',
        ],
    ];
}

Globally

'as routeAccess' => [
    'class' => 'deadmantfa\yii2\rbac\filters\RouteAccessControl',
    'allowActions' => [
        'site/*',
    ],
    'allowRegexp' => '/(gii)/i', // Optional
],

Example Project

You can see an example of this RBAC extension in action in the Yii2 Starter Kit.

deadmantfa/yii2-rbac 适用场景与选型建议

deadmantfa/yii2-rbac 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 deadmantfa/yii2-rbac 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 70
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2025-01-03