2024-11-04 10:10:05 +08:00
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
|
using LFlow.Base;
|
|
|
|
|
|
using LFlow.Base.Utils;
|
|
|
|
|
|
using LFlow.Middleware;
|
|
|
|
|
|
using LFlow.Permission.Service;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
|
|
namespace LFlow.Permission;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 权限中间件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class PermissionMiddleware : ILFlowMiddleware
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 优先级
|
|
|
|
|
|
/// </summary>
|
2024-11-04 15:13:58 +08:00
|
|
|
|
public int Priority => 1;
|
2024-11-04 10:10:05 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 执行入口
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <param name="next"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
|
|
|
|
|
await next();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//TODO 从缓存中根据Token获取用户信息,并判断是否有权限
|
|
|
|
|
|
await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("无权限!", 100501)));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-04 15:13:58 +08:00
|
|
|
|
// 预检请求
|
|
|
|
|
|
if (context.Request.Method == "OPTIONS")
|
|
|
|
|
|
{
|
|
|
|
|
|
await next();
|
|
|
|
|
|
// context.Response.StatusCode = StatusCodes.Status200OK;
|
|
|
|
|
|
}
|
2024-11-04 10:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|