获得更好的yii2 rbac扩展

时间:2016-01-23 19:26:30

标签: yii2

使用yii2我可以获得一个扩展,它具有用于管理基于角色的用户访问的物理接口(R.B.A.C.)。

I have tried using 
mdmsoft, dektrium, yii rbac plus but none has any explanation of how 

设置实体

1 个答案:

答案 0 :(得分:1)

我创建了一个物理接口,用于管理基于角色的用户,只需使用基于Yii2提供的defualt RBAC模型提供的表格的gii,并扩展相关的操作,为插入,角色,项目和item_child插入适当的值。 ..

例如:for create assignment

public function actionCreate()
{
    $model = new AuthAssignment();
    $auth = Yii::$app->authManager;

    if ($model->load(Yii::$app->request->post()) ) {

        $auth->assign($auth->getRole($model->item_name), $model->user_id);
        return $this->redirect(['view', 'item_name' => $model->item_name, 'user_id' => $model->user_id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

和创建项目

public function actionCreate()
{
    $model = new AuthItem();
    $auth = Yii::$app->authManager;


    if ($model->load(Yii::$app->request->post())) {
        switch ($model->type) {
            case AuthItem::TYPE_ROLE :   // 1 = TYPE_ROLE
                $role = $auth->createRole($model->name);
                $role->data         = $model->data;
                //$role->ruleName     = $model->rule_name;
                $role->description  = $model->description;
                //$role->type         = $model->type;
                $auth->add($role);
                break;                    
             case AuthItem::TYPE_PERMISSION :  // 2 = TYPE_PERMISSION
                $permission  = $auth->createPermission($model->name);
                $permission->data         = $model->data;
                //$permission->ruleName     = $model->rule_name;
                $permission->description  = $model->description;
                //$permission->type         = $model->type;
                $auth->add($permission);
                break;              
            default:
                break;
        }
        return $this->redirect(['view', 'id' => $model->name]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}