105040 用户权限项

This commit is contained in:
Ling 2024-11-13 16:23:30 +08:00
parent 4c60372f04
commit fa160cac83
7 changed files with 118 additions and 37 deletions

View File

@ -238,6 +238,7 @@
入口 入口
</summary> </summary>
<param name="args"></param> <param name="args"></param>
</member> </member>
<member name="M:LFlow.Base.Program.ConfigureSqlSugar(Microsoft.Extensions.DependencyInjection.IServiceCollection)"> <member name="M:LFlow.Base.Program.ConfigureSqlSugar(Microsoft.Extensions.DependencyInjection.IServiceCollection)">
<summary> <summary>

View File

@ -1,6 +1,7 @@
using System.Net.Sockets; using System.Net.Sockets;
using LFlow.Base; using LFlow.Base;
using LFlow.Base.Utils; using LFlow.Base.Utils;
using LFlow.Cache.Interface;
using LFlow.Middleware; using LFlow.Middleware;
using LFlow.Permission.Service; using LFlow.Permission.Service;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -12,7 +13,7 @@ namespace LFlow.Permission;
/// <summary> /// <summary>
/// 权限中间件 /// 权限中间件
/// </summary> /// </summary>
public class PermissionMiddleware : ILFlowMiddleware public class PermissionMiddleware(ISelfCache selfCache ) : ILFlowMiddleware
{ {
/// <summary> /// <summary>
/// 优先级 /// 优先级
@ -28,28 +29,34 @@ public class PermissionMiddleware : ILFlowMiddleware
/// <exception cref="NotImplementedException"></exception> /// <exception cref="NotImplementedException"></exception>
public async Task RunAsync(HttpContext context, Func<Task> next) public async Task RunAsync(HttpContext context, Func<Task> next)
{ {
var progName = context.GetRouteData()?.Values["controller"]?.ToString(); // var progName = context.GetRouteData()?.Values["controller"]?.ToString();
var progAction = context.GetRouteData()?.Values["action"]?.ToString(); // var progAction = context.GetRouteData()?.Values["action"]?.ToString();
if (progName != null) // if (progName != null)
{ // {
var service = App.GetService<IPermissionService>(); // var service = App.GetService<IPermissionService>();
var progPermission = service != null ? await service.GetProgPerminssionListAsync(progName) : null; // var progPermission = service != null ? await service.GetProgPerminssionListAsync(progName) : null;
var currentPermission = progPermission?.FirstOrDefault(p => p.PermissionAction == progAction); // var currentPermission = progPermission?.FirstOrDefault(p => p.PermissionAction == progAction);
if (currentPermission == null || currentPermission!.IsPublic) // if (currentPermission == null || currentPermission!.IsPublic)
{ // {
await next(); // await next();
} // }
else // else
{ // {
//TODO 从缓存中根据Token获取用户信息并判断是否有权限 // //TODO 从缓存中根据Token获取用户信息并判断是否有权限
await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("无权限!", 100501))); // await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("无权限!", 100501)));
} // var token = context.Request.Cookies["Token"]?.ToString();
} // if(token != null){
// 预检请求 // var obj = selfCache.GetAsync(token!);
if (context.Request.Method == "OPTIONS") // }else{
{ // await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("未登录!", 100500)));
// }
// }
// }
// // 预检请求
// if (context.Request.Method == "OPTIONS")
// {
await next(); await next();
// context.Response.StatusCode = StatusCodes.Status200OK; // // context.Response.StatusCode = StatusCodes.Status200OK;
} // }
} }
} }

View File

