FunctionAuthorizeAttribute.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Autofac;
  2. using GreenTree.Nachtragsmanagement.Core;
  3. using GreenTree.Nachtragsmanagement.Core.Authentication;
  4. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  5. using GreenTree.Nachtragsmanagement.Services.User;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web;
  12. using System.Web.Mvc;
  13. namespace GreenTree.Nachtragsmanagement.Web.Framework.Authorization
  14. {
  15. public class FunctionAuthorizeAttribute : AuthorizeAttribute
  16. {
  17. #region Fields
  18. private readonly IAuthenticationService _authenticationService;
  19. private readonly IUserHelper _userHelper;
  20. private readonly IUserService _userService;
  21. private readonly string[] _allowedFunctions;
  22. private readonly bool _showNotAuthorized;
  23. #endregion
  24. /// <summary>
  25. /// Initializes a new instance of the FunctionAuthorizeAttribute class
  26. /// </summary>
  27. /// <param name="showNotAuthorized">Determines if a NotAuthorized message or a login redirection is made.</param>
  28. /// <param name="functions">The functions needed.</param>
  29. public FunctionAuthorizeAttribute(bool showNotAuthorized, params string[] functions)
  30. {
  31. _showNotAuthorized = showNotAuthorized;
  32. _allowedFunctions = functions;
  33. _authenticationService = Singleton<IContainer>.Instance.Resolve<IAuthenticationService>();
  34. _userHelper = Singleton<IContainer>.Instance.Resolve<IUserHelper>();
  35. _userService = Singleton<IContainer>.Instance.Resolve<IUserService>();
  36. }
  37. /// <summary>
  38. /// Core authorization
  39. /// </summary>
  40. /// <param name="httpContext">Current HttpContext.</param>
  41. /// <returns>Valid access.</returns>
  42. protected override bool AuthorizeCore(HttpContextBase httpContext)
  43. {
  44. var cookieUser = _userHelper.FromCookiesOrSession();
  45. if (cookieUser == null)
  46. {
  47. cookieUser = CheckWindowsAuthentication(httpContext);
  48. if (cookieUser == null)
  49. return false;
  50. }
  51. if (!_allowedFunctions.Any())
  52. return true;
  53. var dbUser = _userService.GetUserById(cookieUser.Id);
  54. var role = (cookieUser != null && cookieUser.CurrentRole != null)
  55. ? _userService.GetRoleById(cookieUser.CurrentRole.Id)
  56. : null;
  57. if (role == null)
  58. dbUser.CurrentRole = dbUser.Roles
  59. .First(r1 => r1.Level == dbUser.Roles.Max(r2 => r2.Level));
  60. else
  61. dbUser.CurrentRole = role;
  62. _userHelper.ToCookiesAndSession(dbUser, DateTime.Now.AddHours(8), true);
  63. foreach (var function in dbUser.CurrentRole.Functions)
  64. {
  65. var allowed = _allowedFunctions.Contains(function.Name);
  66. if (allowed)
  67. return true;
  68. }
  69. return false;
  70. }
  71. /// <summary>
  72. /// Handle not authorized access
  73. /// </summary>
  74. /// <param name="filterContext">Current filterContext.</param>
  75. protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
  76. {
  77. if (!_showNotAuthorized)
  78. filterContext.Result = new RedirectResult("~/login");
  79. else
  80. filterContext.Result = new RedirectResult("~/global/notauthorized");
  81. }
  82. #region Windows authentication
  83. /// <summary>
  84. /// Checks for Windows SSO authentication
  85. /// </summary>
  86. /// <param name="httpContext">Current HttpContext.</param>
  87. private User CheckWindowsAuthentication(HttpContextBase httpContext)
  88. {
  89. if (httpContext.User == null || String.IsNullOrEmpty(httpContext.User.Identity.Name)) return null;
  90. var username = httpContext.User.Identity.Name.Split('\\').Length > 1
  91. ? httpContext.User.Identity.Name.Split('\\')[1]
  92. : httpContext.User.Identity.Name;
  93. var user = _userService.GetUserByCustomNumber(username);
  94. if (user == null)
  95. return null;
  96. user.CurrentRole = user.Roles.First(r1 => r1.Level == user.Roles.Max(r2 => r2.Level));
  97. _userHelper.ToCookiesAndSession(user, DateTime.Now.AddHours(8));
  98. return user;
  99. }
  100. #endregion
  101. }
  102. }