HomeController.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. var dbUser = _userService.GetUserById(cookieUser.Id);
  45. if (dbUser == null) return new RedirectResult("~/login");
  46. if (dbUser.CurrentRole == null || (dbUser.CurrentRole != null && dbUser.CurrentRole.Id != cookieUser.CurrentRole.Id))
  47. dbUser.CurrentRole = cookieUser.CurrentRole;
  48. var userFunctions = dbUser.CurrentRole.Functions
  49. .Where(f => f.IsMenuMember.HasValue && f.IsMenuMember.Value)
  50. .Distinct();
  51. var groups = userFunctions
  52. .GroupBy(g => g.GroupName)
  53. .Select(g => g.Key);
  54. var dict = new Dictionary<Function, List<Function>>();
  55. foreach (var group in groups)
  56. {
  57. var keyFunction = userFunctions
  58. .FirstOrDefault(f => f.Name == group);
  59. if (keyFunction == null)
  60. continue;
  61. var subFunctions = userFunctions
  62. .Where(f => f.GroupName == group)
  63. .ToList();
  64. dict.Add(keyFunction, subFunctions);
  65. }
  66. var model = new HomeModel
  67. {
  68. AvailableFunctions = dict
  69. };
  70. return View("~/Views/Home/Index.cshtml", model);
  71. }
  72. }
  73. }