@ -5,10 +5,37 @@ namespace LFlow.Permission.Service
{ {
public interface IPermissionService : IService//<VersionDto> public interface IPermissionService : IService//<VersionDto>
{ {
/// <summary>
/// 获取权限列表
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="total"></param>
/// <returns></returns>
Task<List<PermissionDto>> GetPermissionListAsync(int pageIndex, int pageSize, ref int total); Task<List<PermissionDto>> GetPermissionListAsync(int pageIndex, int pageSize, ref int total);
/// <summary>
/// 根据ID获取权限信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<PermissionDto> GetPermissionAsync(string id); Task<PermissionDto> GetPermissionAsync(string id);
/// <summary>
/// 添加一项权限
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<PermissionDto> AddPermissionAsync(PermissionDto model); Task<PermissionDto> AddPermissionAsync(PermissionDto model);
/// <summary>
/// 更新权限内容
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<PermissionDto> UpdatePermissionAsync(PermissionDto model); Task<PermissionDto> UpdatePermissionAsync(PermissionDto model);
/// <summary>
/// 删除一项权限
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<int> DeletePermissionAsync(string id); Task<int> DeletePermissionAsync(string id);
/// <summary> /// <summary>
/// 获取程序权限列表 /// 获取程序权限列表
@ -16,8 +43,15 @@ namespace LFlow.Permission.Service
/// <param name="progID"></param> /// <param name="progID"></param>
/// <returns></returns> /// <returns></returns>
Task<List<PermissionDto>> GetProgPerminssionListAsync(string progID); Task<List<PermissionDto>> GetProgPerminssionListAsync(string progID);
/// <summary>
/// 获取所有权限项
/// </summary>
/// <returns></returns>
Task<List<PermissionDto>> GetPermissions(); Task<List<PermissionDto>> GetPermissions();
/// <summary>
/// 获取用户权限
/// </summary>
/// <returns></returns>
Task<List<PermissionDto>> GetUserPermissions();
} }
} }

View File

@ -112,6 +112,11 @@ namespace LFlow.Permission.Service
}).Adapt<List<PermissionDto>>()); }).Adapt<List<PermissionDto>>());
} }
public Task<List<PermissionDto>> GetUserPermissions()
{
throw new NotImplementedException();
}
/// <summary> /// <summary>
/// 更新权限项 /// 更新权限项
/// </summary> /// </summary>

View File

@ -38,7 +38,9 @@ namespace LFlow.UserManagement.Service
// loginedUser.Token = token; // loginedUser.Token = token;
cacher.SetAsync(token, loginedUser, TimeSpan.FromHours(2)); cacher.SetAsync(token, loginedUser, TimeSpan.FromHours(2));
var result = loginedUser.MapTo<UserDto>(); var result = loginedUser.MapTo<UserDto>();
result.Token = token; if(result != null){
result.Token = token;
}
return result; return result;
} }

View File

@ -1,5 +1,9 @@
using LFlow.Base.Utils; using LFlow.Base;
using LFlow.Base.Utils;
using LFlow.Cache.Interface;
using LFlow.Middleware; using LFlow.Middleware;
using LFlow.Permission.Service;
using LFlow.UserManagement.Model;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -11,7 +15,7 @@ namespace LFlow.UserManagement
/// 用户管理中间件 /// 用户管理中间件
/// </summary> /// </summary>
/// <param name="logger"></param> /// <param name="logger"></param>
public class UserMiddleware(ILogger logger) : ILFlowMiddleware public class UserMiddleware(ILogger logger,ISelfCache selfCache) : ILFlowMiddleware
{ {
/// <summary> /// <summary>
/// 优先级 /// 优先级
@ -24,16 +28,36 @@ namespace LFlow.UserManagement
/// <param name="next"></param> /// <param name="next"></param>
public async Task RunAsync(Microsoft.AspNetCore.Http.HttpContext context, Func<Task> next) public async Task RunAsync(Microsoft.AspNetCore.Http.HttpContext context, Func<Task> next)
{ {
// Do something before var progName = context.GetRouteData()?.Values["controller"]?.ToString();
// var progController = context.GetRouteData()?.Values["controller"]?.ToString(); var progAction = context.GetRouteData()?.Values["action"]?.ToString();
// var progAction = context.GetRouteData()?.Values["action"]?.ToString(); if (progName != null)
// if (progAction != "ListAll") {
var service = App.GetService<IPermissionService>();
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<object>.FailResult("无权限!", 100501)));
var token = context.Request.Cookies["Token"]?.ToString();
if(token != null){
var user = selfCache.GetAsync<UserModel>(token!);
var userPermissions = service.GetPermissions()
}else{
await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("未登录!", 100500)));
}
}
}
// 预检请求
if (context.Request.Method == "OPTIONS")
{
await next(); await next();
// else // context.Response.StatusCode = StatusCodes.Status200OK;
// { }
// await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("无权限!", 100501)));
// }
// Do something after
} }
} }
} }

View File

@ -3,8 +3,16 @@ using System.Text;
namespace LFlow.UserManagement.Util namespace LFlow.UserManagement.Util
{ {
/// <summary>
///
/// </summary>
public class PasswordHelper public class PasswordHelper
{ {
/// <summary>
///
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static string HashPassword(string password) public static string HashPassword(string password)
{ {
byte[] data = Encoding.Default.GetBytes(password); byte[] data = Encoding.Default.GetBytes(password);