Skip to content

Commit 17e16e0

Browse files
committed
V1.10
完成RBAC功能
1 parent 0da2d30 commit 17e16e0

File tree

9 files changed

+847
-2
lines changed

9 files changed

+847
-2
lines changed

backend/controllers/AdminuserController.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use yii\filters\VerbFilter;
1111
use backend\models\SignupForm;
1212
use backend\models\ResetpwdForm;
13+
use common\models\AuthItem;
14+
use common\models\AuthAssignment;
1315

1416
/**
1517
* AdminuserController implements the CRUD actions for Adminuser model.
@@ -147,5 +149,54 @@ public function actionResetpwd($id)
147149

148150
}
149151

152+
public function actionPrivilege($id)
153+
{
154+
//step1. 找出所有权限,提供给checkboxlist
155+
$allPrivileges = AuthItem::find()->select(['name','description'])
156+
->where(['type'=>1])->orderBy('description')->all();
157+
158+
foreach ($allPrivileges as $pri)
159+
{
160+
$allPrivilegesArray[$pri->name]=$pri->description;
161+
}
162+
//step2. 当前用户的权限
163+
164+
$AuthAssignments=AuthAssignment::find()->select(['item_name'])
165+
->where(['user_id'=>$id])->orderBy('item_name')->all();
166+
167+
$AuthAssignmentsArray = array();
168+
169+
foreach ($AuthAssignments as $AuthAssignment)
170+
{
171+
array_push($AuthAssignmentsArray,$AuthAssignment->item_name);
172+
}
173+
174+
//step3. 从表单提交的数据,来更新AuthAssignment表,从而用户的角色发生变化
175+
if(isset($_POST['newPri']))
176+
{
177+
AuthAssignment::deleteAll('user_id=:id',[':id'=>$id]);
150178

179+
$newPri = $_POST['newPri'];
180+
181+
$arrlength = count($newPri);
182+
183+
for($x=0;$x<$arrlength;$x++)
184+
{
185+
$aPri = new AuthAssignment();
186+
$aPri->item_name = $newPri[$x];
187+
$aPri->user_id = $id;
188+
$aPri->created_at = time();
189+
190+
$aPri->save();
191+
}
192+
return $this->redirect(['index']);
193+
}
194+
195+
//step4. 渲染checkBoxList表单
196+
197+
return $this->render('privilege',['id'=>$id,'AuthAssignmentArray'=>$AuthAssignmentsArray,
198+
'allPrivilegesArray'=>$allPrivilegesArray]);
199+
200+
}
201+
151202
}

backend/controllers/PostController.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use yii\web\Controller;
99
use yii\web\NotFoundHttpException;
1010
use yii\filters\VerbFilter;
11+
use yii\filters\AccessControl;
1112

1213
/**
1314
* PostController implements the CRUD actions for Post model.
@@ -25,7 +26,24 @@ public function behaviors()
2526
'actions' => [
2627
'delete' => ['POST'],
2728
],
28-
],
29+
],
30+
31+
'access' =>[
32+
'class' => AccessControl::className(),
33+
'rules' =>
34+
[
35+
[
36+
'actions' => ['index', 'view'],
37+
'allow' => true,
38+
'roles' => ['?'],
39+
],
40+
[
41+
'actions' => ['view', 'index', 'create','update'],
42+
'allow' => true,
43+
'roles' => ['@'],
44+
],
45+
],
46+
],
2947
];
3048
}
3149

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
use yii\widgets\ActiveForm;
5+
use yii\helpers\ArrayHelper;
6+
use common\models\Adminuser;
7+
8+
/* @var $this yii\web\View */
9+
/* @var $model common\models\Adminuser */
10+
11+
$model = Adminuser::findOne($id);
12+
13+
$this->title = '权限设置: ' . $model->username;
14+
$this->params['breadcrumbs'][] = ['label' => '管理员', 'url' => ['index']];
15+
$this->params['breadcrumbs'][] = ['label' => $model->username, 'url' => ['view', 'id' => $id]];
16+
$this->params['breadcrumbs'][] = '权限设置';
17+
?>
18+
19+
<div class="adminuser-update">
20+
21+
<h1><?= Html::encode($this->title) ?></h1>
22+
23+
24+
<div class="adminuser-privilege-form">
25+
26+
<?php $form = ActiveForm::begin(); ?>
27+
28+
<?= Html::checkboxList('newPri',$AuthAssignmentArray,$allPrivilegesArray);?>
29+
30+
<div class="form-group">
31+
<?= Html::submitButton('设置') ?>
32+
</div>
33+
34+
<?php ActiveForm::end(); ?>
35+
36+
</div>
37+
38+
39+
40+
</div>
41+
42+
43+
44+

