HomeController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Plugins()
  45. {
  46. var model = new PluginModel
  47. {
  48. PluginNames = new List<string[]>()
  49. };
  50. var uninstalledPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.NotInstalledOnly);
  51. var installedPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.InstalledOnly);
  52. if (installedPlugins.Any())
  53. model.PluginNames.AddRange(new List<string[]>()
  54. {
  55. new [] { installedPlugins.First().PluginDescriptor.SystemName, "installed" }
  56. });
  57. if (uninstalledPlugins.Any())
  58. model.PluginNames.AddRange(new List<string[]>()
  59. {
  60. new [] { uninstalledPlugins.First().PluginDescriptor.SystemName, "uninstalled" }
  61. });
  62. return View("~/Views/Home/Plugins.cshtml", model);
  63. }
  64. [HttpPost]
  65. public ActionResult InstallPlugin(string pluginName)
  66. {
  67. var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  68. if (pluginDescriptor == null)
  69. return RedirectToAction("Plugins");
  70. if (pluginDescriptor.Installed)
  71. return RedirectToAction("Plugins");
  72. var routes = System.Web.Routing.RouteTable.Routes;
  73. pluginDescriptor.Instance().Install();
  74. _webHelper.RestartAppDomain();
  75. return RedirectToAction("Plugins");
  76. }
  77. [HttpPost]
  78. public ActionResult UninstallPlugin(string pluginName)
  79. {
  80. var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  81. if (pluginDescriptor == null)
  82. return RedirectToAction("Plugins");
  83. if (!pluginDescriptor.Installed)
  84. return RedirectToAction("Plugins");
  85. pluginDescriptor.Instance().Uninstall();
  86. _webHelper.RestartAppDomain();
  87. return RedirectToAction("Plugins");
  88. }
  89. }
  90. }