HomeController.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  17. {
  18. [RoleAuthorize("Controller-Home")]
  19. public class HomeController : Controller
  20. {
  21. private readonly IDbRelationService _dbRelationService;
  22. private readonly IConfigurationService _configurationService;
  23. private readonly IUserService _userService;
  24. private readonly IPluginFinder _pluginFinder;
  25. private readonly IWebHelper _webHelper;
  26. public HomeController(
  27. IDbRelationService dbRelationService,
  28. IConfigurationService configurationService,
  29. IUserService userService,
  30. IPluginFinder pluginFinder,
  31. IWebHelper webHelper)
  32. {
  33. _dbRelationService = dbRelationService;
  34. _configurationService = configurationService;
  35. _userService = userService;
  36. _pluginFinder = pluginFinder;
  37. _webHelper = webHelper;
  38. }
  39. // GET: Home
  40. public ActionResult Index()
  41. {
  42. return View("~/Views/Home/Index.cshtml");
  43. }
  44. public ActionResult Relations()
  45. {
  46. var users = _userService.GetAllUsers();
  47. var relations = _dbRelationService.GetRelations(3, DbRelationFormat.Json);
  48. var model = new DbRelationModel
  49. {
  50. UserJson = relations[0],
  51. DeviationJson = relations[1],
  52. SiteJson = relations[2],
  53. AppendixJson = relations[3]
  54. };
  55. var configSection = _configurationService.GetCurrentConfiguration();
  56. return View("~/Views/Home/Relations.cshtml", model);
  57. }
  58. public ActionResult Plugins()
  59. {
  60. var model = new PluginModel
  61. {
  62. PluginNames = new List<string[]>()
  63. };
  64. var uninstalledPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.NotInstalledOnly);
  65. var installedPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.InstalledOnly);
  66. if (installedPlugins.Any())
  67. model.PluginNames.AddRange(new List<string[]>()
  68. {
  69. new [] { installedPlugins.First().PluginDescriptor.SystemName, "installed" }
  70. });
  71. if (uninstalledPlugins.Any())
  72. model.PluginNames.AddRange(new List<string[]>()
  73. {
  74. new [] { uninstalledPlugins.First().PluginDescriptor.SystemName, "uninstalled" }
  75. });
  76. return View("~/Views/Home/Plugins.cshtml", model);
  77. }
  78. [HttpPost]
  79. public ActionResult InstallPlugin(string pluginName)
  80. {
  81. var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  82. if (pluginDescriptor == null)
  83. return RedirectToAction("Plugins");
  84. if (pluginDescriptor.Installed)
  85. return RedirectToAction("Plugins");
  86. var routes = System.Web.Routing.RouteTable.Routes;
  87. pluginDescriptor.Instance().Install();
  88. _webHelper.RestartAppDomain();
  89. return RedirectToAction("Plugins");
  90. }
  91. [HttpPost]
  92. public ActionResult UninstallPlugin(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. pluginDescriptor.Instance().Uninstall();
  100. _webHelper.RestartAppDomain();
  101. return RedirectToAction("Plugins");
  102. }
  103. }
  104. }