HomeController.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using GreenTree.Nachtragsmanagement.Services.Configuration;
  7. using GreenTree.Nachtragsmanagement.Services.Test;
  8. using GreenTree.Nachtragsmanagement.Services.User;
  9. using GreenTree.Nachtragsmanagement.Web.Framework;
  10. using Newtonsoft.Json;
  11. using GreenTree.Nachtragsmanagement.Core.Plugins;
  12. using GreenTree.Nachtragsmanagement.Core;
  13. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  14. using GreenTree.Nachtragsmanagement.Core.Authentication;
  15. using GreenTree.Nachtragsmanagement.Web.Models.Home;
  16. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  17. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  18. {
  19. [FunctionAuthorize(false)]
  20. public class HomeController : Controller
  21. {
  22. private readonly IDbRelationService _dbRelationService;
  23. private readonly IConfigurationService _configurationService;
  24. private readonly IUserService _userService;
  25. private readonly IUserHelper _userHelper;
  26. private readonly IWebHelper _webHelper;
  27. public HomeController(
  28. IDbRelationService dbRelationService,
  29. IConfigurationService configurationService,
  30. IUserService userService,
  31. IUserHelper userHelper,
  32. IWebHelper webHelper)
  33. {
  34. _dbRelationService = dbRelationService;
  35. _configurationService = configurationService;
  36. _userService = userService;
  37. _userHelper = userHelper;
  38. _webHelper = webHelper;
  39. }
  40. // GET: Home
  41. public ActionResult Index()
  42. {
  43. var cookieUser = _userHelper.FromCookiesOrSession();
  44. if (cookieUser == null) return new RedirectResult("~/login");
  45. var dbUser = _userService.GetUserById(cookieUser.Id);
  46. if (cookieUser != null && dbUser == null)
  47. _userHelper.ClearCookieAndSession();
  48. if (dbUser == null) return new RedirectResult("~/login");
  49. if (dbUser.CurrentRole == null || (dbUser.CurrentRole != null && dbUser.CurrentRole.Id != cookieUser.CurrentRole.Id))
  50. dbUser.CurrentRole = cookieUser.CurrentRole;
  51. var userFunctions = dbUser.CurrentRole.Functions
  52. .Where(f => f.IsMenuMember.HasValue && f.IsMenuMember.Value)
  53. .Distinct();
  54. var groups = userFunctions
  55. .GroupBy(g => g.GroupName)
  56. .Select(g => g.Key);
  57. var dict = new Dictionary<Function, List<Function>>();
  58. foreach (var group in groups)
  59. {
  60. var keyFunction = userFunctions
  61. .FirstOrDefault(f => f.Name == group);
  62. if (keyFunction == null)
  63. continue;
  64. var subFunctions = userFunctions
  65. .Where(f => f.GroupName == group)
  66. .ToList();
  67. dict.Add(keyFunction, subFunctions);
  68. }
  69. var model = new HomeModel
  70. {
  71. AvailableFunctions = dict
  72. };
  73. return View("~/Views/Home/Index.cshtml", model);
  74. }
  75. }
  76. }