113 lines
4.1 KiB
PHP
Raw Normal View History

2021-01-30 20:59:12 +08:00
<?php
2021-07-26 17:41:59 +08:00
/**
* @copyright Copyright (c) 2021 勾股工作室
2021-11-24 17:17:29 +08:00
* @license https://opensource.org/licenses/Apache-2.0
2021-07-26 17:41:59 +08:00
* @link https://www.gougucms.com
*/
2021-01-30 20:59:12 +08:00
declare (strict_types = 1);
namespace app\admin\middleware;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Session;
class Auth
{
public function handle($request, \Closure $next)
{
//获取模块名称
$controller = app('http')->getName();
2023-01-18 17:24:30 +08:00
// $pathInfo = str_replace('.' . $request->ext(), '', $request->pathInfo());
$pathInfo = str_replace('.html', '', $request->pathInfo());
2021-01-30 20:59:12 +08:00
$action = explode('/', $pathInfo)[0];
2021-06-15 09:30:04 +08:00
//var_dump($pathInfo);exit;
2021-07-26 17:41:59 +08:00
if ($pathInfo == '' || $action == '') {
redirect('/admin/index/index.html')->send();
exit;
}
2021-01-30 20:59:12 +08:00
//验证用户登录
if ($action !== 'login') {
$session_admin = get_config('app.session_admin');
if (!Session::has($session_admin)) {
2021-07-26 17:41:59 +08:00
if ($request->isAjax()) {
return to_assign(404, '请先登录');
} else {
redirect('/admin/login/index.html')->send();
exit;
}
2021-01-30 20:59:12 +08:00
}
2022-06-01 00:00:42 +08:00
$uid = Session::get($session_admin)['id'];
2021-01-30 20:59:12 +08:00
// 验证用户访问权限
if ($action !== 'index' && $action !== 'api') {
2022-06-01 00:00:42 +08:00
if (!$this->checkAuth($controller, $pathInfo, $action, $uid)) {
2023-01-18 17:24:30 +08:00
// $pathUrl = $controller . '/' . $pathInfo;
// halt($pathUrl,Cache::get('RulesSrc' . $uid));
2021-07-26 17:41:59 +08:00
if ($request->isAjax()) {
2021-02-19 19:07:41 +08:00
return to_assign(202, '你没有权限,请联系超级管理员!');
2021-07-26 17:41:59 +08:00
} else {
2021-02-19 19:07:41 +08:00
echo '<div style="text-align:center;color:red;margin-top:20%;">您没有权限,请联系超级管理员</div>';exit;
}
2021-01-30 20:59:12 +08:00
}
}
}
return $next($request);
}
/**
* 验证用户访问权限
* @DateTime 2020-12-21
* @param string $controller 当前访问控制器
* @param string $action 当前访问方法
* @param string $uid 当前用户id
* @return [type]
*/
protected function checkAuth($controller, $pathInfo, $action, $uid)
{
2021-02-23 13:52:07 +08:00
//Cache::delete('RulesSrc' . $uid);
2021-07-26 17:41:59 +08:00
if (!Cache::get('RulesSrc' . $uid) || !Cache::get('RulesSrc0')) {
//用户所在权限组及所拥有的权限
// 执行查询
$user_groups = Db::name('AdminGroupAccess')
->alias('a')
->join("AdminGroup g", "a.group_id=g.id", 'LEFT')
->where("a.uid='{$uid}' and g.status='1'")
->select()
->toArray();
$groups = $user_groups ?: [];
$ids = []; //保存用户所属用户组设置的所有权限规则id
foreach ($groups as $g) {
$ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
}
$ids = array_unique($ids);
//读取所有权限规则
$rules_all = Db::name('AdminRule')->field('src')->select();
//读取用户组所有权限规则
$rules = Db::name('AdminRule')->where('id', 'in', $ids)->field('src')->select();
//循环规则,判断结果。
$auth_list_all = [];
$auth_list = [];
foreach ($rules_all as $rule_all) {
$auth_list_all[] = strtolower($rule_all['src']);
}
foreach ($rules as $rule) {
$auth_list[] = strtolower($rule['src']);
}
//规则列表结果保存到Cache
Cache::tag('adminRules')->set('RulesSrc0', $auth_list_all, 36000);
Cache::tag('adminRules')->set('RulesSrc' . $uid, $auth_list, 36000);
} else {
$auth_list_all = Cache::get('RulesSrc0');
$auth_list = Cache::get('RulesSrc' . $uid);
}
$pathUrl = $controller . '/' . $pathInfo;
2021-07-28 15:58:43 +08:00
if (!in_array($pathUrl, $auth_list)) {
2021-07-26 17:41:59 +08:00
return false;
} else {
return true;
}
2021-01-30 20:59:12 +08:00
}
}