PHPackages                             xiaochengfu/yii2-rbac-plus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. xiaochengfu/yii2-rbac-plus

ActiveYii2-extension[Authentication &amp; Authorization](/categories/authentication)

xiaochengfu/yii2-rbac-plus
==========================

yii2权限控制自定义升级版，yii2-rbac-plus可以适应复杂的权限控制

011PHP

Since Jul 18Pushed 8y ago1 watchersCompare

[ Source](https://github.com/xiaochengfu/yii2-rbac-plus)[ Packagist](https://packagist.org/packages/xiaochengfu/yii2-rbac-plus)[ RSS](/packages/xiaochengfu-yii2-rbac-plus/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

yii2-rbac权限控制自定义升级版
===================

[](#yii2-rbac权限控制自定义升级版)

yii2权限控制自定义升级版，yii2-rbac-plus可以适应复杂的权限控制

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist xiaochengfu/yii2-rbac-plus "*"

```

or add

```
"xiaochengfu/yii2-rbac-plus": "*"

```

to the require section of your `composer.json` file.

Usage

表逻辑：

```
oauth_item:角色权限表，type=1时为角色，type=2时为权限
oauth_assignment:用户指定角色表
oauth_item_child：角色赋予权限表

扩展内容：
正常的权限逻辑是这样的：
用户>指定角色>角色分配权限，但是如果此用户不用分配角色，自身就是一个角色呢，此扩展里，我新增了这部分功能，可以在使用下面的方法时，添加user_special参数，指定此操作是不是特殊用户，更多扩展可以查看dbManager里的方法。

```

---

在主配置文件main.php文件中添加oauthManager组件：

```
 'components' => [
    'oauthManager'=> [
        'class' => 'xiaochengfu\rbacPlus\DbManager',
        //自定义表名
        'itemTable' => 'pa_oauth_item',
        'assignmentTable' => 'pa_oauth_assignment',
        'itemChildTable' => 'pa_oauth_item_child',
    ]
]

```

一：创建角色

```
$auth = Yii::$app->oauthManager;
if($this->type == self::T_ROLE){
    $item = $auth->createRole($this->name);
    $item->belong = self::BELONG_BACKEND;
    $item->description = $this->description?:'创建['.$this->name.']角色';
    return $auth->addItem($item);
}

```

二、删除角色

```
$auth = Yii::$app->oauthManager;
$result = $auth->removeRole($roleId);
if($result == false){
    throw new MethodNotAllowedHttpException('当前角色下，有关联用户，不允许删除！');
}else{
    return true;
}

```

三、更新角色

```
$auth = Yii:: $app->oauthManager;
if($this->type == self::T_ROLE){
    $item = $auth->createRole($model->name);
    $item->belong = $model->belong;
    $item->description = $this->description ?'编辑['.$this-> name. ']角色': '创建['.$this-> name. ']角色';
    return $auth->updateItem($model->id, $item);
}

```

四：用户指定角色

```
$auth = Yii:: $app->oauthManager;
$auth->revokeAll($user_id);//移除此用户之前的授权角色
$auth->assign($roleId, $user_id);

```

五：给角色分配权限

```
$auth = Yii::$app->oauthManager;

$permission = $auth->getPermissionsByRole($role);
$menu = new Menu();
if (Yii::$app->request->post()) {
    $rules = Yii::$app->request->post('permission');
    /* 判断角色是否存在 */
    if (!$parent = $auth->getRole($roleId)) {
        throw new yii\web\BadRequestHttpException('角色不存在！');
    }

    /* 删除角色所有child */
    $auth->removePermission($roleId);
    if (is_array($rules)) {
        foreach ($rules as $key =>$rule) {
            $permission['id'] = $key;
            $permission['name'] = $rule;
            $auth->addPermission($roleId,$permission);
        }
    }
}

```

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/1a10742992d52a3b211ea0790e1db0e1eb0c4230296b34b290457fa520a8d4ef?d=identicon)[xiaochengfu](/maintainers/xiaochengfu)

---

Top Contributors

[![xiaochengfu](https://avatars.githubusercontent.com/u/15496255?v=4)](https://github.com/xiaochengfu "xiaochengfu (6 commits)")

---

Tags

yii2-rbac

### Embed Badge

![Health badge](/badges/xiaochengfu-yii2-rbac-plus/health.svg)

```
[![Health](https://phpackages.com/badges/xiaochengfu-yii2-rbac-plus/health.svg)](https://phpackages.com/packages/xiaochengfu-yii2-rbac-plus)
```

###  Alternatives

[kartik-v/yii2-password

Useful password strength validation utilities for Yii Framework 2.0

761.2M17](/packages/kartik-v-yii2-password)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
