refactor:去除菜单权限规则类的静态变量缓存

This commit is contained in:
妙码生花 2024-06-25 00:33:16 +08:00
parent 6fd70972a7
commit 0e4bcd0e1e

View File

@ -10,11 +10,6 @@ use think\facade\Db;
*/ */
class Auth class Auth
{ {
/**
* 用户有权限的规则节点
*/
protected static array $rules = [];
/** /**
* 默认配置 * 默认配置
* @var array|string[] * @var array|string[]
@ -176,27 +171,25 @@ class Auth
$ids = $this->getRuleIds($uid); $ids = $this->getRuleIds($uid);
if (empty($ids)) return []; if (empty($ids)) return [];
$idsCacheKey = md5(implode('', $ids) . $this->config['auth_rule']);
if (empty(self::$rules[$idsCacheKey])) {
$where = []; $where = [];
$where[] = ['status', '=', '1']; $where[] = ['status', '=', '1'];
// 如果没有 * 则只获取用户拥有的规则 // 如果没有 * 则只获取用户拥有的规则
if (!in_array('*', $ids)) { if (!in_array('*', $ids)) {
$where[] = ['id', 'in', $ids]; $where[] = ['id', 'in', $ids];
} }
self::$rules[$idsCacheKey] = Db::name($this->config['auth_rule']) $rules = Db::name($this->config['auth_rule'])
->withoutField(['remark', 'status', 'weigh', 'update_time', 'create_time']) ->withoutField(['remark', 'status', 'weigh', 'update_time', 'create_time'])
->where($where) ->where($where)
->order('weigh desc,id asc') ->order('weigh desc,id asc')
->select() ->select()
->toArray(); ->toArray();
foreach ($rules as $key => $rule) {
foreach (self::$rules[$idsCacheKey] as $key => $rule) { if (!empty($rule['keepalive'])) {
if (!empty($rule['keepalive'])) self::$rules[$idsCacheKey][$key]['keepalive'] = $rule['name']; $rules[$key]['keepalive'] = $rule['name'];
} }
} }
return self::$rules[$idsCacheKey]; return $rules;
} }
/** /**
@ -225,12 +218,6 @@ class Auth
public function getGroups(int $uid): array public function getGroups(int $uid): array
{ {
$dbName = $this->config['auth_group_access'] ?: 'user'; $dbName = $this->config['auth_group_access'] ?: 'user';
static $groups = [];
if (isset($groups[$dbName][$uid])) {
return $groups[$dbName][$uid];
}
if ($this->config['auth_group_access']) { if ($this->config['auth_group_access']) {
$userGroups = Db::name($dbName) $userGroups = Db::name($dbName)
->alias('aga') ->alias('aga')
@ -249,7 +236,6 @@ class Auth
->toArray(); ->toArray();
} }
$groups[$dbName][$uid] = $userGroups ?: []; return $userGroups;
return $groups[$dbName][$uid];
} }
} }