Skip to content

Commit

Permalink
refactor:去除菜单权限规则类的静态变量缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
build-admin committed Jun 24, 2024
1 parent 6fd7097 commit 0e4bcd0
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions extend/ba/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
*/
class Auth
{
/**
* 用户有权限的规则节点
*/
protected static array $rules = [];

/**
* 默认配置
* @var array|string[]
Expand Down Expand Up @@ -176,27 +171,25 @@ public function getOriginAuthRules(int $uid): array
$ids = $this->getRuleIds($uid);
if (empty($ids)) return [];

$idsCacheKey = md5(implode('', $ids) . $this->config['auth_rule']);
if (empty(self::$rules[$idsCacheKey])) {
$where = [];
$where[] = ['status', '=', '1'];
// 如果没有 * 则只获取用户拥有的规则
if (!in_array('*', $ids)) {
$where[] = ['id', 'in', $ids];
}
self::$rules[$idsCacheKey] = Db::name($this->config['auth_rule'])
->withoutField(['remark', 'status', 'weigh', 'update_time', 'create_time'])
->where($where)
->order('weigh desc,id asc')
->select()
->toArray();

foreach (self::$rules[$idsCacheKey] as $key => $rule) {
if (!empty($rule['keepalive'])) self::$rules[$idsCacheKey][$key]['keepalive'] = $rule['name'];
$where = [];
$where[] = ['status', '=', '1'];
// 如果没有 * 则只获取用户拥有的规则
if (!in_array('*', $ids)) {
$where[] = ['id', 'in', $ids];
}
$rules = Db::name($this->config['auth_rule'])
->withoutField(['remark', 'status', 'weigh', 'update_time', 'create_time'])
->where($where)
->order('weigh desc,id asc')
->select()
->toArray();
foreach ($rules as $key => $rule) {
if (!empty($rule['keepalive'])) {
$rules[$key]['keepalive'] = $rule['name'];
}
}

return self::$rules[$idsCacheKey];
return $rules;
}

/**
Expand Down Expand Up @@ -225,12 +218,6 @@ public function getRuleIds(int $uid): array
public function getGroups(int $uid): array
{
$dbName = $this->config['auth_group_access'] ?: 'user';

static $groups = [];
if (isset($groups[$dbName][$uid])) {
return $groups[$dbName][$uid];
}

if ($this->config['auth_group_access']) {
$userGroups = Db::name($dbName)
->alias('aga')
Expand All @@ -249,7 +236,6 @@ public function getGroups(int $uid): array
->toArray();
}

$groups[$dbName][$uid] = $userGroups ?: [];
return $groups[$dbName][$uid];
return $userGroups;
}
}

0 comments on commit 0e4bcd0

Please sign in to comment.