-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAuth.php
414 lines (349 loc) · 11.9 KB
/
Auth.php
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
// +----------------------------------------------------------------------
// | [ Only to facilitate the creation of it]
// +----------------------------------------------------------------------
// | Personal development
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: tekintian <[email protected]>
// +----------------------------------------------------------------------
namespace tp5auth\auth;
defined('VIEW_PATH') or define('VIEW_PATH', __DIR__ . DS.'view'. DS);
use think\Cache;
use think\Config;
use think\Loader;
use think\Request;
use think\Session;
use tp5auth\auth\controller\Rbac;
use tp5auth\auth\model\ActionLog;
use tp5auth\auth\model\AuthAccess;
use tp5auth\auth\model\AuthRoleUser;
use tp5auth\auth\model\Menu;
class Auth
{
const PATH = __DIR__;
public $log = true;
public $noNeedCheckRules = []; //不需要检查的路由规则
public function __construct()
{
$this->request = Request::instance();
$this->param = $this->request->param();
$this->module = $this->request->module();
$this->controller = $this->request->controller();
$this->action = $this->request->action();
}
/**
* 加载控制器方法
* @access public
* @param string $name 方法名
* @return mixed
*/
public function autoload($name){
$controller = new Rbac($this->request);
if(strtolower($this->controller) == 'auth' && method_exists($controller,$name)){
return call_user_func([$controller, $name]);
}
return false;
}
/**
* 权限认证
* @access public
* @return mixed
*/
public function auth(){
$uid = self::sessionGet('user.uid');
$controller = Loader::parseName($this->controller,1); //字符串命名风格转换
$rule = strtolower("{$this->module}/{$controller}/{$this->action}");
//如果用户角色是1,则无需判断
if(empty($uid)){
return false;
}
if($uid == 1){
self::actionLog($rule);
return true;
}
//无需认证
$noNeedCheckRules = array_merge($this->noNeedCheckRules,[$this->module.'/auth/openfile',$this->module.'/auth/cache']);
if( !in_array($rule,$noNeedCheckRules) ){
return self::authCheck($rule,'or');
}else{
return true;
}
}
/**
* 菜单权限检查
* @access public
* @return array
*/
public static function menuCheck(){
$uid = self::sessionGet('user.uid');
if(empty($uid)){
return false;
}
$where['status'] = 1;
if($uid != 1){
$authMenu = self::authMenu('',false);
if(array($authMenu)){ //授权菜单ID
$where['id']=['in',array_keys($authMenu)];
}
}
$menu = Menu::where($where)->order(["list_order" => "asc",'id'=>'asc'])->column('*','id');
return $menu;
}
/**
* 行为日志检查
* @access public
* @param string $rule 日志规则
* @return array
*/
private function actionLog($rule){
//是否需要打开 行为日志检查
if($this->log === false){
return true;
}
$logMenu = Cache::get('logMenu');
if(empty($logMenu)){ //缓存日志24小时
$logMenu = Menu::actionLogMenu();
Cache::set('logMenu',$logMenu,86400);
}
$menu = isset($logMenu[$rule])?$logMenu[$rule]:'';
$log = [];
if(empty($menu)){
return true;
}
//子集行为日志菜单匹配
if(isset($menu['child'])){
foreach($menu['child'] as $v){
if(!empty($v['rule_param'])){
$condition = '';
$command = preg_replace('/\{(\w*?)\}/', '$this->param[\'\\1\']', $v['rule_param']);
@(eval('$condition=(' . $command . ');'));
if ($condition and $v['request'] == $this->request->method()) {
$log = $v;
}
}
}
}
//父集行为日志菜单匹配
if(empty($log)){
if($menu['request'] == $this->request->method()){
$log = $menu;
}
}
if(!empty($log)){
return self::createLog($log['log_rule'],$log['name']);
}
return true;
}
/**
* 创建行为日志
* @param string $logrule 行为日志规则
* @param string $title 标题
* @param int $uid 执行者ID
* @return array
*/
public function createLog($logrule,$title){
$uid = self::sessionGet('user.uid');
$param = $this->param;
$condition = '';
$command = preg_replace('/\{(\w*?)\}/', '{$param[\'\\1\']}', $logrule);
@(eval('$condition=("' . $command . '");'));
$data = [
'action_ip' => ip2long($this->request->ip()),
'username' => self::sessionGet('user.nickname'),
'create_time' => time(),
'log_url' => '/'.$this->request->pathinfo(),
'log' => $condition,
'user_id' => $uid,
'title' => $title
];
return ActionLog::create($data);
}
/**
* 检查路由权限
* @access public static
* @param string $path 路由
* @param array $param 参数
* @return bool
*/
public static function checkPath($path,$param=[]){
$uid = self::sessionGet('user.uid');
if($uid == 1){
return true;
}
$authMenu = Cache::get('authMenu_'.$uid);
if(!$authMenu){ //存入缓存 授权菜单
$authMenu = self::authMenu();
Cache::set('authMenu_'.$uid,$authMenu,600);
}
$count = count(explode('/',$path));
if($count == 2){
$module = Request::instance()->module();
$path = "$module/$path";
}
$path = strtolower($path);
//是否为超级管理员角色
if($path === true ){
return true;
}else if($path === false){
return false;
}
//验证路由
foreach ($authMenu as $v){
if($v['name'] == $path){
if(empty($v['rule_param'])){ //验证规则为空,表示所有通过
return true;
}else{ //如有验证规则,根据规则验证
$condition = false;
$command = preg_replace('/\{(\w*?)\}/', '$param[\'\\1\']', $v['rule_param']);
@(eval('$condition=(' . $command . ');'));
if($condition){
return true;
}
}
}
}
return false;
}
/**
* 检查权限
* @access protected
* @param string $url 路由
* @param string $relation
* @return mixed
*/
protected function authCheck($url,$relation='or'){
$rule = array($url);
$list = []; //保存验证通过的规则名)
$param = $this->param;
$rules = self::authMenu(["b.name"=>["in",$rule]]);
//是否为超级管理员角色
if($rules === true){
//行为日志
self::actionLog($url);
return true;
}else if($rules === false){
return false;
}
foreach ($rules as $rule){
if (!empty($rule['rule_param'])) { //根据rule_param进行验证
$condition = false;
$command = preg_replace('/\{(\w*?)\}/', '$param[\'\\1\']', $rule['rule_param']);
@(eval('$condition=(' . $command . ');'));
if ($condition) {
$list[] = strtolower($rule['name']);
}
}else{
$list[] = strtolower($rule['name']);
}
}
if ($relation == 'or' and !empty($list)) {
//行为日志
self::actionLog($url);
return true;
}
$diff = array_diff($rule, $list);
if ($relation == 'and' and empty($diff)) {
return true;
}
return false;
}
/**
* 权限访问清单
* @access private
* @param array $where 查询附加条件
* @param bool $default 隐藏的菜单
* @return array
*/
private static function authMenu($where=[],$default = true){
$uid = self::sessionGet('user.uid');
$rule = [];
$roleId = AuthRoleUser::hasWhere('authRule')->where(['a.user_id'=>$uid,'b.status'=>1])->column('role_id');
if(in_array(1,$roleId)){
return true;
}
$roleId = implode(',',$roleId);
//角色权限 or 管理员权限
if($default === true){
$rule = AuthAccess::hasWhere('authRule')->where($where)
->where('(a.type="admin_url" and a.role_id in(:roleId))or(a.type="admin" and a.role_id =:uid)',['roleId'=>$roleId,
'uid'=>$uid]);
}else if($default === false){
$rule = AuthAccess::where($where)
->where('(type="admin_url" and role_id in(:roleId))or(type="admin" and role_id =:uid)',['roleId'=>$roleId,
'uid'=>$uid]);
}
$rule = $rule->column('*','menu_id');
if(empty($rule)){
return false;
}
return $rule;
}
/**
* 检测用户是否登录
* @return mixed
*/
public static function is_login(){
$user = self::sessionGet('user');
if (empty($user)) {
return false;
} else {
return self::sessionGet('user_sign') == self::data_auth_sign($user) ? $user : false;
}
}
/**
* 用户登入
* @access private static
* @param int $uid 用户ID
* @param string $nickname 用户昵称
* @return array
*/
public static function login($uid,$nickname){
if(empty($uid) && empty($nickname)){
return false;
}
$session_prefix = Config::get('tp5auth.session_prefix');
$user = [
'uid' => $uid,
'nickname' => $nickname,
'time' => time()
];
Session::set($session_prefix.'user',$user);
Session::set($session_prefix.'user_sign',self::data_auth_sign($user));
return true;
}
/**
* 注销
* @access private static
* @return bool
*/
public static function logout(){
$session_prefix = Config::get('tp5auth.session_prefix');
Session::delete($session_prefix.'user');
Session::delete($session_prefix.'user_sign');
return true;
}
/**
* 数据签名认证
* @access private static
* @param array $data 被认证的数据
* @return string 签名
*/
private static function data_auth_sign($data) {
$code = http_build_query($data); //url编码并生成query字符串
$sign = sha1($code); //生成签名
return $sign;
}
/**
* 读取session
* @access private static
* @param string $path 被认证的数据
* @return mixed
*/
private static function sessionGet($path =''){
$session_prefix = Config::get('tp5auth.session_prefix');
$user = Session::get($session_prefix.$path);
return $user;
}
}