common/config/main.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
'cache' => [
66
'class' => 'yii\caching\FileCache',
77
],
8+
'authManager' => [
9+
'class' =>'yii\rbac\DbManager',
10+
],
811
],
912
];

common/models/AuthAssignment.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace common\models;
4+
5+
use Yii;
6+
7+
/**
8+
* This is the model class for table "auth_assignment".
9+
*
10+
* @property string $item_name
11+
* @property string $user_id
12+
* @property integer $created_at
13+
*
14+
* @property AuthItem $itemName
15+
*/
16+
class AuthAssignment extends \yii\db\ActiveRecord
17+
{
18+
/**
19+
* @inheritdoc
20+
*/
21+
public static function tableName()
22+
{
23+
return 'auth_assignment';
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function rules()
30+
{
31+
return [
32+
[['item_name', 'user_id'], 'required'],
33+
[['created_at'], 'integer'],
34+
[['item_name', 'user_id'], 'string', 'max' => 64],
35+
[['item_name'], 'exist', 'skipOnError' => true, 'targetClass' => AuthItem::className(), 'targetAttribute' => ['item_name' => 'name']],
36+
];
37+
}
38+
39+
/**
40+
* @inheritdoc
41+
*/
42+
public function attributeLabels()
43+
{
44+
return [
45+
'item_name' => 'Item Name',
46+
'user_id' => 'User ID',
47+
'created_at' => 'Created At',
48+
];
49+
}
50+
51+
/**
52+
* @return \yii\db\ActiveQuery
53+
*/
54+
public function getItemName()
55+
{
56+
return $this->hasOne(AuthItem::className(), ['name' => 'item_name']);
57+
}
58+
}

common/models/AuthItem.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace common\models;
4+
5+
use Yii;
6+
7+
/**
8+
* This is the model class for table "auth_item".
9+
*
10+
* @property string $name
11+
* @property integer $type
12+
* @property string $description
13+
* @property string $rule_name
14+
* @property string $data
15+
* @property integer $created_at
16+
* @property integer $updated_at
17+
*
18+
* @property AuthAssignment[] $authAssignments
19+
* @property AuthRule $ruleName
20+
* @property AuthItemChild[] $authItemChildren
21+
* @property AuthItemChild[] $authItemChildren0
22+
* @property AuthItem[] $children
23+
* @property AuthItem[] $parents
24+
*/
25+
class AuthItem extends \yii\db\ActiveRecord
26+
{
27+
/**
28+
* @inheritdoc
29+
*/
30+
public static function tableName()
31+
{
32+
return 'auth_item';
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function rules()
39+
{
40+
return [
41+
[['name', 'type'], 'required'],
42+
[['type', 'created_at', 'updated_at'], 'integer'],
43+
[['description', 'data'], 'string'],
44+
[['name', 'rule_name'], 'string', 'max' => 64],
45+
[['rule_name'], 'exist', 'skipOnError' => true, 'targetClass' => AuthRule::className(), 'targetAttribute' => ['rule_name' => 'name']],
46+
];
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function attributeLabels()
53+
{
54+
return [
55+
'name' => 'Name',
56+
'type' => 'Type',
57+
'description' => 'Description',
58+
'rule_name' => 'Rule Name',
59+
'data' => 'Data',
60+
'created_at' => 'Created At',
61+
'updated_at' => 'Updated At',
62+
];
63+
}
64+
65+
/**
66+
* @return \yii\db\ActiveQuery
67+
*/
68+
public function getAuthAssignments()
69+
{
70+
return $this->hasMany(AuthAssignment::className(), ['item_name' => 'name']);
71+
}
72+
73+
/**
74+
* @return \yii\db\ActiveQuery
75+
*/
76+
public function getRuleName()
77+
{
78+
return $this->hasOne(AuthRule::className(), ['name' => 'rule_name']);
79+
}
80+
81+
/**
82+
* @return \yii\db\ActiveQuery
83+
*/
84+
public function getAuthItemChildren()
85+
{
86+
return $this->hasMany(AuthItemChild::className(), ['parent' => 'name']);
87+
}
88+
89+
/**
90+
* @return \yii\db\ActiveQuery
91+
*/
92+
public function getAuthItemChildren0()
93+
{
94+
return $this->hasMany(AuthItemChild::className(), ['child' => 'name']);
95+
}
96+
97+
/**
98+
* @return \yii\db\ActiveQuery
99+
*/
100+
public function getChildren()
101+
{
102+
return $this->hasMany(AuthItem::className(), ['name' => 'child'])->viaTable('auth_item_child', ['parent' => 'name']);
103+
}
104+
105+
/**
106+
* @return \yii\db\ActiveQuery
107+
*/
108+
public function getParents()
109+
{
110+
return $this->hasMany(AuthItem::className(), ['name' => 'parent'])->viaTable('auth_item_child', ['child' => 'name']);
111+
}
112+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
namespace console\controllers;
3+
4+
use Yii;
5+
use yii\console\Controller;
6+
7+
class RbacController extends Controller
8+
{
9+
public function actionInit()
10+
{
11+
$auth = Yii::$app->authManager;
12+
13+
// 添加 "createPost" 权限
14+
$createPost = $auth->createPermission('createPost');
15+
$createPost->description = '新增文章';
16+
$auth->add($createPost);
17+
18+
// 添加 "updatePost" 权限
19+
$updatePost = $auth->createPermission('updatePost');
20+
$updatePost->description = '修改文章';
21+
$auth->add($updatePost);
22+
23+
// 添加 "deletePost" 权限
24+
$deletePost = $auth->createPermission('deletePost');
25+
$deletePost->description = '删除文章';
26+
$auth->add($deletePost);
27+
28+
// 添加 "approveComment" 权限
29+
$approveComment = $auth->createPermission('approveComment');
30+
$approveComment->description = '审核评论';
31+
$auth->add($approveComment);
32+
33+
34+
// 添加 "postadmin" 角色并赋予 "updatePost" “deletePost” “createPost”
35+
$postAdmin = $auth->createRole('postAdmin');
36+
$postAdmin->description = '文章管理员';
37+
$auth->add($postAdmin);
38+
$auth->addChild($postAdmin, $updatePost);
39+
$auth->addChild($postAdmin, $createPost);
40+
$auth->addChild($postAdmin, $deletePost);
41+
42+
// 添加 "postOperator" 角色并赋予 “deletePost”
43+
$postOperator = $auth->createRole('postOperator');
44+
$postOperator->description = '文章操作员';
45+
$auth->add($postOperator);
46+
$auth->addChild($postOperator, $deletePost);
47+
48+
// 添加 "commentAuditor" 角色并赋予 “approveComment”
49+
$commentAuditor = $auth->createRole('commentAuditor');
50+
$commentAuditor->description = '评论审核员';
51+
$auth->add($commentAuditor);
52+
$auth->addChild($commentAuditor, $approveComment);
53+
54+
// 添加 "admin" 角色并赋予所有其他角色拥有的权限
55+
$admin = $auth->createRole('admin');
56+
$commentAuditor->description = '系统管理员';
57+
$auth->add($admin);
58+
$auth->addChild($admin, $postAdmin);
59+
$auth->addChild($admin, $commentAuditor);
60+
61+
62+
63+
// 为用户指派角色。其中 1 和 2 是由 IdentityInterface::getId() 返回的id (译者注:user表的id)
64+
// 通常在你的 User 模型中实现这个函数。
65+
$auth->assign($admin, 1);
66+
$auth->assign($postAdmin, 2);
67+
$auth->assign($postOperator, 3);
68+
$auth->assign($commentAuditor, 4);
69+
}
70+
}

0 commit comments

Comments
 (0)