🍒 refactor(core): 使用 LazyHelper 类统一处理延迟初始化服务实例
This commit is contained in:
parent
e19e6a13bb
commit
c4f3295409
@ -11,8 +11,6 @@ namespace Admin.NET.Core;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class DatabaseLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
public class DatabaseLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
||||||
{
|
{
|
||||||
private static readonly Lazy<SqlSugarRepository<SysUser>> _sysUserRep = new(() => App.GetService<SqlSugarRepository<SysUser>>());
|
|
||||||
private static readonly Lazy<UserManager> _userManager = new(() => App.GetService<UserManager>());
|
|
||||||
private readonly ILogger<DatabaseLoggingWriter> _logger;
|
private readonly ILogger<DatabaseLoggingWriter> _logger;
|
||||||
private readonly SysConfigService _sysConfigService;
|
private readonly SysConfigService _sysConfigService;
|
||||||
private readonly IEventPublisher _eventPublisher;
|
private readonly IEventPublisher _eventPublisher;
|
||||||
@ -190,6 +188,7 @@ public class DatabaseLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private LoggingUserInfo GetUserInfo(LoggingMonitorDto loggingMonitor)
|
private LoggingUserInfo GetUserInfo(LoggingMonitorDto loggingMonitor)
|
||||||
{
|
{
|
||||||
|
var userManger = LazyHelper.GetService<UserManager>();
|
||||||
LoggingUserInfo result = new();
|
LoggingUserInfo result = new();
|
||||||
if (loggingMonitor.AuthorizationClaims != null)
|
if (loggingMonitor.AuthorizationClaims != null)
|
||||||
{
|
{
|
||||||
@ -198,7 +197,7 @@ public class DatabaseLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
|||||||
.GetValueOrDefault(ClaimConst.UserId) ?? ""
|
.GetValueOrDefault(ClaimConst.UserId) ?? ""
|
||||||
, out var temp) ? temp : null;
|
, out var temp) ? temp : null;
|
||||||
|
|
||||||
var userSession = _userManager.Value.GetSession(result.UserId);
|
var userSession = userManger.GetSession(result.UserId);
|
||||||
result.TenantId = long.TryParse(userSession?.TenantId?.ToString() ?? "", out temp) ? temp : null;
|
result.TenantId = long.TryParse(userSession?.TenantId?.ToString() ?? "", out temp) ? temp : null;
|
||||||
result.RealName = userSession?.RealName;
|
result.RealName = userSession?.RealName;
|
||||||
result.Account = userSession?.Account;
|
result.Account = userSession?.Account;
|
||||||
@ -207,7 +206,7 @@ public class DatabaseLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
|||||||
// 退出登陆时没有session,尝试从数据库中获取
|
// 退出登陆时没有session,尝试从数据库中获取
|
||||||
if (string.IsNullOrWhiteSpace(result.Account) && result.UserId != null)
|
if (string.IsNullOrWhiteSpace(result.Account) && result.UserId != null)
|
||||||
{
|
{
|
||||||
var user = _sysUserRep.Value.GetById(result.UserId);
|
var user = _db.Queryable<SysUser>().First(u => u.Id == result.UserId);
|
||||||
result.Account = user?.TenantId?.ToString();
|
result.Account = user?.TenantId?.ToString();
|
||||||
result.RealName = user?.RealName;
|
result.RealName = user?.RealName;
|
||||||
result.Account = user?.Account;
|
result.Account = user?.Account;
|
||||||
@ -221,7 +220,7 @@ public class DatabaseLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
|||||||
result.Account = (loggingMonitor.Parameters[0].Value as JObject)!.GetValue("account")?.ToString();
|
result.Account = (loggingMonitor.Parameters[0].Value as JObject)!.GetValue("account")?.ToString();
|
||||||
if (!string.IsNullOrEmpty(result.Account))
|
if (!string.IsNullOrEmpty(result.Account))
|
||||||
{
|
{
|
||||||
var user = _sysUserRep.Value.AsQueryable().First(u => u.Account == result.Account);
|
var user = _db.Queryable<SysUser>().First(u => u.Account == result.Account);
|
||||||
result.TenantId = user?.TenantId;
|
result.TenantId = user?.TenantId;
|
||||||
result.RealName = user?.RealName;
|
result.RealName = user?.RealName;
|
||||||
result.UserId = user?.Id;
|
result.UserId = user?.Id;
|
||||||
|
|||||||
@ -17,7 +17,6 @@ public class ElasticSearchLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
|||||||
private readonly ElasticsearchClient _esClient;
|
private readonly ElasticsearchClient _esClient;
|
||||||
private readonly SysCacheService _sysCacheService;
|
private readonly SysCacheService _sysCacheService;
|
||||||
private readonly SysConfigService _sysConfigService;
|
private readonly SysConfigService _sysConfigService;
|
||||||
private static readonly Lazy<UserManager> _userManager = new(() => App.GetService<UserManager>());
|
|
||||||
|
|
||||||
public ElasticSearchLoggingWriter(IServiceScopeFactory scopeFactory)
|
public ElasticSearchLoggingWriter(IServiceScopeFactory scopeFactory)
|
||||||
{
|
{
|
||||||
@ -51,7 +50,7 @@ public class ElasticSearchLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
|||||||
{
|
{
|
||||||
if (item.type != ClaimConst.UserId) continue;
|
if (item.type != ClaimConst.UserId) continue;
|
||||||
userId = item.value;
|
userId = item.value;
|
||||||
userSession = _userManager.Value?.GetSession(userId);
|
userSession = LazyHelper.GetService<UserManager>().GetSessionOrRefresh(userId);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
tenantId = userSession?.TenantId.ToString();
|
tenantId = userSession?.TenantId.ToString();
|
||||||
|
|||||||
@ -11,7 +11,6 @@ namespace Admin.NET.Core;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class HttpLoggingHandler : DelegatingHandler, ITransient
|
public class HttpLoggingHandler : DelegatingHandler, ITransient
|
||||||
{
|
{
|
||||||
private static readonly Lazy<UserManager> UserManager = new(() => App.GetService<UserManager>());
|
|
||||||
private readonly Dictionary<string, HttpRemoteItem> _optionMap;
|
private readonly Dictionary<string, HttpRemoteItem> _optionMap;
|
||||||
private readonly SysConfigService _sysConfigService;
|
private readonly SysConfigService _sysConfigService;
|
||||||
private readonly IEventPublisher _eventPublisher;
|
private readonly IEventPublisher _eventPublisher;
|
||||||
@ -39,6 +38,7 @@ public class HttpLoggingHandler : DelegatingHandler, ITransient
|
|||||||
if (!enabledLog || attr?.IgnoreLog == true) return await base.SendAsync(request, cancellationToken);
|
if (!enabledLog || attr?.IgnoreLog == true) return await base.SendAsync(request, cancellationToken);
|
||||||
|
|
||||||
var stopWatch = Stopwatch.StartNew();
|
var stopWatch = Stopwatch.StartNew();
|
||||||
|
var userManger = LazyHelper.GetService<UserManager>();
|
||||||
var urlList = request.RequestUri?.LocalPath.Split("/") ?? [];
|
var urlList = request.RequestUri?.LocalPath.Split("/") ?? [];
|
||||||
var sysLogHttp = new SysLogHttp
|
var sysLogHttp = new SysLogHttp
|
||||||
{
|
{
|
||||||
@ -49,9 +49,9 @@ public class HttpLoggingHandler : DelegatingHandler, ITransient
|
|||||||
RequestUrl = request.RequestUri?.ToString(),
|
RequestUrl = request.RequestUri?.ToString(),
|
||||||
RequestHeaders = request.Headers.ToDictionary(u => u.Key, u => u.Value.Join(";")).ToJson(),
|
RequestHeaders = request.Headers.ToDictionary(u => u.Key, u => u.Value.Join(";")).ToJson(),
|
||||||
RequestBodyPlaintext = request.GetRequestBodyPlaintext(),
|
RequestBodyPlaintext = request.GetRequestBodyPlaintext(),
|
||||||
TenantId = UserManager.Value?.TenantId,
|
TenantId = userManger?.TenantId,
|
||||||
CreateUserId = UserManager.Value?.UserId,
|
CreateUserId = userManger?.UserId,
|
||||||
CreateUserName = UserManager.Value?.RealName,
|
CreateUserName = userManger?.RealName,
|
||||||
};
|
};
|
||||||
if (request.Content != null) sysLogHttp.RequestBody = await request.Content.ReadAsStringAsync(cancellationToken);
|
if (request.Content != null) sysLogHttp.RequestBody = await request.Content.ReadAsStringAsync(cancellationToken);
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,6 @@ 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 static ICacheProvider _cacheProvider;
|
private static ICacheProvider _cacheProvider;
|
||||||
private readonly CacheOptions _cacheOptions;
|
private readonly CacheOptions _cacheOptions;
|
||||||
|
|
||||||
@ -432,7 +431,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
|
|||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
// 超管用户操作,清空所有缓存
|
// 超管用户操作,清空所有缓存
|
||||||
if (_userManager.Value.SuperAdmin)
|
if (LazyHelper.GetService<UserManager>().SuperAdmin)
|
||||||
{
|
{
|
||||||
_cacheProvider.Cache.Clear();
|
_cacheProvider.Cache.Clear();
|
||||||
Cache.Default.Clear();
|
Cache.Default.Clear();
|
||||||
|
|||||||
@ -172,17 +172,14 @@ public class SysCommonService : IDynamicApiController, ITransient
|
|||||||
[DisplayName("获取所有移动端接口")]
|
[DisplayName("获取所有移动端接口")]
|
||||||
public List<string> GetAppApiList()
|
public List<string> GetAppApiList()
|
||||||
{
|
{
|
||||||
var apiList = _sysCacheService.Get<List<string>>(CacheConst.KeyAppApi);
|
List<string> apiList = [];
|
||||||
if (apiList == null)
|
var queue = new Queue<ApiOutput>(GetApiList());
|
||||||
|
var item = queue.Dequeue();
|
||||||
|
while (item != null)
|
||||||
{
|
{
|
||||||
apiList = [];
|
if (item.Children is { Count: > 0 }) queue.EnqueueRange(item.Children);
|
||||||
var allApiList = GetApiList("", true);
|
else apiList.Add(item.Route);
|
||||||
foreach (var apiOutput in allApiList)
|
item = queue.Count > 0 ? queue.Dequeue() : null;
|
||||||
{
|
|
||||||
foreach (var controller in apiOutput.Children)
|
|
||||||
apiList.AddRange(controller.Children.Select(u => u.Route));
|
|
||||||
}
|
|
||||||
_sysCacheService.Set(CacheConst.KeyAppApi, apiList, TimeSpan.FromDays(7));
|
|
||||||
}
|
}
|
||||||
return apiList;
|
return apiList;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -522,11 +522,6 @@ public static class SqlSugarExtension
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly ConcurrentDictionary<Type, Dictionary<string, (PropertyInfo Prop, BindTextAbbrAttribute Attr)>?> _textAbbrPropCache = new();
|
private static readonly ConcurrentDictionary<Type, Dictionary<string, (PropertyInfo Prop, BindTextAbbrAttribute Attr)>?> _textAbbrPropCache = new();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 系统通用服务
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Lazy<SysCommonService> _lazySysCommonService = new(() => App.GetService<SysCommonService>());
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化文本简称数据
|
/// 初始化文本简称数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -588,7 +583,7 @@ public static class SqlSugarExtension
|
|||||||
if (string.IsNullOrWhiteSpace(value)) return;
|
if (string.IsNullOrWhiteSpace(value)) return;
|
||||||
|
|
||||||
// 使用线程安全的延迟初始化服务实例获取文本缩写
|
// 使用线程安全的延迟初始化服务实例获取文本缩写
|
||||||
var abbrValue = _lazySysCommonService.Value
|
var abbrValue = LazyHelper.GetService<SysCommonService>()
|
||||||
.GetNameAbbr(new() { Text = value, All = attribute.SaveFullAbbr })
|
.GetNameAbbr(new() { Text = value, All = attribute.SaveFullAbbr })
|
||||||
.GetAwaiter()
|
.GetAwaiter()
|
||||||
.GetResult();
|
.GetResult();
|
||||||
@ -604,11 +599,6 @@ public static class SqlSugarExtension
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly ConcurrentDictionary<Type, Dictionary<string, (PropertyInfo Prop, BindSerialAttribute Attr)>?> _serialPropCache = new();
|
private static readonly ConcurrentDictionary<Type, Dictionary<string, (PropertyInfo Prop, BindSerialAttribute Attr)>?> _serialPropCache = new();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 系统通用服务
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Lazy<SysSerialService> _lazySysSerialService = new(() => App.GetService<SysSerialService>());
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 自动生成流水号到绑定字段
|
/// 自动生成流水号到绑定字段
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -643,7 +633,7 @@ public static class SqlSugarExtension
|
|||||||
var (_, attribute) = propData;
|
var (_, attribute) = propData;
|
||||||
|
|
||||||
// 使用线程安全的延迟初始化服务实例获取流水号
|
// 使用线程安全的延迟初始化服务实例获取流水号
|
||||||
var serial = _lazySysSerialService.Value
|
var serial = LazyHelper.GetService<SysSerialService>()
|
||||||
.NextSeqNo(attribute.Type, attribute.IsGlobal)
|
.NextSeqNo(attribute.Type, attribute.IsGlobal)
|
||||||
.GetAwaiter()
|
.GetAwaiter()
|
||||||
.GetResult();
|
.GetResult();
|
||||||
|
|||||||
@ -12,8 +12,6 @@ namespace Admin.NET.Core;
|
|||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public class SqlSugarRepository<T> : SimpleClient<T>, ISqlSugarRepository<T> where T : class, new()
|
public class SqlSugarRepository<T> : SimpleClient<T>, ISqlSugarRepository<T> where T : class, new()
|
||||||
{
|
{
|
||||||
private static readonly Lazy<UserManager> _userManager = new(() => App.GetService<UserManager>());
|
|
||||||
|
|
||||||
public SqlSugarRepository()
|
public SqlSugarRepository()
|
||||||
{
|
{
|
||||||
var iTenant = SqlSugarSetup.ITenant;
|
var iTenant = SqlSugarSetup.ITenant;
|
||||||
@ -39,7 +37,7 @@ public class SqlSugarRepository<T> : SimpleClient<T>, ISqlSugarRepository<T> whe
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
// 若未贴任何表特性或当前未登录或是默认租户Id,则返回默认库连接
|
// 若未贴任何表特性或当前未登录或是默认租户Id,则返回默认库连接
|
||||||
var tenantId = _userManager.Value.TenantId?.ToString();
|
var tenantId = LazyHelper.GetService<UserManager>().TenantId?.ToString();
|
||||||
if (string.IsNullOrWhiteSpace(tenantId) || tenantId == SqlSugarConst.MainConfigId) return;
|
if (string.IsNullOrWhiteSpace(tenantId) || tenantId == SqlSugarConst.MainConfigId) return;
|
||||||
|
|
||||||
// 根据租户Id切换库连接, 为空则返回默认库连接
|
// 根据租户Id切换库连接, 为空则返回默认库连接
|
||||||
|
|||||||
@ -215,8 +215,8 @@ public static class SqlSugarSetup
|
|||||||
Log.Warning(log);
|
Log.Warning(log);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var userManager = LazyHelper.GetService<UserManager>();
|
||||||
// 数据审计
|
// 数据审计
|
||||||
Lazy<UserManager> userManager = new(() => App.GetService<UserManager>());
|
|
||||||
dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
|
dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
|
||||||
{
|
{
|
||||||
// 新增/插入 操作
|
// 新增/插入 操作
|
||||||
@ -249,7 +249,7 @@ public static class SqlSugarSetup
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 若当前用户非空(web线程时)
|
// 若当前用户非空(web线程时)
|
||||||
if (App.User == null || userManager.Value == null) return;
|
if (App.User == null) return;
|
||||||
|
|
||||||
dynamic entityValue = entityInfo.EntityValue;
|
dynamic entityValue = entityInfo.EntityValue;
|
||||||
// 若应用Id为空则赋值当前用户应用Id,若当前用户应用Id为空,则取默认应用Id
|
// 若应用Id为空则赋值当前用户应用Id,若当前用户应用Id为空,则取默认应用Id
|
||||||
@ -257,33 +257,33 @@ public static class SqlSugarSetup
|
|||||||
{
|
{
|
||||||
var appId = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue)!;
|
var appId = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue)!;
|
||||||
if (appId == null || appId.Equals(0))
|
if (appId == null || appId.Equals(0))
|
||||||
entityInfo.SetValue(userManager.Value?.AppId ?? SqlSugarConst.DefaultAppId);
|
entityInfo.SetValue(userManager.AppId ?? SqlSugarConst.DefaultAppId);
|
||||||
}
|
}
|
||||||
else if (entityInfo.PropertyName == nameof(EntityTenantId.TenantId))
|
else if (entityInfo.PropertyName == nameof(EntityTenantId.TenantId))
|
||||||
{
|
{
|
||||||
if (entityValue.TenantId == null || entityValue.TenantId == 0)
|
if (entityValue.TenantId == null || entityValue.TenantId == 0)
|
||||||
entityInfo.SetValue(userManager.Value.TenantId?.ToString() ??
|
entityInfo.SetValue(userManager.TenantId?.ToString() ??
|
||||||
SqlSugarConst.DefaultTenantId.ToString());
|
SqlSugarConst.DefaultTenantId.ToString());
|
||||||
}
|
}
|
||||||
else if (entityInfo.PropertyName == nameof(EntityBase.CreateUserId))
|
else if (entityInfo.PropertyName == nameof(EntityBase.CreateUserId))
|
||||||
{
|
{
|
||||||
if (entityValue.CreateUserId == null || entityValue.CreateUserId == 0)
|
if (entityValue.CreateUserId == null || entityValue.CreateUserId == 0)
|
||||||
entityInfo.SetValue(userManager.Value.UserId);
|
entityInfo.SetValue(userManager.UserId);
|
||||||
}
|
}
|
||||||
else if (entityInfo.PropertyName == nameof(EntityBase.CreateUserName))
|
else if (entityInfo.PropertyName == nameof(EntityBase.CreateUserName))
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(entityValue.CreateUserName))
|
if (string.IsNullOrWhiteSpace(entityValue.CreateUserName))
|
||||||
entityInfo.SetValue(userManager.Value.RealName);
|
entityInfo.SetValue(userManager.RealName);
|
||||||
}
|
}
|
||||||
else if (entityInfo.PropertyName == nameof(EntityBaseData.CreateOrgId))
|
else if (entityInfo.PropertyName == nameof(EntityBaseData.CreateOrgId))
|
||||||
{
|
{
|
||||||
if (entityValue.CreateOrgId == null || entityValue.CreateOrgId == 0)
|
if (entityValue.CreateOrgId == null || entityValue.CreateOrgId == 0)
|
||||||
entityInfo.SetValue(userManager.Value.OrgId);
|
entityInfo.SetValue(userManager.OrgId);
|
||||||
}
|
}
|
||||||
else if (entityInfo.PropertyName == nameof(EntityBaseData.CreateOrgName))
|
else if (entityInfo.PropertyName == nameof(EntityBaseData.CreateOrgName))
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(entityValue.CreateOrgName))
|
if (string.IsNullOrWhiteSpace(entityValue.CreateOrgName))
|
||||||
entityInfo.SetValue(userManager.Value.OrgName);
|
entityInfo.SetValue(userManager.OrgName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 编辑/更新 操作
|
// 编辑/更新 操作
|
||||||
@ -294,7 +294,7 @@ public static class SqlSugarSetup
|
|||||||
else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserId))
|
else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserId))
|
||||||
entityInfo.SetValue(App.User?.FindFirst(ClaimConst.UserId)?.Value);
|
entityInfo.SetValue(App.User?.FindFirst(ClaimConst.UserId)?.Value);
|
||||||
else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserName))
|
else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserName))
|
||||||
entityInfo.SetValue(userManager.Value.RealName);
|
entityInfo.SetValue(userManager.RealName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置绑定简称字段数据
|
// 设置绑定简称字段数据
|
||||||
@ -308,13 +308,13 @@ public static class SqlSugarSetup
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 超管不受任何过滤器限制
|
// 超管不受任何过滤器限制
|
||||||
if (userManager.Value.AccountType == AccountTypeEnum.SuperAdmin) return;
|
if (userManager.AccountType == AccountTypeEnum.SuperAdmin) return;
|
||||||
|
|
||||||
// 配置假删除过滤器
|
// 配置假删除过滤器
|
||||||
dbProvider.QueryFilter.AddTableFilter<IDeletedFilter>(u => u.IsDelete == false);
|
dbProvider.QueryFilter.AddTableFilter<IDeletedFilter>(u => u.IsDelete == false);
|
||||||
|
|
||||||
// 配置租户过滤器
|
// 配置租户过滤器
|
||||||
var tenantId = userManager.Value.TenantId?.ToString();
|
var tenantId = userManager.TenantId?.ToString();
|
||||||
if (!string.IsNullOrWhiteSpace(tenantId))
|
if (!string.IsNullOrWhiteSpace(tenantId))
|
||||||
dbProvider.QueryFilter.AddTableFilter<ITenantIdFilter>(u => u.TenantId == long.Parse(tenantId));
|
dbProvider.QueryFilter.AddTableFilter<ITenantIdFilter>(u => u.TenantId == long.Parse(tenantId));
|
||||||
|
|
||||||
|
|||||||
23
Admin.NET/Admin.NET.Core/Utils/LazyHelper.cs
Normal file
23
Admin.NET/Admin.NET.Core/Utils/LazyHelper.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||||
|
//
|
||||||
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||||
|
//
|
||||||
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||||
|
|
||||||
|
namespace Admin.NET.Core;
|
||||||
|
|
||||||
|
public class LazyHelper
|
||||||
|
{
|
||||||
|
private static readonly Dictionary<Type, Lazy<object>> _cache = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取服务
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static T GetService<T>() where T : class
|
||||||
|
{
|
||||||
|
if (_cache.TryGetValue(typeof(T), out var lazy)) return (T)lazy.Value;
|
||||||
|
return (T)(_cache[typeof(T)] = new Lazy<object>(() => App.GetService<T>())).Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -23,7 +23,6 @@ namespace Admin.NET.Web.Core
|
|||||||
public class JwtHandler : AppAuthorizeHandler
|
public class JwtHandler : AppAuthorizeHandler
|
||||||
{
|
{
|
||||||
private readonly IServiceProvider _serviceProvider;
|
private readonly IServiceProvider _serviceProvider;
|
||||||
private static readonly Lazy<UserManager> _userManager = new(() => App.GetService<UserManager>());
|
|
||||||
|
|
||||||
public JwtHandler(IServiceProvider serviceProvider)
|
public JwtHandler(IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
@ -93,7 +92,7 @@ namespace Admin.NET.Web.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 验证租户有效期
|
// 验证租户有效期
|
||||||
var tenantId = _userManager.Value.TenantId?.ToString();
|
var tenantId = LazyHelper.GetService<UserManager>().TenantId?.ToString();
|
||||||
if (!string.IsNullOrWhiteSpace(tenantId))
|
if (!string.IsNullOrWhiteSpace(tenantId))
|
||||||
{
|
{
|
||||||
var tenant = sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.FirstOrDefault(u => u.Id == long.Parse(tenantId));
|
var tenant = sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.FirstOrDefault(u => u.Id == long.Parse(tenantId));
|
||||||
@ -119,32 +118,31 @@ namespace Admin.NET.Web.Core
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private static async Task<bool> CheckAuthorizeAsync(DefaultHttpContext httpContext)
|
private static async Task<bool> CheckAuthorizeAsync(DefaultHttpContext httpContext)
|
||||||
{
|
{
|
||||||
// 排除超管权限判断
|
var userManager = LazyHelper.GetService<UserManager>();
|
||||||
if (_userManager.Value?.AccountType == AccountTypeEnum.SuperAdmin)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
var serviceScope = httpContext.RequestServices.CreateScope();
|
// 排除超管权限判断
|
||||||
|
if (userManager?.AccountType == AccountTypeEnum.SuperAdmin) return true;
|
||||||
|
|
||||||
// 当前接口路由
|
// 当前接口路由
|
||||||
var path = httpContext.Request.Path.ToString();
|
var path = httpContext.Request.Path.ToString();
|
||||||
|
|
||||||
// 移动端接口权限判断
|
// 移动端接口权限判断
|
||||||
if (_userManager.Value?.LoginMode == LoginModeEnum.APP)
|
if (userManager?.LoginMode == LoginModeEnum.APP)
|
||||||
{
|
{
|
||||||
var appApiList = serviceScope.ServiceProvider.GetRequiredService<SysCommonService>().GetAppApiList();
|
var appPermissions = userManager.AppPermissions ?? [];
|
||||||
return appApiList.Exists(u => path.EndsWith(u, StringComparison.CurrentCultureIgnoreCase));
|
return appPermissions.Exists(u => path.EndsWith(u, StringComparison.CurrentCultureIgnoreCase));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取当前用户按钮权限集合和接口黑名单
|
// 获取当前用户按钮权限集合和接口黑名单
|
||||||
var sysRoleService = serviceScope.ServiceProvider.GetRequiredService<SysRoleService>();
|
var unauthorizedPermissions = userManager?.UnauthorizedPermissions ?? [];
|
||||||
var roleApis = await sysRoleService.GetUserApiList();
|
var permissions = userManager?.Permissions ?? [];
|
||||||
|
|
||||||
// 若当前路由在按钮权限集合里面则放行
|
// 若当前路由在按钮权限集合里面则放行
|
||||||
if (roleApis[0].Exists(u => path.EndsWith(u, StringComparison.CurrentCultureIgnoreCase)))
|
if (permissions.Exists(u => path.EndsWith(u, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// 若当前路由在已接口黑名单里面则禁止
|
// 若当前路由在已接口黑名单里面则禁止
|
||||||
return roleApis[1].TrueForAll(u => !path.EndsWith(u, StringComparison.CurrentCultureIgnoreCase));
|
return unauthorizedPermissions.TrueForAll(u => !path.EndsWith(u, StringComparison.CurrentCultureIgnoreCase));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -17,8 +17,6 @@ namespace Admin.NET.Plugin.ReZero.Service;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SuperApiAop : DefaultSuperApiAop
|
public class SuperApiAop : DefaultSuperApiAop
|
||||||
{
|
{
|
||||||
private static readonly Lazy<UserManager> _userManager = new(() => App.GetService<UserManager>());
|
|
||||||
|
|
||||||
public override async Task OnExecutingAsync(InterfaceContext aopContext)
|
public override async Task OnExecutingAsync(InterfaceContext aopContext)
|
||||||
{
|
{
|
||||||
////if (aopContext.InterfaceType == InterfaceType.DynamicApi)
|
////if (aopContext.InterfaceType == InterfaceType.DynamicApi)
|
||||||
@ -60,33 +58,34 @@ public class SuperApiAop : DefaultSuperApiAop
|
|||||||
var api = aopContext.InterfaceInfo;
|
var api = aopContext.InterfaceInfo;
|
||||||
var context = aopContext.HttpContext;
|
var context = aopContext.HttpContext;
|
||||||
|
|
||||||
var paths = api.Url.Split('/');
|
var paths = api?.Url?.Split('/');
|
||||||
var actionName = paths[paths.Length - 1];
|
var actionName = paths?[^1];
|
||||||
|
|
||||||
|
var userManager = LazyHelper.GetService<UserManager>();
|
||||||
var apiInfo = new
|
var apiInfo = new
|
||||||
{
|
{
|
||||||
requestUrl = api.Url,
|
requestUrl = api?.Url,
|
||||||
httpMethod = api.HttpMethod,
|
httpMethod = api?.HttpMethod,
|
||||||
displayTitle = api.Name,
|
displayTitle = api?.Name,
|
||||||
actionTypeName = actionName,
|
actionTypeName = actionName,
|
||||||
controllerName = aopContext.InterfaceType == InterfaceType.DynamicApi ? $"ReZero动态-{api.GroupName}" : $"ReZero系统-{api.GroupName}",
|
controllerName = aopContext.InterfaceType == InterfaceType.DynamicApi ? $"ReZero动态-{api?.GroupName}" : $"ReZero系统-{api?.GroupName}",
|
||||||
remoteIPv4 = context.GetRemoteIpAddressToIPv4(),
|
remoteIPv4 = context.GetRemoteIpAddressToIPv4(),
|
||||||
userAgent = context.Request.Headers["User-Agent"],
|
userAgent = context?.Request.Headers["User-Agent"],
|
||||||
returnInformation = new
|
returnInformation = new
|
||||||
{
|
{
|
||||||
httpStatusCode = context.Response.StatusCode,
|
httpStatusCode = context?.Response.StatusCode,
|
||||||
},
|
},
|
||||||
authorizationClaims = new[]
|
authorizationClaims = new[]
|
||||||
{
|
{
|
||||||
new
|
new
|
||||||
{
|
{
|
||||||
type = _userManager.Value?.Account,
|
type = userManager?.Account,
|
||||||
value = _userManager.Value?.RealName
|
value = userManager?.RealName
|
||||||
},
|
},
|
||||||
new
|
new
|
||||||
{
|
{
|
||||||
type = _userManager.Value?.RealName,
|
type = userManager?.RealName,
|
||||||
value = _userManager.Value?.RealName
|
value = userManager?.RealName
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
exception = aopContext.Exception == null ? null : JSON.Serialize(aopContext.Exception)
|
exception = aopContext.Exception == null ? null : JSON.Serialize(aopContext.Exception)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user