From fa160cac8312199a8120975a567e3ad69ffbd29e Mon Sep 17 00:00:00 2001 From: Ling0925 <2449858657a@gmail.com> Date: Wed, 13 Nov 2024 16:23:30 +0800 Subject: [PATCH] =?UTF-8?q?105040=20=E7=94=A8=E6=88=B7=E6=9D=83=E9=99=90?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LFlow.Base/Base.xml | 1 + LFlow.Permission/PermissionMiddleware.cs | 53 +++++++++++-------- .../Service/IPermissionService.cs | 38 ++++++++++++- LFlow.Permission/Service/PermissionService.cs | 5 ++ .../Service/UserManagementService.cs | 4 +- LFlow.UserManagement/UserMiddleware.cs | 46 ++++++++++++---- LFlow.UserManagement/Util/PasswordHelper.cs | 8 +++ 7 files changed, 118 insertions(+), 37 deletions(-) diff --git a/LFlow.Base/Base.xml b/LFlow.Base/Base.xml index a65d9c5..87a4ee5 100644 --- a/LFlow.Base/Base.xml +++ b/LFlow.Base/Base.xml @@ -238,6 +238,7 @@ 入口 + diff --git a/LFlow.Permission/PermissionMiddleware.cs b/LFlow.Permission/PermissionMiddleware.cs index fa21d74..6e74960 100644 --- a/LFlow.Permission/PermissionMiddleware.cs +++ b/LFlow.Permission/PermissionMiddleware.cs @@ -1,6 +1,7 @@ using System.Net.Sockets; using LFlow.Base; using LFlow.Base.Utils; +using LFlow.Cache.Interface; using LFlow.Middleware; using LFlow.Permission.Service; using Microsoft.AspNetCore.Http; @@ -12,7 +13,7 @@ namespace LFlow.Permission; /// /// 权限中间件 /// -public class PermissionMiddleware : ILFlowMiddleware +public class PermissionMiddleware(ISelfCache selfCache ) : ILFlowMiddleware { /// /// 优先级 @@ -28,28 +29,34 @@ public class PermissionMiddleware : ILFlowMiddleware /// public async Task RunAsync(HttpContext context, Func next) { - var progName = context.GetRouteData()?.Values["controller"]?.ToString(); - var progAction = context.GetRouteData()?.Values["action"]?.ToString(); - if (progName != null) - { - var service = App.GetService(); - var progPermission = service != null ? await service.GetProgPerminssionListAsync(progName) : null; - var currentPermission = progPermission?.FirstOrDefault(p => p.PermissionAction == progAction); - if (currentPermission == null || currentPermission!.IsPublic) - { - await next(); - } - else - { - //TODO 从缓存中根据Token获取用户信息,并判断是否有权限 - await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult.FailResult("无权限!", 100501))); - } - } - // 预检请求 - if (context.Request.Method == "OPTIONS") - { + // var progName = context.GetRouteData()?.Values["controller"]?.ToString(); + // var progAction = context.GetRouteData()?.Values["action"]?.ToString(); + // if (progName != null) + // { + // var service = App.GetService(); + // var progPermission = service != null ? await service.GetProgPerminssionListAsync(progName) : null; + // var currentPermission = progPermission?.FirstOrDefault(p => p.PermissionAction == progAction); + // if (currentPermission == null || currentPermission!.IsPublic) + // { + // await next(); + // } + // else + // { + // //TODO 从缓存中根据Token获取用户信息,并判断是否有权限 + // await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult.FailResult("无权限!", 100501))); + // var token = context.Request.Cookies["Token"]?.ToString(); + // if(token != null){ + // var obj = selfCache.GetAsync(token!); + // }else{ + // await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult.FailResult("未登录!", 100500))); + // } + // } + // } + // // 预检请求 + // if (context.Request.Method == "OPTIONS") + // { await next(); - // context.Response.StatusCode = StatusCodes.Status200OK; - } + // // context.Response.StatusCode = StatusCodes.Status200OK; + // } } } \ No newline at end of file diff --git a/LFlow.Permission/Service/IPermissionService.cs b/LFlow.Permission/Service/IPermissionService.cs index 650a248..35bb798 100644 --- a/LFlow.Permission/Service/IPermissionService.cs +++ b/LFlow.Permission/Service/IPermissionService.cs @@ -5,10 +5,37 @@ namespace LFlow.Permission.Service { public interface IPermissionService : IService// { + /// + /// 获取权限列表 + /// + /// + /// + /// + /// Task> GetPermissionListAsync(int pageIndex, int pageSize, ref int total); + /// + /// 根据ID获取权限信息 + /// + /// + /// Task GetPermissionAsync(string id); + /// + /// 添加一项权限 + /// + /// + /// Task AddPermissionAsync(PermissionDto model); + /// + /// 更新权限内容 + /// + /// + /// Task UpdatePermissionAsync(PermissionDto model); + /// + /// 删除一项权限 + /// + /// + /// Task DeletePermissionAsync(string id); /// /// 获取程序权限列表 @@ -16,8 +43,15 @@ namespace LFlow.Permission.Service /// /// Task> GetProgPerminssionListAsync(string progID); - - + /// + /// 获取所有权限项 + /// + /// Task> GetPermissions(); + /// + /// 获取用户权限 + /// + /// + Task> GetUserPermissions(); } } diff --git a/LFlow.Permission/Service/PermissionService.cs b/LFlow.Permission/Service/PermissionService.cs index 685d82c..543ac7d 100644 --- a/LFlow.Permission/Service/PermissionService.cs +++ b/LFlow.Permission/Service/PermissionService.cs @@ -112,6 +112,11 @@ namespace LFlow.Permission.Service }).Adapt>()); } + public Task> GetUserPermissions() + { + throw new NotImplementedException(); + } + /// /// 更新权限项 /// diff --git a/LFlow.UserManagement/Service/UserManagementService.cs b/LFlow.UserManagement/Service/UserManagementService.cs index 641424e..b9409b4 100644 --- a/LFlow.UserManagement/Service/UserManagementService.cs +++ b/LFlow.UserManagement/Service/UserManagementService.cs @@ -38,7 +38,9 @@ namespace LFlow.UserManagement.Service // loginedUser.Token = token; cacher.SetAsync(token, loginedUser, TimeSpan.FromHours(2)); var result = loginedUser.MapTo(); - result.Token = token; + if(result != null){ + result.Token = token; + } return result; } diff --git a/LFlow.UserManagement/UserMiddleware.cs b/LFlow.UserManagement/UserMiddleware.cs index 5dd34e0..a5525b7 100644 --- a/LFlow.UserManagement/UserMiddleware.cs +++ b/LFlow.UserManagement/UserMiddleware.cs @@ -1,5 +1,9 @@ -using LFlow.Base.Utils; +using LFlow.Base; +using LFlow.Base.Utils; +using LFlow.Cache.Interface; using LFlow.Middleware; +using LFlow.Permission.Service; +using LFlow.UserManagement.Model; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Newtonsoft.Json; @@ -11,7 +15,7 @@ namespace LFlow.UserManagement /// 用户管理中间件 /// /// - public class UserMiddleware(ILogger logger) : ILFlowMiddleware + public class UserMiddleware(ILogger logger,ISelfCache selfCache) : ILFlowMiddleware { /// /// 优先级 @@ -24,16 +28,36 @@ namespace LFlow.UserManagement /// public async Task RunAsync(Microsoft.AspNetCore.Http.HttpContext context, Func next) { - // Do something before - // var progController = context.GetRouteData()?.Values["controller"]?.ToString(); - // var progAction = context.GetRouteData()?.Values["action"]?.ToString(); - // if (progAction != "ListAll") + var progName = context.GetRouteData()?.Values["controller"]?.ToString(); + var progAction = context.GetRouteData()?.Values["action"]?.ToString(); + if (progName != null) + { + var service = App.GetService(); + var progPermission = service != null ? await service.GetProgPerminssionListAsync(progName) : null; + var currentPermission = progPermission?.FirstOrDefault(p => p.PermissionAction == progAction); + if (currentPermission == null || currentPermission!.IsPublic) + { + await next(); + } + else + { + //TODO 从缓存中根据Token获取用户信息,并判断是否有权限 + await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult.FailResult("无权限!", 100501))); + var token = context.Request.Cookies["Token"]?.ToString(); + if(token != null){ + var user = selfCache.GetAsync(token!); + var userPermissions = service.GetPermissions() + }else{ + await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult.FailResult("未登录!", 100500))); + } + } + } + // 预检请求 + if (context.Request.Method == "OPTIONS") + { await next(); - // else - // { - // await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult.FailResult("无权限!", 100501))); - // } - // Do something after + // context.Response.StatusCode = StatusCodes.Status200OK; + } } } } diff --git a/LFlow.UserManagement/Util/PasswordHelper.cs b/LFlow.UserManagement/Util/PasswordHelper.cs index 11dcfdd..c53da8c 100644 --- a/LFlow.UserManagement/Util/PasswordHelper.cs +++ b/LFlow.UserManagement/Util/PasswordHelper.cs @@ -3,8 +3,16 @@ using System.Text; namespace LFlow.UserManagement.Util { + /// + /// + /// public class PasswordHelper { + /// + /// + /// + /// + /// public static string HashPassword(string password) { byte[] data = Encoding.Default.GetBytes(password);