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
<?php
namespace backend\controllers;
use common\models\Admin;
use common\models\search\AdminSearch;
use common\core\Exception;
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['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;
/* 表单数据加载和验证,具体验证规则在模型rule中配置 */
/* 密码单独验证,否则setPassword后密码肯定符合rule */
if (empty($data['password']) || strlen($data['password']) < 6) {
$this->error('密码为空或小于6字符');
}
$model->setAttributes($data);
$model->generateAuthKey();
$model->setPassword($data['password']);
/* 保存用户数据到数据库 */
if ($model->save()) {
$this->success('操作成功', $this->getForward());
} else {
$this->error('操作错误');
}
}
return $this->render('add', [
'model' => $model,
]);
}
/**
* ---------------------------------------
* 用户授权
* ---------------------------------------
*/
// 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();
//
// //更新Fxee权限
// try {
// $ret = FxeeBusiness::updateMemberGroup($uid,isset($data['param_fxee']) ? $data['param_fxee'] : null);
// } catch (Exception $e) {
// $ret = $e->getMessage();
// }
//
// /* 用户权限组 */
// $item_name = $data['param'];
//
// /* 先删除 用户组-用户 记录 */
// $auth->revokeAll($uid);
// /* 再添加记录 */
// $role = $auth->getRole($item_name);
// $auth->assign($role, $uid);
//
// //fxee用户权限操作
//
// if ($ret == true) {
// $this->success('授权成功!', $this->getForward());
// } else if (is_string($ret)) {
// $this->error($ret);
// }
// exit;
// }
//
// $roles = $auth->getRoles();
// $group = array_keys($auth->getAssignments($uid));
//
// $one = Admin::findOne(['uid' => $uid]);
// if (isset($one['bind_uid']) && is_numeric($one['bind_uid'])) {
// $fxeeRoles = Member::getRoles();
// $fxeeGroup = Member::find()->select('group')->where(['id' => $one['bind_uid']])->scalar();
// } else {
// $fxeeRoles = $fxeeGroup = null;
// }
//
// return $this->render('auth', [
// 'model' => $model,
// 'roles' => $roles,
// 'group' => $group,
// 'fxee_roles' => $fxeeRoles,
// 'fxee_group' => $fxeeGroup,
// ]);
// }
}