RbacController.php
10.1 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;
use tree\Tree;
use app\admin\model\AdminMenuModel;
class RbacController extends AdminBaseController
{
/**
* 角色管理列表
* @adminMenu(
* 'name' => '角色管理',
* 'parent' => 'admin/User/default',
* 'display'=> true,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '角色管理',
* 'param' => ''
* )
*/
public function index()
{
$data = Db::name('role')->order(["list_order" => "ASC", "id" => "DESC"])->select();
$this->assign("roles", $data);
return $this->fetch();
}
/**
* 添加角色
* @adminMenu(
* 'name' => '添加角色',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '添加角色',
* 'param' => ''
* )
*/
public function roleAdd()
{
return $this->fetch();
}
/**
* 添加角色提交
* @adminMenu(
* 'name' => '添加角色提交',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '添加角色提交',
* 'param' => ''
* )
*/
public function roleAddPost()
{
if ($this->request->isPost()) {
$data = $this->request->param();
$result = $this->validate($data, 'role');
if ($result !== true) {
// 验证失败 输出错误信息
$this->error($result);
} else {
$result = Db::name('role')->insert($data);
if ($result) {
$this->success("添加角色成功", url("rbac/index"));
} else {
$this->error("添加角色失败");
}
}
}
}
/**
* 编辑角色
* @adminMenu(
* 'name' => '编辑角色',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '编辑角色',
* 'param' => ''
* )
*/
public function roleEdit()
{
$id = $this->request->param("id", 0, 'intval');
if ($id == 1) {
$this->error("超级管理员角色不能被修改!");
}
$data = Db::name('role')->where(["id" => $id])->find();
if (!$data) {
$this->error("该角色不存在!");
}
$this->assign("data", $data);
return $this->fetch();
}
/**
* 编辑角色提交
* @adminMenu(
* 'name' => '编辑角色提交',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '编辑角色提交',
* 'param' => ''
* )
*/
public function roleEditPost()
{
$id = $this->request->param("id", 0, 'intval');
if ($id == 1) {
$this->error("超级管理员角色不能被修改!");
}
if ($this->request->isPost()) {
$data = $this->request->param();
$result = $this->validate($data, 'role');
if ($result !== true) {
// 验证失败 输出错误信息
$this->error($result);
} else {
if (Db::name('role')->update($data) !== false) {
$this->success("保存成功!", url('rbac/index'));
} else {
$this->error("保存失败!");
}
}
}
}
/**
* 删除角色
* @adminMenu(
* 'name' => '删除角色',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '删除角色',
* 'param' => ''
* )
*/
public function roleDelete()
{
$id = $this->request->param("id", 0, 'intval');
if ($id == 1) {
$this->error("超级管理员角色不能被删除!");
}
$count = Db::name('RoleUser')->where(['role_id' => $id])->count();
if ($count > 0) {
$this->error("该角色已经有用户!");
} else {
$status = Db::name('role')->delete($id);
if (!empty($status)) {
$this->success("删除成功!", url('rbac/index'));
} else {
$this->error("删除失败!");
}
}
}
/**
* 设置角色权限
* @adminMenu(
* 'name' => '设置角色权限',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '设置角色权限',
* 'param' => ''
* )
*/
public function authorize()
{
$AuthAccess = Db::name("AuthAccess");
$adminMenuModel = new AdminMenuModel();
//角色ID
$roleId = $this->request->param("id", 0, 'intval');
if (empty($roleId)) {
$this->error("参数错误!");
}
$tree = new Tree();
$tree->icon = ['│ ', '├─ ', '└─ '];
$tree->nbsp = ' ';
$result = $adminMenuModel->menuCache();
$newMenus = [];
$privilegeData = $AuthAccess->where(["role_id" => $roleId])->column("rule_name");//获取权限表数据
foreach ($result as $m) {
$newMenus[$m['id']] = $m;
}
foreach ($result as $n => $t) {
$result[$n]['checked'] = ($this->_isChecked($t, $privilegeData)) ? ' checked' : '';
$result[$n]['level'] = $this->_getLevel($t['id'], $newMenus);
$result[$n]['style'] = empty($t['parent_id']) ? '' : 'display:none;';
$result[$n]['parentIdNode'] = ($t['parent_id']) ? ' class="child-of-node-' . $t['parent_id'] . '"' : '';
}
$str = "<tr id='node-\$id'\$parentIdNode style='\$style'>
<td style='padding-left:30px;'>\$spacer<input type='checkbox' name='menuId[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</td>
</tr>";
$tree->init($result);
$category = $tree->getTree(0, $str);
$this->assign("category", $category);
$this->assign("roleId", $roleId);
return $this->fetch();
}
/**
* 角色授权提交
* @adminMenu(
* 'name' => '角色授权提交',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '角色授权提交',
* 'param' => ''
* )
*/
public function authorizePost()
{
if ($this->request->isPost()) {
$roleId = $this->request->param("roleId", 0, 'intval');
if (!$roleId) {
$this->error("需要授权的角色不存在!");
}
if (is_array($this->request->param('menuId/a')) && count($this->request->param('menuId/a')) > 0) {
Db::name("authAccess")->where(["role_id" => $roleId, 'type' => 'admin_url'])->delete();
foreach ($_POST['menuId'] as $menuId) {
$menu = Db::name("adminMenu")->where(["id" => $menuId])->field("app,controller,action")->find();
if ($menu) {
$app = $menu['app'];
$model = $menu['controller'];
$action = $menu['action'];
$name = strtolower("$app/$model/$action");
Db::name("authAccess")->insert(["role_id" => $roleId, "rule_name" => $name, 'type' => 'admin_url']);
}
}
$this->success("授权成功!");
} else {
//当没有数据时,清除当前角色授权
Db::name("authAccess")->where(["role_id" => $roleId])->delete();
$this->error("没有接收到数据,执行清除授权成功!");
}
}
}
/**
* 检查指定菜单是否有权限
* @param array $menu menu表中数组
* @param $privData
* @return bool
*/
private function _isChecked($menu, $privData)
{
$app = $menu['app'];
$model = $menu['controller'];
$action = $menu['action'];
$name = strtolower("$app/$model/$action");
if ($privData) {
if (in_array($name, $privData)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* 获取菜单深度
* @param $id
* @param array $array
* @param int $i
* @return int
*/
protected function _getLevel($id, $array = [], $i = 0)
{
if ($array[$id]['parent_id'] == 0 || empty($array[$array[$id]['parent_id']]) || $array[$id]['parent_id'] == $id) {
return $i;
} else {
$i++;
return $this->_getLevel($array[$id]['parent_id'], $array, $i);
}
}
//角色成员管理
public function member()
{
//TODO 添加角色成员管理
}
}