1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace backend\controllers;
use common\models\Admin;
use common\models\search\AdminSearch;
use Yii;
/**
* 后台用户控制器
*
* @author libingle
*/
class AdminController extends BaseController
{
/**
* ---------------------------------------
* 用户列表
* ---------------------------------------
*/
public function actionIndex()
{
/* 添加当前位置到cookie供后续操作调用 */
$this->setForward();
$searchModel = new AdminSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* ---------------------------------------
* 添加
* ---------------------------------------
*/
public function actionAdd()
{
$model = new Admin();
if (Yii::$app->request->isPost) {
/* 表单验证 */
$data = Yii::$app->request->post('Admin');
$data = array_map('trim', $data);
$data['reg_time'] = time();
$data['reg_ip'] = ip2long(Yii::$app->request->getUserIP());
$data['last_login_time'] = 0;
$data['last_login_ip'] = ip2long('127.0.0.1');
$data['update_time'] = 0;
$role = Yii::$app->request->post('role', 'administrator');
/* 表单数据加载和验证,具体验证规则在模型rule中配置 */
/* 密码单独验证,否则setPassword后密码肯定符合rule */
if (empty($data['password']) || strlen($data['password']) < 6) {
$this->error('密码为空或小于6字符');
}
if (empty($data['status'])) {
$data['status'] = 0;
}
$model->setAttributes($data);
//判断用户名重复
$count = Admin::find()->where(['username' => $model->username])->count();
if ($count > 0) {
$this->error('用户名已被占用');
}
$model->generateAuthKey();
$model->setPassword($data['password']);
$auth = Yii::$app->authManager;
$roleTemp = $auth->getRole($role);
$model->platform_id = empty($roleTemp->data) ? Yii::$app->user->identity->platform_id : $roleTemp->data;
/* 保存用户数据到数据库 */
if ($model->save()) {
$auth = Yii::$app->authManager;
$role = $auth->getRole($role);
$auth->assign($role, $model->uid);
$this->success('操作成功', $this->getForward());
} else {
$this->error('操作错误');
}
}
return $this->render('add', [
'model' => $model,
'role' => Yii::$app->request->get('role', 'administrator'),
]);
}
/**
* ---------------------------------------
* 用户授权
* ---------------------------------------
*/
public function actionAuth()
{
/* 获取用户信息 */
$uid = Yii::$app->request->get('uid');
if (!is_numeric($uid) || !($model = Admin::findOne($uid))) {
$this->error('操作对象不合法');
}
$auth = Yii::$app->authManager;
if (Yii::$app->request->isPost) {
$data = Yii::$app->request->post();
/* 用户权限组 */
$item_name = $data['param'];
/* 先删除 用户组-用户 记录 */
$auth->revokeAll($uid);
/* 再添加记录 */
$role = $auth->getRole($item_name);
$auth->assign($role, $uid);
$this->success('授权成功!', $this->getForward());
}
$roles = $auth->getRoles();
$group = array_keys($auth->getAssignments($uid));
return $this->render('auth', [
'model' => $model,
'roles' => $roles,
'group' => $group,
]);
}
public function actionEdit()
{
$model = Admin::findOne(Yii::$app->request->get('uid'));
if (Yii::$app->request->isPost) {
/* 表单验证 */
$data = Yii::$app->request->post('Admin');
$model->setAttributes($data);
if (!empty($data['password'])) {
$model->generateAuthKey();
$model->setPassword($data['password']);
}
/* 保存用户数据到数据库 */
if ($model->save()) {
$this->success('操作成功', $this->getForward());
} else {
$this->error('操作错误');
}
}
$model->password = '';
return $this->render('add', [
'model' => $model,
'role' => Yii::$app->request->get('role', 'administrator'),
]);
}
}