HomeController.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 GreenTree.Nachtragsmanagement.Web.Models.Test;
  11. using Newtonsoft.Json;
  12. using GreenTree.Nachtragsmanagement.Core.Plugins;
  13. using GreenTree.Nachtragsmanagement.Core;
  14. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  15. using GreenTree.Nachtragsmanagement.Core.Authentication;
  16. using GreenTree.Nachtragsmanagement.Web.Models.Home;
  17. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  18. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  19. {
  20. [RoleAuthorize(false)]
  21. public class HomeController : Controller
  22. {
  23. private readonly IDbRelationService _dbRelationService;
  24. private readonly IConfigurationService _configurationService;
  25. private readonly IUserService _userService;
  26. private readonly IUserHelper _userHelper;
  27. private readonly IPluginFinder _pluginFinder;
  28. private readonly IWebHelper _webHelper;
  29. public HomeController(
  30. IDbRelationService dbRelationService,
  31. IConfigurationService configurationService,
  32. IUserService userService,
  33. IUserHelper userHelper,
  34. IPluginFinder pluginFinder,
  35. IWebHelper webHelper)
  36. {
  37. _dbRelationService = dbRelationService;
  38. _configurationService = configurationService;
  39. _userService = userService;
  40. _userHelper = userHelper;
  41. _pluginFinder = pluginFinder;
  42. _webHelper = webHelper;
  43. }
  44. // GET: Home
  45. public ActionResult Index()
  46. {
  47. var user = _userHelper.FromCookies();
  48. var userFunctions = user.Roles
  49. .SelectMany(s => s.Functions);
  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. public ActionResult Plugins()
  72. {
  73. var model = new PluginModel
  74. {
  75. PluginNames = new List<string[]>()
  76. };
  77. var uninstalledPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.NotInstalledOnly);
  78. var installedPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.InstalledOnly);
  79. if (installedPlugins.Any())
  80. model.PluginNames.AddRange(new List<string[]>()
  81. {
  82. new [] { installedPlugins.First().PluginDescriptor.SystemName, "installed" }
  83. });
  84. if (uninstalledPlugins.Any())
  85. model.PluginNames.AddRange(new List<string[]>()
  86. {
  87. new [] { uninstalledPlugins.First().PluginDescriptor.SystemName, "uninstalled" }
  88. });
  89. return View("~/Views/Home/Plugins.cshtml", model);
  90. }
  91. [HttpPost]
  92. public ActionResult InstallPlugin(string pluginName)
  93. {
  94. var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  95. if (pluginDescriptor == null)
  96. return RedirectToAction("Plugins");
  97. if (pluginDescriptor.Installed)
  98. return RedirectToAction("Plugins");
  99. var routes = System.Web.Routing.RouteTable.Routes;
  100. pluginDescriptor.Instance().Install();
  101. _webHelper.RestartAppDomain();
  102. return RedirectToAction("Plugins");
  103. }
  104. [HttpPost]
  105. public ActionResult UninstallPlugin(string pluginName)
  106. {
  107. var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  108. if (pluginDescriptor == null)
  109. return RedirectToAction("Plugins");
  110. if (!pluginDescriptor.Installed)
  111. return RedirectToAction("Plugins");
  112. pluginDescriptor.Instance().Uninstall();
  113. _webHelper.RestartAppDomain();
  114. return RedirectToAction("Plugins");
  115. }
  116. }
  117. }