🍅 refactor(cache): 清空缓存时,非超管用户禁止清空非本租户的用户Session
This commit is contained in:
parent
768cad6ddc
commit
183e2acfe1
@ -12,11 +12,17 @@ namespace Admin.NET.Core.Service;
|
|||||||
[ApiDescriptionSettings(Order = 400, Description = "系统缓存")]
|
[ApiDescriptionSettings(Order = 400, Description = "系统缓存")]
|
||||||
public class SysCacheService : IDynamicApiController, ISingleton
|
public class SysCacheService : IDynamicApiController, ISingleton
|
||||||
{
|
{
|
||||||
|
private readonly Lazy<UserManager> _userManager = new(() => App.GetService<UserManager>());
|
||||||
|
private readonly SqlSugarRepository<SysUser> _sysUserRep;
|
||||||
private static ICacheProvider _cacheProvider;
|
private static ICacheProvider _cacheProvider;
|
||||||
private readonly CacheOptions _cacheOptions;
|
private readonly CacheOptions _cacheOptions;
|
||||||
|
|
||||||
public SysCacheService(ICacheProvider cacheProvider, IOptions<CacheOptions> cacheOptions)
|
public SysCacheService(
|
||||||
|
ICacheProvider cacheProvider,
|
||||||
|
IOptions<CacheOptions> cacheOptions,
|
||||||
|
SqlSugarRepository<SysUser> sysUserRep)
|
||||||
{
|
{
|
||||||
|
_sysUserRep = sysUserRep;
|
||||||
_cacheProvider = cacheProvider;
|
_cacheProvider = cacheProvider;
|
||||||
_cacheOptions = cacheOptions.Value;
|
_cacheOptions = cacheOptions.Value;
|
||||||
}
|
}
|
||||||
@ -427,10 +433,19 @@ public class SysCacheService : IDynamicApiController, ISingleton
|
|||||||
[DisplayName("清空所有缓存")]
|
[DisplayName("清空所有缓存")]
|
||||||
[ApiDescriptionSettings(Name = "Clear"), HttpPost]
|
[ApiDescriptionSettings(Name = "Clear"), HttpPost]
|
||||||
public void Clear()
|
public void Clear()
|
||||||
|
{
|
||||||
|
// 超管用户操作,清空所有缓存
|
||||||
|
if (_userManager.Value.SuperAdmin)
|
||||||
{
|
{
|
||||||
_cacheProvider.Cache.Clear();
|
_cacheProvider.Cache.Clear();
|
||||||
|
|
||||||
Cache.Default.Clear();
|
Cache.Default.Clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 排除非本租户、以及超管的Session缓存
|
||||||
|
var userIds = _sysUserRep.AsQueryable().Where(u => u.AccountType != AccountTypeEnum.SuperAdmin).Select(u => u.Id).ToList().Select(u => u.ToString()).ToList();
|
||||||
|
var keys = _cacheProvider.Cache.Keys.Where(key => !key.StartsWith(CacheConst.KeyUserSession) || userIds.Any(key.EndsWith)).ToList();
|
||||||
|
keys.ForEach(key => _cacheProvider.Cache.Remove(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user