Files
zyt/server/app/common/cache/AdminAuthCache.php
T
2026-11-11 13:53:14 +08:00

103 lines
3.3 KiB
PHP
Executable File

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\common\cache;
use app\adminapi\logic\auth\AuthLogic;
use app\common\model\auth\SystemMenu;
/**
* 管理员权限缓存
* Class AdminAuthCache
* @package app\common\cache
*/
class AdminAuthCache extends BaseCache
{
private $prefix = 'admin_auth_';
private $authConfigList = [];
private $cacheMd5Key = ''; //权限文件MD5的key
private $cacheAllKey = ''; //全部权限的key
private $cacheUrlKey = ''; //管理员的url缓存key
private $authMd5 = ''; //权限文件MD5的值
private $adminId = '';
public function __construct($adminId = '')
{
parent::__construct();
$this->adminId = $adminId;
// 全部权限
$this->authConfigList = AuthLogic::getAllAuth();
// 当前权限配置文件的md5
$this->authMd5 = md5(json_encode($this->authConfigList));
$this->cacheMd5Key = $this->prefix . 'md5';
$this->cacheAllKey = $this->prefix . 'all';
$this->cacheUrlKey = $this->prefix . 'url_' . $this->adminId;
$cacheAuthMd5 = $this->get($this->cacheMd5Key);
$cacheAuth = $this->get($this->cacheAllKey);
//权限配置和缓存权限对比,不一样说明权限配置文件已修改,清理缓存
if ($this->authMd5 !== $cacheAuthMd5 || empty($cacheAuth)) {
$this->deleteTag();
}
}
/**
* @notes 获取管理权限uri
* @param $adminId
* @return array|mixed
* @author 令狐冲
* @date 2021/8/19 15:27
*/
public function getAdminUri()
{
// 授权必须读取已提交的原关联;缓存删除后仍可能被并发旧请求回填,不能再信任小时快照。
return AuthLogic::getAuthByAdminId($this->adminId);
}
/**
* @notes 获取全部权限uri
* @return array|mixed
* @author cjhao
* @date 2021/9/13 11:41
*/
public function getAllUri()
{
// 停用的菜单仍是受控URI,否则旧中间件会把它误判为“未登记接口”直接放行。
return SystemMenu::distinct(true)->where('perms', '<>', '')->column('perms');
}
/**
* @notes 清理管理员缓存
* @return bool
* @author cjhao
* @date 2021/10/13 18:47
*/
public function clearAuthCache()
{
// BaseCache::set() 使用类名作为标签,并没有使用 cacheUrlKey 作为标签;
// 这里必须直接删除单账号缓存键,否则角色变更后旧权限会继续保留一小时。
return $this->delete($this->cacheUrlKey);
}
}