首先我自己没有使用自带的user表
要想使用yii rbac组件 首先需要在我们的配置文件中开启 rbac组件 
在common/config/main-local.php 中添加 下边的组建
‘authManager‘ => [
    ‘class‘ => ‘yii\rbac\DbManager‘,
    ‘itemTable‘ => ‘auth_item‘,
    ‘assignmentTable‘ => ‘auth_assignment‘,
    ‘itemChildTable‘ => ‘auth_item_child‘,
],
接下来  创建我们的表
首先创建 控制权限的四张表 
这四张表的sql在  我们的yii的核心文件中  具体路径是
vendor/yiisoft/yii2/rbac/migrations/schma-mysql.sql
这是我们操作mysql的sql语句
 
另外的文件分别操作  sql server  oracle postgresql sqlite
然后  因为需要个我们的用户分配权限  我们还需要使用我们的yii的用户表  这个用户表的sql  下边给出
可以参考地址 http://www.yiichina.com/question/285
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `auth_key` varchar(32) NOT NULL,
  `password_hash` varchar(255) NOT NULL,
  `password_reset_token` varchar(255) DEFAULT NULL,
  `email` varchar(255) NOT NULL,
  `role` smallint(6) NOT NULL DEFAULT ‘10‘,
  `status` smallint(6) NOT NULL DEFAULT ‘10‘,
  `created_at` int(11) NOT NULL,
  `updated_at` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
在这里  我偷懒了一下  没有自己在写一个登录和注册功能  而是使用yii自带的登录功能  影响不大 因为 我们在进行权限操作
时,只需要只要是谁有权限就行了,其他信息不需要。如果是在项目中,user表 可以自定义
下边开始 实现rbac。我们对自己要求高一点,代码都要符合yii的规范。尽量不要使用自己的方法而是用框架封装好的
首先  创建一个model层   Rbac.php
<?php
/**
 * Created by PhpStorm.
 * User: jinlei
 * Date: 2017/2/16
 * Time: 9:58
 */
namespace frontend\models;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\base\Model;
class Rbac extends Model
{
    public $power;
    public $role;
}
然后创建控制器   RbacController.php
<?php
/**
 * Created by PhpStorm.
 * User: jinlei
 * Date: 2017/1/13
 * Time: 8:47
 */
namespace backend\controllers;
use backend\models\Rbac;
use yii\web\Controller;
use yii;
class RbacController  extends Controller
{
   
}
 
下边开始权限的操作
第一步  添加权限 
    首先 来一个添加权限的页面 
    public function actionIndex(){
        $model = new Rbac();
        return $this->render(‘index‘,[‘model‘=>$model]);
    }
然后创建 views 层
    <?php
/**
 * Created by PhpStorm.
 * User: jinlei
 * Date: 2017/2/16
 * Time: 10:06
 */
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
    ‘id‘ => ‘login-form‘,
    ‘options‘ => [‘class‘ => ‘form-horizontal‘],
    ‘action‘=>‘?r=rbac/power‘,
    ‘method‘=>‘post‘,
]) ?>
    <?= $form->field($model, ‘power‘) ?>
    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton(‘添加权限‘, [‘class‘ => ‘btn btn-primary‘]) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>
然后在控制器里把权限入库 
 public function actionPower()
    {
        $item = \Yii::$app->request->post(‘Rbac‘)[‘power‘];
        $auth = Yii::$app->authManager;
        $createPost = $auth->createPermission($item);
        $createPost->description = ‘创建了 ‘ . $item . ‘ 权限‘;
        $auth->add($createPost);
       return $this->redirect(‘?r=rbac/role‘);
    }
接下来  创建角色
首先创建一个添加角色的表单
 //创建一个就角色的表单
    public function actionRole(){
        $model = new Rbac();
        return $this->render(‘role‘,[‘model‘=>$model]);
    }
然后进入view层
<?php
/**
 * Created by PhpStorm.
 * User: jinlei
 * Date: 2017/2/16
 * Time: 13:52
 */
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
    ‘id‘ => ‘login-form‘,
    ‘options‘ => [‘class‘ => ‘form-horizontal‘],
    ‘action‘=>‘?r=rbac/addrole‘,
    ‘method‘=>‘post‘,
]) ?>
<?= $form->field($model, ‘role‘) ?>
    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton(‘添加角色‘, [‘class‘ => ‘btn btn-primary‘]) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>
然后将角色入库
/添加角色入库
    public function actionAddrole(){
        $item = \Yii::$app->request->post(‘Rbac‘)[‘role‘];
        $auth = Yii::$app->authManager;
        $role = $auth->createRole($item);
        $role->description = ‘创建了 ‘ . $item . ‘ 角色‘;
        $auth->add($role);
        return $this->redirect(‘?r=rbac/rp‘);
    }
