Merge pull request '🍒 添加系统接口信息表并实现接口信息持久化,并增加权限相关视图,提高查询效率' (#440) from jasondom/Admin.NET.Pro:v2 into v2
Reviewed-on: https://code.adminnet.top/Admin.NET/Admin.NET.Pro/pulls/440
This commit is contained in:
commit
0cd44ad692
63
Admin.NET/Admin.NET.Core/Entity/SysApiInfo.cs
Normal file
63
Admin.NET/Admin.NET.Core/Entity/SysApiInfo.cs
Normal file
@ -0,0 +1,63 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 系统接口信息表
|
||||
/// </summary>
|
||||
[SysTable]
|
||||
[SugarTable(null, "系统接口信息表")]
|
||||
public partial class SysApiInfo : EntityBaseId
|
||||
{
|
||||
/// <summary>
|
||||
/// 组名
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "组名", Length = 64)]
|
||||
public string? GroupName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 路由名
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "路由名", Length = 64)]
|
||||
public string? Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 描述
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "描述", Length = 64)]
|
||||
public string? Desc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 路由
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "路由", Length = 64)]
|
||||
public string Route { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 控制器名称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "控制器名称", Length = 64)]
|
||||
public string Action { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 请求方式
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "请求方式", Length = 16)]
|
||||
public string HttpMethod { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否移动端接口
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "是否移动端接口")]
|
||||
public bool IsAppApi { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "排序")]
|
||||
public int Order { get; set; } = 100;
|
||||
}
|
||||
58
Admin.NET/Admin.NET.Core/Entity/VSysPermissions.cs
Normal file
58
Admin.NET/Admin.NET.Core/Entity/VSysPermissions.cs
Normal file
@ -0,0 +1,58 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 系统权限标识视图
|
||||
/// </summary>
|
||||
[SugarTable(null, "系统权限标识视图"), IgnoreTable]
|
||||
public class VSysPermissions : ISqlSugarView
|
||||
{
|
||||
/// <summary>
|
||||
/// 来自菜单标识
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "来自菜单标识")]
|
||||
public long MenuId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "状态")]
|
||||
public StatusEnum Status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 权限标识
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "权限标识", IsPrimaryKey = true)]
|
||||
public string Permission { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 视图语句
|
||||
/// </summary>
|
||||
public string GetQueryableSqlString(SqlSugarScopeProvider db)
|
||||
{
|
||||
return db.Union(
|
||||
db.Queryable<SysMenu>()
|
||||
.Where(u => u.Type == MenuTypeEnum.Btn && u.Status == StatusEnum.Enable && !string.IsNullOrWhiteSpace(u.Permission))
|
||||
.Select(u => new VSysPermissions
|
||||
{
|
||||
MenuId = u.Id,
|
||||
Status = u.Status,
|
||||
Permission = u.Permission
|
||||
}),
|
||||
db.Queryable<SysApiInfo>()
|
||||
.Where(u => !string.IsNullOrWhiteSpace(u.Route) && SqlFunc.Subqueryable<SysMenu>().Where(z => z.Permission == u.Route).NotAny())
|
||||
.Select(u => new VSysPermissions
|
||||
{
|
||||
MenuId = 0,
|
||||
Status = StatusEnum.Enable,
|
||||
Permission = u.Route,
|
||||
}).Distinct())
|
||||
.OrderBy(u => u.Permission)
|
||||
.ToMappedSqlString();
|
||||
}
|
||||
}
|
||||
82
Admin.NET/Admin.NET.Core/Entity/VSysUserOrg.cs
Normal file
82
Admin.NET/Admin.NET.Core/Entity/VSysUserOrg.cs
Normal file
@ -0,0 +1,82 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 系统用户机构视图
|
||||
/// </summary>
|
||||
[SugarTable(null, "系统用户机构视图"), IgnoreTable]
|
||||
public class VSysUserOrg : ISqlSugarView
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户Id
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "用户Id")]
|
||||
public long UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 机构Id
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "机构Id")]
|
||||
public long OrgId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 视图语句
|
||||
/// </summary>
|
||||
public string GetQueryableSqlString(SqlSugarScopeProvider db)
|
||||
{
|
||||
return db.Union(
|
||||
// 获取用户表中的机构Id
|
||||
db.Queryable<SysUser>().IgnoreTenant()
|
||||
.Select(u => new VSysUserOrg
|
||||
{
|
||||
UserId = u.Id,
|
||||
OrgId = u.OrgId
|
||||
}),
|
||||
// 获取用户创建的机构Id
|
||||
db.Queryable<SysOrg>().IgnoreTenant()
|
||||
.Where(u => u.CreateUserId != null)
|
||||
.Select(u => new VSysUserOrg
|
||||
{
|
||||
UserId = u.CreateUserId.Value,
|
||||
OrgId = u.Id
|
||||
}),
|
||||
// 获取用户扩展机构Id
|
||||
db.Queryable<SysUserExtOrg>()
|
||||
.Select(u => new VSysUserOrg
|
||||
{
|
||||
UserId = u.UserId,
|
||||
OrgId = u.OrgId
|
||||
}),
|
||||
// 获取用户角色关联的机构Id
|
||||
db.Queryable<SysRoleOrg>()
|
||||
.InnerJoin<SysUserRole>((u, a) => u.RoleId == a.RoleId)
|
||||
.Select((u, a) => new VSysUserOrg
|
||||
{
|
||||
UserId = a.UserId,
|
||||
OrgId = u.OrgId
|
||||
}),
|
||||
// 获取包含全部数据权限的机构Id
|
||||
db.Queryable<SysOrg>().IgnoreTenant()
|
||||
.FullJoin<SysUserRole>((u, a) => a.SysRole.DataScope == DataScopeEnum.All)
|
||||
.Select((u, a) => new VSysUserOrg
|
||||
{
|
||||
UserId = a.UserId,
|
||||
OrgId = u.Id
|
||||
}),
|
||||
// 超管获取全部机构Id
|
||||
db.Queryable<SysOrg>().IgnoreTenant()
|
||||
.FullJoin<SysUser>((u, a) => a.AccountType == AccountTypeEnum.SuperAdmin)
|
||||
.Select((u, a) => new VSysUserOrg
|
||||
{
|
||||
UserId = a.Id,
|
||||
OrgId = u.Id
|
||||
}))
|
||||
.Where(u => u.OrgId != null && u.OrgId != 0)
|
||||
.ToMappedSqlString();
|
||||
}
|
||||
}
|
||||
@ -326,9 +326,9 @@ public enum ErrorCodeEnum
|
||||
D3001,
|
||||
|
||||
/// <summary>
|
||||
/// 字典类型下面有字典值禁止删除
|
||||
/// 字典编码已存在
|
||||
/// </summary>
|
||||
[ErrorCodeItemMetadata("字典类型下面有字典值禁止删除")]
|
||||
[ErrorCodeItemMetadata("字典编码已存在")]
|
||||
D3002,
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -41,6 +41,13 @@ public class StartHostedService(IServiceScopeFactory serviceScopeFactory) : IHos
|
||||
Console.WriteLine(message);
|
||||
Log.Information(message);
|
||||
|
||||
// 持久化系统接口数据
|
||||
var sysAuthService = serviceScope.ServiceProvider.GetRequiredService<SysAuthService>();
|
||||
await sysAuthService.ResetSysApiInfo();
|
||||
message = $"【启动任务】持久化系统接口数据 {DateTime.Now}";
|
||||
Console.WriteLine(message);
|
||||
Log.Information(message);
|
||||
|
||||
Console.ForegroundColor = originColor;
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
@ -188,7 +188,7 @@ public class DatabaseLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
||||
/// <returns></returns>
|
||||
private LoggingUserInfo GetUserInfo(LoggingMonitorDto loggingMonitor)
|
||||
{
|
||||
var userManger = LazyHelper.GetService<UserManager>();
|
||||
var userManger = LazyHelper.GetService<UserManager>().Value;
|
||||
LoggingUserInfo result = new();
|
||||
if (loggingMonitor.AuthorizationClaims != null)
|
||||
{
|
||||
|
||||
@ -42,7 +42,7 @@ public class ElasticSearchLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
||||
return;
|
||||
|
||||
// 获取当前操作者
|
||||
var userManager = LazyHelper.GetService<UserManager>();
|
||||
var userManager = LazyHelper.GetService<UserManager>().Value;
|
||||
string account = "", realName = "", userId = "", tenantId = "";
|
||||
if (loggingMonitor.authorizationClaims != null)
|
||||
{
|
||||
|
||||
@ -38,7 +38,7 @@ public class HttpLoggingHandler : DelegatingHandler, ITransient
|
||||
if (!enabledLog || attr?.IgnoreLog == true) return await base.SendAsync(request, cancellationToken);
|
||||
|
||||
var stopWatch = Stopwatch.StartNew();
|
||||
var userManger = LazyHelper.GetService<UserManager>();
|
||||
var userManger = LazyHelper.GetService<UserManager>().Value;
|
||||
var urlList = request.RequestUri?.LocalPath.Split("/") ?? [];
|
||||
var sysLogHttp = new SysLogHttp
|
||||
{
|
||||
|
||||
@ -6,6 +6,8 @@
|
||||
|
||||
using Furion.SpecificationDocument;
|
||||
using Lazy.Captcha.Core;
|
||||
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
|
||||
namespace Admin.NET.Core.Service;
|
||||
|
||||
@ -20,7 +22,9 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
private readonly SqlSugarRepository<SysUser> _sysUserRep;
|
||||
private readonly SysConfigService _sysConfigService;
|
||||
private readonly SysCacheService _sysCacheService;
|
||||
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly IApiDescriptionGroupCollectionProvider _apiProvider;
|
||||
private readonly ICaptcha _captcha;
|
||||
private readonly IEventPublisher _eventPublisher;
|
||||
|
||||
@ -28,12 +32,14 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
SqlSugarRepository<SysUser> sysUserRep,
|
||||
SysConfigService sysConfigService,
|
||||
SysCacheService sysCacheService,
|
||||
IApiDescriptionGroupCollectionProvider apiProvider,
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
ICaptcha captcha,
|
||||
IEventPublisher eventPublisher)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_sysUserRep = sysUserRep;
|
||||
_apiProvider = apiProvider;
|
||||
_sysConfigService = sysConfigService;
|
||||
_sysCacheService = sysCacheService;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
@ -288,14 +294,28 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
user.SysPos ??= await db.Queryable<SysPos>().FirstAsync(u => u.Id == user.PosId);
|
||||
user.SysOrg ??= await db.Queryable<SysOrg>().FirstAsync(u => u.Id == user.OrgId);
|
||||
|
||||
var permissions = await LazyHelper.GetService<SysRoleService>().GetUserApiList(user.Id);
|
||||
var unauthorizedPermissions = await LazyHelper.GetService<SysRoleService>().GetUnAuthApiList(user.Id);
|
||||
var orgIds = await LazyHelper.GetService<SysOrgService>().GetUserOrgIdList(user.Id, user.OrgId);
|
||||
var roleIds = await db.Queryable<SysUserRole>().Where(u => u.UserId == user.Id).Select(u => u.RoleId).ToListAsync();
|
||||
var postIds = await db.Queryable<SysUserExtOrg>().Where(u => u.UserId == user.Id).Select(u => u.PosId).ToListAsync() ?? [];
|
||||
var orgIds = await db.Queryable<VSysUserOrg>().Where(u => u.UserId == user.Id).Select(u => u.OrgId).ToListAsync();
|
||||
var roleIds = await db.Queryable<SysRole>().InnerJoinIF<SysUserRole>(user.AccountType != AccountTypeEnum.SuperAdmin, (u, a) => a.RoleId == u.Id && a.UserId == user.Id).Select((u, a) => u.Id).ToListAsync();
|
||||
var posIds = await db.Queryable<SysPos>().InnerJoinIF<SysUserExtOrg>(user.AccountType != AccountTypeEnum.SuperAdmin, (u, a) => a.PosId == u.Id && a.UserId == user.Id).Select((u, a) => u.Id).ToListAsync();
|
||||
posIds = posIds.Concat([user.PosId]).Where(u => u != 0).ToList();
|
||||
|
||||
var maxDataScope = await db.Queryable<SysRole>().Where(u => roleIds.Contains(u.Id)).MinAsync(u => u.DataScope);
|
||||
if (maxDataScope == 0) maxDataScope = DataScopeEnum.Self;
|
||||
postIds.Add(user.PosId);
|
||||
|
||||
var permissions = user.AccountType != AccountTypeEnum.SuperAdmin ? await db.Queryable<VSysPermissions>()
|
||||
.InnerJoin<SysRoleMenu>((u, a) => roleIds.Contains(a.RoleId) && u.MenuId == a.MenuId)
|
||||
.InnerJoin<SysRoleApi>((u, a, b) => u.Permission != b.Route)
|
||||
.Select(u => u.Permission)
|
||||
.ToListAsync()
|
||||
: await db.Queryable<VSysPermissions>().Select(u => u.Permission).ToListAsync();
|
||||
|
||||
var unauthorizedPermissions = user.AccountType != AccountTypeEnum.SuperAdmin ? await db.Union(
|
||||
db.Queryable<SysRoleApi>().Where(u => roleIds.Contains(u.RoleId)).Select(u => u.Route),
|
||||
db.Queryable<VSysPermissions>().Where(u => u.MenuId != 0 && SqlFunc.Subqueryable<SysRoleMenu>()
|
||||
.Where(z => roleIds.Contains(z.RoleId) && z.MenuId == u.MenuId).NotAny())
|
||||
.Select(u => u.Permission)
|
||||
).ToListAsync()
|
||||
: [];
|
||||
|
||||
// 缓存用户Session
|
||||
_userManager.SetSession(new()
|
||||
@ -317,11 +337,11 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
LoginMode = loginMode,
|
||||
TokenVersion = user.TokenVersion,
|
||||
OrgIds = orgIds,
|
||||
PosIds = postIds,
|
||||
PosIds = posIds,
|
||||
RoleIds = roleIds,
|
||||
Permissions = permissions,
|
||||
UnauthorizedPermissions = unauthorizedPermissions,
|
||||
AppPermissions = loginMode == LoginModeEnum.APP ? LazyHelper.GetService<SysCommonService>().GetAppApiList() : null,
|
||||
AppPermissions = loginMode == LoginModeEnum.APP ? LazyHelper.GetService<SysCommonService>().Value.GetAppApiList() : null,
|
||||
MaxDataScope = user.AccountType == AccountTypeEnum.SuperAdmin ? DataScopeEnum.All : maxDataScope,
|
||||
ExtProps = App.GetServices<IUserSessionExtProps>().SelectMany(u => u.GetInitExtProps(user)).ToDictionary(u => u.Key, u => u.Value)
|
||||
});
|
||||
@ -485,4 +505,59 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
var user = await _sysUserRep.AsQueryable().IgnoreTenant().Includes(u => u.SysOrg).FirstAsync(u => u.Id == userId);
|
||||
await SetUserSession(user, CommonHelper.IsMobile(_httpContextAccessor.HttpContext?.Request.Headers.UserAgent ?? "") ? LoginModeEnum.APP : LoginModeEnum.PC);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置系统接口信息
|
||||
/// </summary>
|
||||
[NonAction]
|
||||
public async Task ResetSysApiInfo()
|
||||
{
|
||||
var id = SqlSugarConst.DefaultTenantId + 10000000000;
|
||||
var apiList = new List<SysApiInfo>();
|
||||
var apiDescriptionGroups = _apiProvider.ApiDescriptionGroups.Items;
|
||||
foreach (ApiDescriptionGroup group in apiDescriptionGroups)
|
||||
{
|
||||
foreach (var action in group.Items)
|
||||
{
|
||||
// 控制器信息
|
||||
if (action.ActionDescriptor is not ControllerActionDescriptor actionDescriptor) continue;
|
||||
var apiDescription = actionDescriptor.ControllerTypeInfo.GetCustomAttribute<ApiDescriptionSettingsAttribute>(true);
|
||||
|
||||
// 路由
|
||||
var route = action.RelativePath!.Contains('{') ? action.RelativePath[..(action.RelativePath.IndexOf('{') - 1)] : action.RelativePath; // 去掉路由参数
|
||||
route = route[(route.IndexOf('/') + 1)..]; // 去掉路由前缀
|
||||
|
||||
// 获取分组名称和接口名称
|
||||
string groupName = null, displayText = null;
|
||||
foreach (var attr in action.ActionDescriptor.EndpointMetadata)
|
||||
{
|
||||
switch (attr)
|
||||
{
|
||||
case ApiDescriptionSettingsAttribute settings:
|
||||
if (!string.IsNullOrWhiteSpace(settings.GroupName)) groupName = settings.GroupName;
|
||||
break;
|
||||
case DisplayNameAttribute displayName:
|
||||
if (!string.IsNullOrWhiteSpace(displayName.DisplayName)) displayText = displayName.DisplayName;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
apiList.Add(new SysApiInfo
|
||||
{
|
||||
Id = id++,
|
||||
Route = route,
|
||||
HttpMethod = action.HttpMethod,
|
||||
Order = apiDescription?.Order ?? 0,
|
||||
Action = actionDescriptor.ActionName,
|
||||
Name = actionDescriptor.ControllerName,
|
||||
GroupName = groupName ?? group.GroupName,
|
||||
IsAppApi = actionDescriptor.ControllerTypeInfo.GetCustomAttribute<AppApiDescriptionAttribute>(true) != null,
|
||||
Desc = displayText ?? (string.IsNullOrWhiteSpace(apiDescription?.Description) ? actionDescriptor.ControllerName : apiDescription?.Description),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await _sysUserRep.Context.Deleteable<SysApiInfo>().ExecuteCommandAsync();
|
||||
await _sysUserRep.Context.Insertable(apiList).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
@ -431,7 +431,8 @@ public class SysCacheService : IDynamicApiController, ISingleton
|
||||
public void Clear()
|
||||
{
|
||||
// 超管用户操作,清空所有缓存
|
||||
if (LazyHelper.GetService<UserManager>().SuperAdmin)
|
||||
var userManager = App.GetService<UserManager>();
|
||||
if (userManager.SuperAdmin)
|
||||
{
|
||||
_cacheProvider.Cache.Clear();
|
||||
Cache.Default.Clear();
|
||||
|
||||
@ -61,7 +61,10 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[DisplayName("增加字典值")]
|
||||
public async Task AddDictData(AddDictDataInput input)
|
||||
{
|
||||
var isExist = await _sysDictDataRep.IsAnyAsync(u => u.Value == input.Value && u.DictTypeId == input.DictTypeId);
|
||||
var isExist = await _sysDictDataRep.IsAnyAsync(u => u.Code != null && u.Code == input.Code && u.DictTypeId == input.DictTypeId);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D3002);
|
||||
|
||||
isExist = await _sysDictDataRep.IsAnyAsync(u => u.Value != null && u.Value == input.Value && u.DictTypeId == input.DictTypeId);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D3003);
|
||||
|
||||
var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(input.DictTypeId);
|
||||
@ -86,7 +89,10 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
var isExist = await _sysDictDataRep.IsAnyAsync(u => u.Id == input.Id);
|
||||
if (!isExist) throw Oops.Oh(ErrorCodeEnum.D3004);
|
||||
|
||||
isExist = await _sysDictDataRep.IsAnyAsync(u => u.Value == input.Value && u.DictTypeId == input.DictTypeId && u.Id != input.Id);
|
||||
isExist = await _sysDictDataRep.IsAnyAsync(u => u.Code != null && u.Code == input.Code && u.DictTypeId == input.DictTypeId && u.Id != input.Id);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D3002);
|
||||
|
||||
isExist = await _sysDictDataRep.IsAnyAsync(u => u.Value != null && u.Value == input.Value && u.DictTypeId == input.DictTypeId && u.Id != input.Id);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D3003);
|
||||
|
||||
var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(input.DictTypeId);
|
||||
|
||||
@ -211,7 +211,7 @@ public class UserManager(
|
||||
if (long.TryParse(userId.ToString(), out long tempId)) userId = tempId;
|
||||
else return null;
|
||||
}
|
||||
LazyHelper.GetService<SysAuthService>().RefreshSession(userId).GetAwaiter().GetResult();
|
||||
LazyHelper.GetService<SysAuthService>().Value.RefreshSession(userId).GetAwaiter().GetResult();
|
||||
}
|
||||
return sysCacheService.Get<UserSessionDao>(CacheConst.KeyUserSession + userId);
|
||||
}
|
||||
|
||||
@ -584,7 +584,7 @@ public static class SqlSugarExtension
|
||||
|
||||
// 使用线程安全的延迟初始化服务实例获取文本缩写
|
||||
var abbrValue = LazyHelper.GetService<SysCommonService>()
|
||||
.GetNameAbbr(new() { Text = value, All = attribute.SaveFullAbbr })
|
||||
.Value.GetNameAbbr(new() { Text = value, All = attribute.SaveFullAbbr })
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
entityInfo.SetValue(abbrValue);
|
||||
@ -634,7 +634,7 @@ public static class SqlSugarExtension
|
||||
|
||||
// 使用线程安全的延迟初始化服务实例获取流水号
|
||||
var serial = LazyHelper.GetService<SysSerialService>()
|
||||
.NextSeqNo(attribute.Type, attribute.IsGlobal)
|
||||
.Value.NextSeqNo(attribute.Type, attribute.IsGlobal)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
entityInfo.SetValue(serial);
|
||||
|
||||
@ -59,7 +59,8 @@ public static class SqlSugarFilter
|
||||
if (string.IsNullOrWhiteSpace(userId)) return;
|
||||
|
||||
// 获取用户session
|
||||
var session = LazyHelper.GetService<UserManager>().GetSessionOrRefresh(userId);
|
||||
var userManager = LazyHelper.GetService<UserManager>().Value;
|
||||
var session = userManager.GetSessionOrRefresh(userId);
|
||||
if (session == null) return;
|
||||
|
||||
// 配置用户机构集合缓存
|
||||
@ -113,7 +114,8 @@ public static class SqlSugarFilter
|
||||
if (string.IsNullOrWhiteSpace(userId)) return maxDataScope;
|
||||
|
||||
// 获取用户session
|
||||
var session = LazyHelper.GetService<UserManager>().GetSessionOrRefresh(userId);
|
||||
var userManager = LazyHelper.GetService<UserManager>().Value;
|
||||
var session = userManager.GetSessionOrRefresh(userId);
|
||||
if (session == null) return (int)DataScopeEnum.Self;
|
||||
|
||||
// 获取用户最大数据范围---仅本人数据
|
||||
|
||||
@ -37,7 +37,8 @@ public class SqlSugarRepository<T> : SimpleClient<T>, ISqlSugarRepository<T> whe
|
||||
return;
|
||||
|
||||
// 若未贴任何表特性或当前未登录或是默认租户Id,则返回默认库连接
|
||||
var tenantId = LazyHelper.GetService<UserManager>().TenantId?.ToString();
|
||||
var userManager = LazyHelper.GetService<UserManager>().Value;
|
||||
var tenantId = userManager.TenantId?.ToString();
|
||||
if (string.IsNullOrWhiteSpace(tenantId) || tenantId == SqlSugarConst.MainConfigId) return;
|
||||
|
||||
// 根据租户Id切换库连接, 为空则返回默认库连接
|
||||
|
||||
@ -215,7 +215,7 @@ public static class SqlSugarSetup
|
||||
Log.Warning(log);
|
||||
};
|
||||
|
||||
var userManager = LazyHelper.GetService<UserManager>();
|
||||
var userManager = LazyHelper.GetService<UserManager>().Value;
|
||||
// 数据审计
|
||||
dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
|
||||
{
|
||||
|
||||
@ -6,18 +6,28 @@
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 懒加载帮助类
|
||||
/// </summary>
|
||||
public class LazyHelper
|
||||
{
|
||||
private static readonly Dictionary<Type, Lazy<object>> _cache = new();
|
||||
private static readonly ConcurrentDictionary<Type, dynamic> Cache = new();
|
||||
|
||||
/// <summary>
|
||||
/// 获取服务
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T GetService<T>() where T : class
|
||||
public static Lazy<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;
|
||||
try
|
||||
{
|
||||
return Cache.GetOrAdd(typeof(T), _ => new Lazy<T>(() => App.GetService<T>()));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Cache.Remove(typeof(T), out _);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -91,7 +91,8 @@ namespace Admin.NET.Web.Core
|
||||
}
|
||||
|
||||
// 验证租户有效期
|
||||
var tenantId = LazyHelper.GetService<UserManager>().TenantId?.ToString();
|
||||
var userManager = LazyHelper.GetService<UserManager>().Value;
|
||||
var tenantId = userManager.TenantId?.ToString();
|
||||
if (!string.IsNullOrWhiteSpace(tenantId))
|
||||
{
|
||||
var tenant = sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.FirstOrDefault(u => u.Id == long.Parse(tenantId));
|
||||
@ -117,7 +118,7 @@ namespace Admin.NET.Web.Core
|
||||
/// <returns></returns>
|
||||
private static async Task<bool> CheckAuthorizeAsync(DefaultHttpContext httpContext)
|
||||
{
|
||||
var userManager = LazyHelper.GetService<UserManager>();
|
||||
var userManager = LazyHelper.GetService<UserManager>().Value;
|
||||
|
||||
// 排除超管权限判断
|
||||
if (userManager?.AccountType == AccountTypeEnum.SuperAdmin) return true;
|
||||
|
||||
@ -61,7 +61,7 @@ public class SuperApiAop : DefaultSuperApiAop
|
||||
var paths = api?.Url?.Split('/');
|
||||
var actionName = paths?[^1];
|
||||
|
||||
var userManager = LazyHelper.GetService<UserManager>();
|
||||
var userManager = LazyHelper.GetService<UserManager>().Value;
|
||||
var apiInfo = new
|
||||
{
|
||||
requestUrl = api?.Url,
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
|
||||
<el-form-item label="编码">
|
||||
<el-form-item label="编码" prop="code">
|
||||
<el-input v-model="state.ruleForm.code" placeholder="编码" clearable />
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
@ -57,10 +57,10 @@
|
||||
</el-col>
|
||||
<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
|
||||
<el-form-item label="拓展数据">
|
||||
<div style="display: flex; flex-direction: column; gap: 10px; width: 100%">
|
||||
<el-input v-model="state.ruleForm.extData" placeholder="请输入拓展数据" clearable type="textarea" :rows="12" style="width: 100%" />
|
||||
<div style="width: 100%">
|
||||
<el-button type="primary" @click="formatJSON" style="width: 100%">格式化JSON</el-button>
|
||||
<div style="display: flex; flex-direction: column; gap: 10px; width: 100%;">
|
||||
<el-input v-model="state.ruleForm.extData" placeholder="请输入拓展数据" clearable type="textarea" :rows="12" style="width: 100%;" />
|
||||
<div style="width: 100%;">
|
||||
<el-button type="primary" @click="formatJSON" style="width: 100%;">格式化JSON</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</el-form-item>
|
||||
@ -156,4 +156,4 @@ const formatJSON = () => {
|
||||
|
||||
// 导出对象
|
||||
defineExpose({ openDialog });
|
||||
</script>
|
||||
</script>
|
||||
Loading…
Reference in New Issue
Block a user