PermController.php
8.48 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
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class PermController extends Controller
{
// 获取用户角色
public function getUserRole(Request $request)
{
$uid = $request->user->userId;
$email = $request->user->email;
if ($email == 'admin@ichunt.com') {
return 1;
}
// 根据域名查询系统业务ID
$domain = Config('website.order_url');
$business = DB::table('t_business_config')->where('url', $domain)->first();
if ($business) {
$bid = $business->bid;
// 权限系统配置的管理帐号
$adminAccount = json_decode($business->admin, true);
if (in_array($email, $adminAccount)) {
return 1;
}
// 根据用户ID和业务ID查看角色
$userPerm = DB::table('t_user_perm')->where(['userId' => $uid, 'bid' => $bid])->first();
$role = json_decode($userPerm->roles, true);
if (empty($role)) {
return 0;
} else {
foreach ($role as $v) {
$department = DB::table('t_role_perm')->where(['roleId' => $v, 'bid' => $bid])->first();
if ($department->name == '管理员') {
return 1;
}else if ($department->name == '经理') {
return 2;
} else if ($department->name == '交易员') {
return 3;
} else if ($department->name == '客服') {
return 4;
} else if ($department->name == '测试') {
return 5;
} else if ($department->name == '京东自营') {
return 6;
} else {
return 0;
}
}
}
}
return 0;
}
// 获取所有角色用户集合
public function getRoleUsers(Request $request, $roleName)
{
// 根据域名查询系统业务ID
$domain = Config('website.order_url');
$business = DB::table('t_business_config')->where('url', $domain)->first();
$userId = array();
$roleUsers = array();
if ($business) {
$bid = $business->bid;
$role = DB::table('t_role_perm')->where(['bid' => $bid, 'name' => $roleName])->first();
$roleId = $role->roleId;
$user = DB::select("SELECT * FROM `t_user_perm` WHERE `bid` = $bid AND `roles` REGEXP $roleId");
if ($user) {
foreach ($user as $v) {
$userId[] = $v->userId;
}
if ($userId) {
foreach ($userId as $id) {
$userInfo = DB::table('user_info')->where('userId', $id)->select('userId', 'name', 'status')->first();
// 判断用户是否已离职 4为离职状态
// if ($userInfo->status != 4) {
$roleUsers[] = $userInfo;
// }
continue;
}
}
}
}
return $roleUsers;
}
// 获取权限系统用户的所有权限
public function getUserPerms(Request $request, $uid)
{
// 根据域名查询系统业务ID
$domain = Config('website.order_url');
$business = DB::table('t_business_config')->where('url', $domain)->first();
if ($business) {
$bid = $business->bid;
// 权限系统配置的管理帐号
$adminAccount = json_decode($business->admin, true);
$configs = Config('perm');
// 若是管理员,则返回Config所有权限
if (in_array($request->user->email, $adminAccount)) {
return $configs;
}
// 获取用户权限
$perms = json_decode($this->curlPermApi($uid, $bid), true);
if ($perms['retcode'] == 0) {
$userPerms = $perms['data']['perms'];
$matchPerms = $this->matchUserPerms($configs, $userPerms);
return $matchPerms;
}
}
return false;
}
// 处理用户权限值
public function handlePerms(Request $request, $uid)
{
$perms = $this->getUserPerms($request, $uid);
$userPerms = array();
foreach ($perms as $perm) {
if(is_array($perm)) {
foreach ($perm as $val) {
if (is_array($val)) {
foreach ($val as $v) {
$userPerms[] = $v;
}
}
}
}
}
return $userPerms;
}
/**
* 请求权限系统接口
* @param [Integer] $uid [用户ID]
* @param [Integer] $bid [业务ID]
* @return [Json] 返回权限数据
*/
public function curlPermApi($uid, $bid)
{
$url = Config('website.perm_api').$uid.'/'.$bid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* 匹配用户在菜单的权限
* @param [Array] $config [配置菜单权限]
* @param [Array] $perms [用户所有权限]
* @return [Json]
*/
public function matchUserPerms($configs, $perms)
{
foreach ($configs as $key=>$config) {
if (is_array($config)) {
foreach($config as $k=>$v) {
if (is_array($v)) {
$menu[$key][$k] = array_intersect($v, $perms);
continue;
} else {
if (in_array($v, $perms)) {
$menu[$key][$k] = $v;
}
continue;
}
}
}
continue;
}
return $menu;
}
/**
* 获取权限菜单
* @param [Array] $menus [数据表菜单]
* @return [Array] 返回处理后的菜单
*/
public function getPermMenu(Request $request, $menus)
{
$menuPerm = $this->getUserPerms($request, $request->user->userId);
$menuData = array();
$newMenu = array();
$matchMenu = array();
if ($menuPerm) {
// 处理返回的菜单---简化
foreach ($menuPerm as $key => $val) {
if (is_array($val)) {
// 处理二级菜单内容
foreach ($val as $k => $v) {
if (is_array($v)) {
foreach ($v as $k1 => $v1) {
if (preg_match('/_check$/', $v1)) {
$menuData[$key][] = $k;
continue;
}
}
} else {
if (strpos($v, '_check') !== false) {
$menuData[$key][] = $k;
continue;
}
}
}
}
}
// 匹配菜单
if ($menus) {
foreach ($menus as $k => $v) {
// 遍历简化过的菜单
foreach ($menuData as $pk => $pv) {
// 匹配标题
if ($pk == $v->title) {
// 遍历对应标题下的子项
foreach ($pv as $pk1 => $pv1) {
if (is_array($v->childs) && !empty($v->childs)) {
foreach ($v->childs as $k1 => $v1) {
if ($pv1 == $v1->title) {
$childs[] = $v1;
$object = (object)[
'title' => $v->title,
'href' => $v->href,
'class' => $v->class,
'childs' => $childs,
];
$matchMenu[$k] = $object;
continue;
}
}
} else {
$object = (object)[
'title' => $v->title,
'href' => $v->href,
'class' => $v->class,
'childs' => $v->childs,
];
$matchMenu[$k] = $object;
continue;
}
}
unset($childs); // 清除内容
}
}
}
}
// 重新组合数组
if ($matchMenu) {
foreach ($matchMenu as $v) {
$newMenu[] = $v;
}
}
}
return $newMenu;
}
}