|
|
@@ -9,6 +9,7 @@ using GreenTree.Nachtragsmanagement.Web.Models.Admin.User;
|
|
|
using GreenTree.Nachtragsmanagement.Core.Domain.User;
|
|
|
using Newtonsoft.Json;
|
|
|
using GreenTree.Nachtragsmanagement.Core;
|
|
|
+using GreenTree.Nachtragsmanagement.Core.Plugins;
|
|
|
|
|
|
namespace GreenTree.Nachtragsmanagement.Web.Controllers
|
|
|
{
|
|
|
@@ -16,15 +17,19 @@ namespace GreenTree.Nachtragsmanagement.Web.Controllers
|
|
|
{
|
|
|
private readonly IUserService _userService;
|
|
|
private readonly IUserHelper _userHelper;
|
|
|
+ private readonly IPluginFinder _pluginFinder;
|
|
|
|
|
|
public AdminController(
|
|
|
IUserService userService,
|
|
|
- IUserHelper userHelper)
|
|
|
+ IUserHelper userHelper,
|
|
|
+ IPluginFinder pluginFinder)
|
|
|
{
|
|
|
_userService = userService;
|
|
|
_userHelper = userHelper;
|
|
|
+ _pluginFinder = pluginFinder;
|
|
|
|
|
|
ViewData["AllRoles"] = _userService.GetAllRoles();
|
|
|
+ ViewData["AllFunctions"] = _userService.GetAllFunctions();
|
|
|
}
|
|
|
|
|
|
#region Users
|
|
|
@@ -168,21 +173,207 @@ namespace GreenTree.Nachtragsmanagement.Web.Controllers
|
|
|
/// </summary>
|
|
|
public ActionResult ViewRoles()
|
|
|
{
|
|
|
- return View("~/Views/Admin/Roles/View.cshtml");
|
|
|
+ var roles = _userService.GetAllRoles();
|
|
|
+ var roleModels = roles
|
|
|
+ .Select(r => RoleDataModel.FromRole(r, false))
|
|
|
+ .ToList();
|
|
|
+
|
|
|
+ return View("~/Views/Admin/Roles/View.cshtml", roleModels);
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
+ /// <summary>
|
|
|
+ /// Get JSON data of specific role
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id">Role id.</param>
|
|
|
+ public ActionResult GetRole(int id = -1)
|
|
|
+ {
|
|
|
+ var role = _userService.GetRoleById(id);
|
|
|
+ if (role == null)
|
|
|
+ return new JsonResult
|
|
|
+ {
|
|
|
+ Data = "notFound",
|
|
|
+ JsonRequestBehavior = JsonRequestBehavior.AllowGet
|
|
|
+ };
|
|
|
|
|
|
- #region Plugins
|
|
|
+ var roleModel = RoleDataModel.FromRole(role, false);
|
|
|
+
|
|
|
+ return new JsonResult
|
|
|
+ {
|
|
|
+ Data = JsonConvert.SerializeObject(roleModel),
|
|
|
+ JsonRequestBehavior = JsonRequestBehavior.AllowGet
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Callback result for role grid
|
|
|
+ /// </summary>
|
|
|
+ public ActionResult PartialRoles()
|
|
|
+ {
|
|
|
+ var roles = _userService.GetAllRoles();
|
|
|
+ var roleModels = roles
|
|
|
+ .Select(r => RoleDataModel.FromRole(r, false))
|
|
|
+ .ToList();
|
|
|
+
|
|
|
+ return PartialView("~/Views/Admin/Roles/_RoleGridPartial.cshtml", roleModels);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Partial edit for editing of existing or for new role
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id">Id for existing role, otherweise -1.</param>
|
|
|
+ public ActionResult EditRole(int id = -1)
|
|
|
+ {
|
|
|
+ var role = _userService.GetRoleById(id);
|
|
|
+ var roleModel = RoleDataModel.FromRole(role, true);
|
|
|
+
|
|
|
+ return PartialView("~/Views/Admin/Roles/_RoleEditPartial.cshtml", roleModel);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="roleModel">Role model to be saved.</param>
|
|
|
+ [HttpPost, ValidateInput(false)]
|
|
|
+ public ActionResult EditRole(RoleDataModel roleModel)
|
|
|
+ {
|
|
|
+ if (!ModelState.IsValid)
|
|
|
+ {
|
|
|
+ foreach (var role in roleModel.FunctionValues)
|
|
|
+ roleModel.FunctionDescriptions.Add(
|
|
|
+ ((IList<Role>)ViewData["AllRoles"])
|
|
|
+ .First(r => r.Id == role).Description);
|
|
|
+
|
|
|
+ return PartialView("~/Views/Admin/Roles/_RoleEditPartial.cshtml", roleModel);
|
|
|
+ }
|
|
|
+
|
|
|
+ var selectedFunctions = _userService.GetFunctionsByIds(roleModel.FunctionValues.ToArray());
|
|
|
+
|
|
|
+ if (roleModel.Id == -1)
|
|
|
+ {
|
|
|
+ var role = roleModel.ToRole();
|
|
|
+
|
|
|
+ role.SetFunctions(selectedFunctions);
|
|
|
+
|
|
|
+ _userService.InsertRole(role);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var role = _userService.GetRoleById(roleModel.Id);
|
|
|
+
|
|
|
+ role.Description = roleModel.Description;
|
|
|
+ role.Level = roleModel.Level;
|
|
|
+
|
|
|
+ role.SetFunctions(selectedFunctions);
|
|
|
+
|
|
|
+ _userService.UpdateRole(role);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new JsonResult
|
|
|
+ {
|
|
|
+ Data = "success"
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Basic plugin view function
|
|
|
+ /// Simple JSON result for deleting a specific role
|
|
|
/// </summary>
|
|
|
- public ActionResult ViewPlugins()
|
|
|
+ /// <param name="id">Role id.</param>
|
|
|
+ /// <param name="replaceId">Id of role which user get in place of deleting role.</param>
|
|
|
+ [HttpPost]
|
|
|
+ public ActionResult DeleteRole(int id, int replaceId)
|
|
|
{
|
|
|
- return View("~/Views/Admin/Plugins/View.cshtml");
|
|
|
+ var role = _userService.GetRoleById(id);
|
|
|
+ var replaceRole = _userService.GetRoleById(replaceId);
|
|
|
+
|
|
|
+ var roleUsers = _userService.GetUsersByRole(id);
|
|
|
+
|
|
|
+ foreach (var user in roleUsers)
|
|
|
+ {
|
|
|
+ if (replaceId == -1)
|
|
|
+ user.Roles.Remove(role);
|
|
|
+ else
|
|
|
+ user.Roles.Add(replaceRole);
|
|
|
+
|
|
|
+ _userService.UpdateUser(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (role != null)
|
|
|
+ _userService.DeleteRole(role);
|
|
|
+
|
|
|
+ return new JsonResult
|
|
|
+ {
|
|
|
+ Data = "success"
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
+
|
|
|
+ #region Plugins
|
|
|
+
|
|
|
+ ///// <summary>
|
|
|
+ ///// Basic plugin view function
|
|
|
+ ///// </summary>
|
|
|
+ //public ActionResult ViewPlugins()
|
|
|
+ //{
|
|
|
+ // var model = new PluginModel
|
|
|
+ // {
|
|
|
+ // PluginNames = new List<string[]>()
|
|
|
+ // };
|
|
|
+
|
|
|
+ // var uninstalledPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.NotInstalledOnly);
|
|
|
+ // var installedPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.InstalledOnly);
|
|
|
+
|
|
|
+ // if (installedPlugins.Any())
|
|
|
+ // model.PluginNames.AddRange(new List<string[]>()
|
|
|
+ // {
|
|
|
+ // new [] { installedPlugins.First().PluginDescriptor.SystemName, "installed" }
|
|
|
+ // });
|
|
|
+
|
|
|
+ // if (uninstalledPlugins.Any())
|
|
|
+ // model.PluginNames.AddRange(new List<string[]>()
|
|
|
+ // {
|
|
|
+ // new [] { uninstalledPlugins.First().PluginDescriptor.SystemName, "uninstalled" }
|
|
|
+ // });
|
|
|
+
|
|
|
+ // return View("~/Views/Admin/Plugins/View.cshtml");
|
|
|
+ //}
|
|
|
+
|
|
|
+ //[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");
|
|
|
+ //}
|
|
|
+
|
|
|
+ #endregion
|
|
|
}
|
|
|
}
|