HomeController.cs 4.8 KB

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