HandlerAuthorizeAttribute.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Lottomat.Application.Busines.AuthorizeManage;
  2. using Lottomat.Application.Code;
  3. using Lottomat.Util;
  4. using Lottomat.Util.Extension;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. namespace Lottomat.Application.Admin
  8. {
  9. /// <summary>
  10. /// 版 本 1.0
  11. /// Copyright (c) 2016-2017
  12. /// 创建人:赵轶
  13. /// 日 期:2015.11.9 10:45
  14. /// 描 述:(权限认证+安全)拦截组件
  15. /// </summary>
  16. public class HandlerAuthorizeAttribute : ActionFilterAttribute
  17. {
  18. private PermissionMode _customMode;
  19. /// <summary>默认构造</summary>
  20. /// <param name="Mode">认证模式</param>
  21. public HandlerAuthorizeAttribute(PermissionMode Mode)
  22. {
  23. _customMode = Mode;
  24. }
  25. /// <summary>
  26. /// 权限认证
  27. /// </summary>
  28. /// <param name="filterContext"></param>
  29. public override void OnActionExecuting(ActionExecutingContext filterContext)
  30. {
  31. //是否超级管理员
  32. if (OperatorProvider.Provider.Current().IsSystem)
  33. {
  34. return;
  35. }
  36. //是否忽略
  37. if (_customMode == PermissionMode.Ignore)
  38. {
  39. return;
  40. }
  41. //IP过滤
  42. if (!this.FilterIP())
  43. {
  44. ContentResult Content = new ContentResult();
  45. Content.Content = "<script type='text/javascript'>alert('很抱歉!您当前所在IP被系统拒绝访问!');top.Loading(false);</script>";
  46. filterContext.Result = Content;
  47. return;
  48. }
  49. //时段过滤
  50. if (!this.FilterTime())
  51. {
  52. ContentResult Content = new ContentResult();
  53. Content.Content = "<script type='text/javascript'>alert('很抱歉!系统不允许您在当前时段访问!');top.Loading(false);</script>";
  54. filterContext.Result = Content;
  55. return;
  56. }
  57. //认证执行
  58. if (!this.ActionAuthorize(filterContext))
  59. {
  60. ContentResult Content = new ContentResult();
  61. Content.Content = "<script type='text/javascript'>alert('很抱歉!您的权限不足,访问被拒绝!');top.Loading(false);</script>";
  62. filterContext.Result = Content;
  63. return;
  64. }
  65. }
  66. /// <summary>
  67. /// IP过滤
  68. /// </summary>
  69. /// <returns></returns>
  70. private bool FilterIP()
  71. {
  72. bool isFilterIP = ConfigHelper.GetValue("FilterIP").ToBool();
  73. if (isFilterIP == true)
  74. {
  75. return new FilterIPBLL().FilterIP();
  76. }
  77. return true;
  78. }
  79. /// <summary>
  80. /// 时段过滤
  81. /// </summary>
  82. /// <returns></returns>
  83. private bool FilterTime()
  84. {
  85. bool isFilterIP = ConfigHelper.GetValue("FilterTime").ToBool();
  86. if (isFilterIP == true)
  87. {
  88. return new FilterTimeBLL().FilterTime();
  89. }
  90. return true;
  91. }
  92. /// <summary>
  93. /// 执行权限认证
  94. /// </summary>
  95. /// <param name="filterContext"></param>
  96. /// <returns></returns>
  97. private bool ActionAuthorize(ActionExecutingContext filterContext)
  98. {
  99. string currentUrl = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString();
  100. return new AuthorizeBLL().ActionAuthorize(SystemInfo.CurrentUserId, SystemInfo.CurrentModuleId, currentUrl);
  101. }
  102. }
  103. }