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>
<param name="args"></param>
</member>
<member name="M:LFlow.Base.Program.ConfigureSqlSugar(Microsoft.Extensions.DependencyInjection.IServiceCollection)">
<summary>

View File

@ -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;
/// <summary>
/// 权限中间件
/// </summary>
public class PermissionMiddleware : ILFlowMiddleware
public class PermissionMiddleware(ISelfCache selfCache ) : ILFlowMiddleware
{
/// <summary>
/// 优先级
@ -28,28 +29,34 @@ public class PermissionMiddleware : ILFlowMiddleware
/// <exception cref="NotImplementedException"></exception>
public async Task RunAsync(HttpContext context, Func<Task> next)
{
var progName = context.GetRouteData()?.Values["controller"]?.ToString();
var progAction = context.GetRouteData()?.Values["action"]?.ToString();
if (progName != null)
{
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)
{
// var progName = context.GetRouteData()?.Values["controller"]?.ToString();
// var progAction = context.GetRouteData()?.Values["action"]?.ToString();
// if (progName != null)
// {
// 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 obj = selfCache.GetAsync(token!);
// }else{
// await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("未登录!", 100500)));
// }
// }
// }
// // 预检请求
// if (context.Request.Method == "OPTIONS")
// {
await next();
}
else
{
//TODO 从缓存中根据Token获取用户信息并判断是否有权限
await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("无权限!", 100501)));
}
}
// 预检请求
if (context.Request.Method == "OPTIONS")
{
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>
{
/// <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);
/// <summary>
/// 根据ID获取权限信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<PermissionDto> GetPermissionAsync(string id);
/// <summary>
/// 添加一项权限
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<PermissionDto> AddPermissionAsync(PermissionDto model);
/// <summary>
/// 更新权限内容
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<PermissionDto> UpdatePermissionAsync(PermissionDto model);
/// <summary>
/// 删除一项权限
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<int> DeletePermissionAsync(string id);
/// <summary>
/// 获取程序权限列表
@ -16,8 +43,15 @@ namespace LFlow.Permission.Service
/// <param name="progID"></param>
/// <returns></returns>
Task<List<PermissionDto>> GetProgPerminssionListAsync(string progID);
/// <summary>
/// 获取所有权限项
/// </summary>
/// <returns></returns>
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>>());
}
public Task<List<PermissionDto>> GetUserPermissions()
{
throw new NotImplementedException();
}
/// <summary>
/// 更新权限项
/// </summary>

View File

@ -38,7 +38,9 @@ namespace LFlow.UserManagement.Service
// loginedUser.Token = token;
cacher.SetAsync(token, loginedUser, TimeSpan.FromHours(2));
var result = loginedUser.MapTo<UserDto>();
if(result != null){
result.Token = token;
}
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.Permission.Service;
using LFlow.UserManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json;
@ -11,7 +15,7 @@ namespace LFlow.UserManagement
/// 用户管理中间件
/// </summary>
/// <param name="logger"></param>
public class UserMiddleware(ILogger logger) : ILFlowMiddleware
public class UserMiddleware(ILogger logger,ISelfCache selfCache) : ILFlowMiddleware
{
/// <summary>
/// 优先级
@ -24,16 +28,36 @@ namespace LFlow.UserManagement
/// <param name="next"></param>
public async Task RunAsync(Microsoft.AspNetCore.Http.HttpContext context, Func<Task> 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<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
// {
// await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("无权限!", 100501)));
// }
// Do something after
}
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();
// context.Response.StatusCode = StatusCodes.Status200OK;
}
}
}
}

View File

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