FunctionAuthorizeAttribute.cs 3.3 KB

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