🍒 feat(Core): 添加系统接口信息表并实现接口信息持久化
- 新增权限标识视图 - 新增用户机构权限视图 - 引用视图提高查询效率
This commit is contained in:
parent
002bc90c81
commit
a91dd7f134
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();
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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,16 +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 sysRoleService = LazyHelper.GetService<SysRoleService>().Value;
|
||||
var sysOrgService = LazyHelper.GetService<SysOrgService>().Value;
|
||||
var permissions = await sysRoleService.GetUserApiList(user.Id);
|
||||
var unauthorizedPermissions = await sysRoleService.GetUnAuthApiList(user.Id);
|
||||
var orgIds = await 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()
|
||||
@ -319,7 +337,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
LoginMode = loginMode,
|
||||
TokenVersion = user.TokenVersion,
|
||||
OrgIds = orgIds,
|
||||
PosIds = postIds,
|
||||
PosIds = posIds,
|
||||
RoleIds = roleIds,
|
||||
Permissions = permissions,
|
||||
UnauthorizedPermissions = unauthorizedPermissions,
|
||||
@ -487,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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user