然后给角色分配权限
 public function actionRp(){
        $model = new Rbac();
        $role =  AuthItem::find()->where(‘type=1‘)->asArray()->all();
        foreach($role as $value){
            $roles[$value[‘name‘]] = $value[‘name‘];
        }
        $power=  AuthItem::find()->where(‘type=2‘)->asArray()->all();
        foreach($power as $value){
            $powers[$value[‘name‘]] = $value[‘name‘];
        }
        return $this->render(‘rp‘,[‘model‘=>$model,‘role‘=>$roles,‘power‘=>$powers]);
    }
然后到views层  进行分配
<?php
/**
 * Created by PhpStorm.
 * User: jinlei
 * Date: 2017/2/16
 * Time: 14:05
 */
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
    ‘id‘ => ‘login-form‘,
    ‘options‘ => [‘class‘ => ‘form-horizontal‘],
    ‘action‘=>‘?r=rbac/empowerment‘,
    ‘method‘=>‘post‘,
]) ?>
<?= $form->field($model, ‘role‘)->checkboxList($role) ?>
<?= $form->field($model, ‘power‘)->checkboxList($power) ?>
    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton(‘提交‘, [‘class‘ => ‘btn btn-primary‘]) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>
然后入库
public function actionEmpowerment(){
        $auth = Yii::$app->authManager;
        $data = \Yii::$app->request->post(‘Rbac‘);
        $role = $data[‘role‘];
        $power = $data[‘power‘];
       foreach($role as $value){
           foreach($power as $v){
               $parent = $auth->createRole($value);
               $child = $auth->createPermission($v);
               //var_dump($child);
               $auth->addChild($parent, $child);
           }
       }
return $this->redirect(‘?r=rbac/fenpei‘);
    }
然后给用户分配角色
 public function actionFenpei(){
        $model = new Rbac();
        $role =  AuthItem::find()->where(‘type=1‘)->asArray()->all();//注意AuthItem,如果你用的自己新建的登录表 需要将它改为  你自己的表名
        foreach($role as $value){
            $roles[$value[‘name‘]] = $value[‘name‘];
        }
        $user=  User::find()->asArray()->all();
        foreach($user as $value){
            $users[$value[‘id‘]] = $value[‘username‘];
        }
        return $this->render(‘fenpei‘,[‘model‘=>$model,‘role‘=>$roles,‘user‘=>$users]);
    }
分配角色的views层
<?php
/**
 * Created by PhpStorm.
 * User: jinlei
 * Date: 2017/2/16
 * Time: 14:05
 */
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
    ‘id‘ => ‘login-form‘,
    ‘options‘ => [‘class‘ => ‘form-horizontal‘],
    ‘action‘=>‘?r=rbac/ur‘,
    ‘method‘=>‘post‘,
]) ?>
<?= $form->field($model, ‘user‘)->checkboxList($user) ?>
<?= $form->field($model, ‘role‘)->checkboxList($role) ?>
    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton(‘提交‘, [‘class‘ => ‘btn btn-primary‘]) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>
将给用户分配的角色入库
 public function actionUr(){
        $auth = Yii::$app->authManager;
        $data = \Yii::$app->request->post(‘Rbac‘);
        $role = $data[‘role‘];
        $power = $data[‘user‘];
        foreach($role as $value) {
            foreach ($power as $v) {
                $reader = $auth->createRole($value);
                $auth->assign($reader, $v);
            }
        }
    }
然后  在你想要使用权限控制的控制器  添加上下面的方法
 //检测权限
    public function beforeAction($action)
    {
        $user_id= Yii::$app->session[‘id‘];
        $action=$_REQUEST[‘r‘];
//这句是查询你所拥有的角色所拥有的权限
        $sql="select child from auth_assignment as a inner join auth_item_child as b on a.item_name=b.parent where user_id=$user_id";
        $ar=\yii::$app->db->createCommand($sql)->queryAll();
        //将所查出来的权限  转化为一维数组(因为下边的条件需要一个一维数组做比对)
        $arr = array_column($ar, ‘child‘);
//        print_r($arr);die;
//条件里所用的函数不懂得可以百度就知道什么意思了
        if(in_array($action, $arr)){
            return true;
        }else{
            throw new \yii\web\UnauthorizedHttpException(‘对不起,您现在还没获此操作的权限‘);
        }
    }
 
这样你就可以实现  依据actionID的权限控制了
yii2 Rbac实例 (做完以下这些 会有些小的报错,相信各位都能解决,大多数都是自己命名空间上的问题)。
原文:http://www.cnblogs.com/sandada/p/6601846.html