RbacController.class.php
6.4 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
<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class RbacController extends AdminbaseController {
protected $role_model, $auth_access_model;
public function _initialize() {
parent::_initialize();
$this->role_model = D("Common/Role");
}
// 角色管理列表
public function index() {
$data = $this->role_model->order(array("listorder" => "ASC", "id" => "DESC"))->select();
$this->assign("roles", $data);
$this->display();
}
// 添加角色
public function roleadd() {
$this->display();
}
// 添加角色提交
public function roleadd_post() {
if (IS_POST) {
if ($this->role_model->create()!==false) {
if ($this->role_model->add()!==false) {
$this->success("添加角色成功",U("rbac/index"));
} else {
$this->error("添加失败!");
}
} else {
$this->error($this->role_model->getError());
}
}
}
// 删除角色
public function roledelete() {
$id = I("get.id",0,'intval');
if ($id == 1) {
$this->error("超级管理员角色不能被删除!");
}
$role_user_model=M("RoleUser");
$count=$role_user_model->where(array('role_id'=>$id))->count();
if($count>0){
$this->error("该角色已经有用户!");
}else{
$status = $this->role_model->delete($id);
if ($status!==false) {
$this->success("删除成功!", U('Rbac/index'));
} else {
$this->error("删除失败!");
}
}
}
// 编辑角色
public function roleedit() {
$id = I("get.id",0,'intval');
if ($id == 1) {
$this->error("超级管理员角色不能被修改!");
}
$data = $this->role_model->where(array("id" => $id))->find();
if (!$data) {
$this->error("该角色不存在!");
}
$this->assign("data", $data);
$this->display();
}
// 编辑角色提交
public function roleedit_post() {
$id = I("request.id",0,'intval');
if ($id == 1) {
$this->error("超级管理员角色不能被修改!");
}
if (IS_POST) {
if ($this->role_model->create()!==false) {
if ($this->role_model->save()!==false) {
$this->success("修改成功!", U('Rbac/index'));
} else {
$this->error("修改失败!");
}
} else {
$this->error($this->role_model->getError());
}
}
}
// 角色授权
public function authorize() {
$this->auth_access_model = D("Common/AuthAccess");
//角色ID
$roleid = I("get.id",0,'intval');
if (empty($roleid)) {
$this->error("参数错误!");
}
import("Tree");
$menu = new \Tree();
$menu->icon = array('│ ', '├─ ', '└─ ');
$menu->nbsp = ' ';
$result = $this->initMenu();
$newmenus=array();
$priv_data=$this->auth_access_model->where(array("role_id"=>$roleid))->getField("rule_name",true);//获取权限表数据
foreach ($result as $m){
$newmenus[$m['id']]=$m;
}
foreach ($result as $n => $t) {
$result[$n]['checked'] = ($this->_is_checked($t, $roleid, $priv_data)) ? ' checked' : '';
$result[$n]['level'] = $this->_get_level($t['id'], $newmenus);
$result[$n]['style'] = empty($t['parentid']) ? '' : 'display:none;';
$result[$n]['parentid_node'] = ($t['parentid']) ? ' class="child-of-node-' . $t['parentid'] . '"' : '';
}
$str = "<tr id='node-\$id' \$parentid_node 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>";
$menu->init($result);
$categorys = $menu->get_tree(0, $str);
$this->assign("categorys", $categorys);
$this->assign("roleid", $roleid);
$this->display();
}
// 角色授权提交
public function authorize_post() {
$this->auth_access_model = D("Common/AuthAccess");
if (IS_POST) {
$roleid = I("post.roleid",0,'intval');
if(!$roleid){
$this->error("需要授权的角色不存在!");
}
if (is_array($_POST['menuid']) && count($_POST['menuid'])>0) {
$menu_model=M("Menu");
$auth_rule_model=M("AuthRule");
$this->auth_access_model->where(array("role_id"=>$roleid,'type'=>'admin_url'))->delete();
foreach ($_POST['menuid'] as $menuid) {
$menu=$menu_model->where(array("id"=>$menuid))->field("app,model,action")->find();
if($menu){
$app=$menu['app'];
$model=$menu['model'];
$action=$menu['action'];
$name=strtolower("$app/$model/$action");
$this->auth_access_model->add(array("role_id"=>$roleid,"rule_name"=>$name,'type'=>'admin_url'));
}
}
$this->success("授权成功!", U("Rbac/index"));
}else{
//当没有数据时,清除当前角色授权
$this->auth_access_model->where(array("role_id" => $roleid))->delete();
$this->error("没有接收到数据,执行清除授权成功!");
}
}
}
/**
* 检查指定菜单是否有权限
* @param array $menu menu表中数组
* @param int $roleid 需要检查的角色ID
*/
private function _is_checked($menu, $roleid, $priv_data) {
$app=$menu['app'];
$model=$menu['model'];
$action=$menu['action'];
$name=strtolower("$app/$model/$action");
if($priv_data){
if (in_array($name, $priv_data)) {
return true;
} else {
return false;
}
}else{
return false;
}
}
/**
* 获取菜单深度
* @param $id
* @param $array
* @param $i
*/
protected function _get_level($id, $array = array(), $i = 0) {
if ($array[$id]['parentid']==0 || empty($array[$array[$id]['parentid']]) || $array[$id]['parentid']==$id){
return $i;
}else{
$i++;
return $this->_get_level($array[$id]['parentid'],$array,$i);
}
}
//角色成员管理
public function member(){
//TODO 添加角色成员管理
}
}