using Base;
using Infrastructure;
using Infrastructure.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Model;
using Model.Base;
using Services;
using Util;
namespace Middleware
{
///
/// API授权判断
///
public class ActionPermissionFilter : ActionFilterAttribute//, IAsyncActionFilter
{
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
///
/// 权限字符串,例如 system:user:view
///
public string Permission { get; set; } = string.Empty;
///
/// 角色字符串,例如 common,admin
///
public string RolePermi { get; set; } = string.Empty;
private bool HasPermi { get; set; }
public ActionPermissionFilter() { }
public ActionPermissionFilter(string permission)
{
Permission = permission;
HasPermi = !string.IsNullOrEmpty(Permission);
}
///
/// 执行Action前校验是否有权限访问
///
///
///
///
public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
// TokenModel info = JwtUtil.GetLoginUser(context.HttpContext);
// if (info != null && info?.userId > 0)
// {
// long userId = info.userId;
// List perms = CacheService.GetUserPerms(GlobalConstant.UserPermKEY + userId);
// List rolePerms = info.RoleIds;
// if (perms == null)
// {
// var sysPermissionService = App.GetService();
// perms = sysPermissionService.List();
// CacheService.SetUserPerms(GlobalConstant.UserPermKEY + userId, perms);
// }
// if (perms.Exists(f => f.Equals(GlobalConstant.AdminPerm)))
// {
// HasPermi = true;
// }
// else if (rolePerms.Exists(f => f.Equals(GlobalConstant.AdminRole)))
// {
// HasPermi = true;
// }
// else if (!string.IsNullOrEmpty(Permission))
// {
// HasPermi = perms.Exists(f => f.ToLower() == Permission.ToLower());
// }
// if (!HasPermi && !string.IsNullOrEmpty(RolePermi))
// {
// HasPermi = info.RoleIds.Contains(RolePermi);
// }
// var url = context.HttpContext.Request.Path;
// if (!HasPermi && !Permission.Equals("common"))
// {
// logger.Info($"用户{info.username}没有权限访问{url},当前权限[{Permission}]");
// JsonResult result = new(new ApiResult((int)ResultCode.FORBIDDEN, $"你当前没有权限访问,请联系管理员", url))
// {
// ContentType = "application/json",
// };
// context.HttpContext.Response.StatusCode = 403;
// context.Result = result;
// }
// }
return base.OnActionExecutionAsync(context, next);
}
}
}