using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using GreenTree.Nachtragsmanagement.Services.Configuration; using GreenTree.Nachtragsmanagement.Services.Test; using GreenTree.Nachtragsmanagement.Services.User; using GreenTree.Nachtragsmanagement.Web.Framework; using GreenTree.Nachtragsmanagement.Web.Models.Test; using GreenTree.Nachtragsmanagement.Web.Models.User; using Newtonsoft.Json; using GreenTree.Nachtragsmanagement.Core.Plugins; using GreenTree.Nachtragsmanagement.Core; using GreenTree.Nachtragsmanagement.Web.Framework.Authorization; using GreenTree.Nachtragsmanagement.Core.Authentication; using GreenTree.Nachtragsmanagement.Web.Models.Home; using GreenTree.Nachtragsmanagement.Core.Domain.User; namespace GreenTree.Nachtragsmanagement.Web.Controllers { [RoleAuthorize(false)] public class HomeController : Controller { private readonly IDbRelationService _dbRelationService; private readonly IConfigurationService _configurationService; private readonly IUserService _userService; private readonly IUserHelper _userHelper; private readonly IPluginFinder _pluginFinder; private readonly IWebHelper _webHelper; public HomeController( IDbRelationService dbRelationService, IConfigurationService configurationService, IUserService userService, IUserHelper userHelper, IPluginFinder pluginFinder, IWebHelper webHelper) { _dbRelationService = dbRelationService; _configurationService = configurationService; _userService = userService; _userHelper = userHelper; _pluginFinder = pluginFinder; _webHelper = webHelper; } // GET: Home public ActionResult Index() { var user = _userHelper.FromCookies(); var userFunctions = user.Roles .SelectMany(s => s.Functions); var groups = userFunctions .GroupBy(g => g.GroupName) .Select(g => g.Key); var dict = new Dictionary>(); foreach (var group in groups) { var keyFunction = userFunctions .FirstOrDefault(f => f.Name == group); if (keyFunction == null) continue; var subFunctions = userFunctions .Where(f => f.GroupName == group) .ToList(); dict.Add(keyFunction, subFunctions); } var model = new HomeModel { AvailableFunctions = dict }; return View("~/Views/Home/Index.cshtml", model); } public ActionResult Plugins() { var model = new PluginModel { PluginNames = new List() }; var uninstalledPlugins = _pluginFinder.GetPlugins(LoadPluginsMode.NotInstalledOnly); var installedPlugins = _pluginFinder.GetPlugins(LoadPluginsMode.InstalledOnly); if (installedPlugins.Any()) model.PluginNames.AddRange(new List() { new [] { installedPlugins.First().PluginDescriptor.SystemName, "installed" } }); if (uninstalledPlugins.Any()) model.PluginNames.AddRange(new List() { new [] { uninstalledPlugins.First().PluginDescriptor.SystemName, "uninstalled" } }); return View("~/Views/Home/Plugins.cshtml", model); } [HttpPost] public ActionResult InstallPlugin(string pluginName) { var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All); if (pluginDescriptor == null) return RedirectToAction("Plugins"); if (pluginDescriptor.Installed) return RedirectToAction("Plugins"); var routes = System.Web.Routing.RouteTable.Routes; pluginDescriptor.Instance().Install(); _webHelper.RestartAppDomain(); return RedirectToAction("Plugins"); } [HttpPost] public ActionResult UninstallPlugin(string pluginName) { var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All); if (pluginDescriptor == null) return RedirectToAction("Plugins"); if (!pluginDescriptor.Installed) return RedirectToAction("Plugins"); pluginDescriptor.Instance().Uninstall(); _webHelper.RestartAppDomain(); return RedirectToAction("Plugins"); } } }