HomeController.cs 2.8 KB

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