Admin.php
4.2 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
<?php
namespace app\admin\controller\auth;
use app\common\controller\Backend;
use fast\Random;
use fast\Tree;
/**
* 管理员管理
*
* @icon fa fa-users
* @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
*/
class Admin extends Backend
{
protected $model = null;
//当前登录管理员所有子节点组别
protected $childrenIds = [];
public function _initialize()
{
parent::_initialize();
$this->model = model('Admin');
$groups = $this->auth->getGroups();
// 取出所有分组
$grouplist = model('AuthGroup')->all(['status' => 'normal']);
$objlist = [];
foreach ($groups as $K => $v)
{
// 取出包含自己的所有子节点
$childrenlist = Tree::instance()->init($grouplist)->getChildren($v['id'], TRUE);
$obj = Tree::instance()->init($childrenlist)->getTreeArray($v['pid']);
$objlist = array_merge($objlist, Tree::instance()->getTreeList($obj));
}
$groupdata = [];
foreach ($objlist as $k => $v)
{
$groupdata[$v['id']] = $v['name'];
}
$this->childrenIds = array_keys($groupdata);
$this->view->assign('groupdata', $groupdata);
}
/**
* 添加
*/
public function add()
{
if ($this->request->isPost())
{
$this->code = -1;
$params = $this->request->post("row/a");
if ($params)
{
$params['salt'] = Random::basic(4);
$params['password'] = md5(md5($params['password']) . $params['salt']);
$admin = $this->model->create($params);
$group = $this->request->post("group/a");
//过滤不允许的组别,避免越权
$group = array_intersect($this->childrenIds, $group);
$dataset = [];
foreach ($group as $value)
{
$dataset[] = ['uid' => $admin->id, 'group_id' => $value];
}
model('AuthGroupAccess')->saveAll($dataset);
$this->code = 1;
}
return;
}
return $this->view->fetch();
}
/**
* 编辑
*/
public function edit($ids = NULL)
{
$row = $this->model->get(['id' => $ids]);
if (!$row)
$this->error(__('No Results were found'));
if ($this->request->isPost())
{
$this->code = -1;
$params = $this->request->post("row/a");
if ($params)
{
if ($params['password'])
{
$params['salt'] = Random::basic(4);
$params['password'] = md5(md5($params['password']) . $params['salt']);
}
$row->save($params);
// 先移除所有权限
model('AuthGroupAccess')->where('uid', $row->id)->delete();
$group = $this->request->post("group/a");
// 过滤不允许的组别,避免越权
$group = array_intersect($this->childrenIds, $group);
$dataset = [];
foreach ($group as $value)
{
$dataset[] = ['uid' => $row->id, 'group_id' => $value];
}
model('AuthGroupAccess')->saveAll($dataset);
$this->code = 1;
}
return;
}
$grouplist = $this->auth->getGroups($row['id']);
$groupids = [];
foreach ($grouplist as $k => $v)
{
$groupids[] = $v['id'];
}
$this->view->assign("row", $row);
$this->view->assign("groupids", $groupids);
return $this->view->fetch();
}
/**
* 删除
*/
public function del($ids = "")
{
$this->code = -1;
if ($ids)
{
$count = $this->model->where('id', 'in', $ids)->delete();
if ($count)
{
model('AuthGroupAccess')->where('uid', 'in', $ids)->delete();
$this->code = 1;
}
}
return;
}
}