ActionPermissionFilter.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Base;
  2. using Infrastructure;
  3. using Infrastructure.Model;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.Filters;
  6. using Model;
  7. using Model.Base;
  8. using Services;
  9. using Util;
  10. namespace Middleware
  11. {
  12. /// <summary>
  13. /// API授权判断
  14. /// </summary>
  15. public class ActionPermissionFilter : ActionFilterAttribute//, IAsyncActionFilter
  16. {
  17. private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  18. /// <summary>
  19. /// 权限字符串,例如 system:user:view
  20. /// </summary>
  21. public string Permission { get; set; } = string.Empty;
  22. /// <summary>
  23. /// 角色字符串,例如 common,admin
  24. /// </summary>
  25. public string RolePermi { get; set; } = string.Empty;
  26. private bool HasPermi { get; set; }
  27. public ActionPermissionFilter() { }
  28. public ActionPermissionFilter(string permission)
  29. {
  30. Permission = permission;
  31. HasPermi = !string.IsNullOrEmpty(Permission);
  32. }
  33. /// <summary>
  34. /// 执行Action前校验是否有权限访问
  35. /// </summary>
  36. /// <param name="context"></param>
  37. /// <param name="next"></param>
  38. /// <returns></returns>
  39. public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
  40. {
  41. // TokenModel info = JwtUtil.GetLoginUser(context.HttpContext);
  42. // if (info != null && info?.userId > 0)
  43. // {
  44. // long userId = info.userId;
  45. // List<string> perms = CacheService.GetUserPerms(GlobalConstant.UserPermKEY + userId);
  46. // List<string> rolePerms = info.RoleIds;
  47. // if (perms == null)
  48. // {
  49. // var sysPermissionService = App.GetService<ISysPermissionService>();
  50. // perms = sysPermissionService.List();
  51. // CacheService.SetUserPerms(GlobalConstant.UserPermKEY + userId, perms);
  52. // }
  53. // if (perms.Exists(f => f.Equals(GlobalConstant.AdminPerm)))
  54. // {
  55. // HasPermi = true;
  56. // }
  57. // else if (rolePerms.Exists(f => f.Equals(GlobalConstant.AdminRole)))
  58. // {
  59. // HasPermi = true;
  60. // }
  61. // else if (!string.IsNullOrEmpty(Permission))
  62. // {
  63. // HasPermi = perms.Exists(f => f.ToLower() == Permission.ToLower());
  64. // }
  65. // if (!HasPermi && !string.IsNullOrEmpty(RolePermi))
  66. // {
  67. // HasPermi = info.RoleIds.Contains(RolePermi);
  68. // }
  69. // var url = context.HttpContext.Request.Path;
  70. // if (!HasPermi && !Permission.Equals("common"))
  71. // {
  72. // logger.Info($"用户{info.username}没有权限访问{url},当前权限[{Permission}]");
  73. // JsonResult result = new(new ApiResult((int)ResultCode.FORBIDDEN, $"你当前没有权限访问,请联系管理员", url))
  74. // {
  75. // ContentType = "application/json",
  76. // };
  77. // context.HttpContext.Response.StatusCode = 403;
  78. // context.Result = result;
  79. // }
  80. // }
  81. return base.OnActionExecutionAsync(context, next);
  82. }
  83. }
  84. }