using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Newtonsoft.Json; using GreenTree.Nachtragsmanagement.Core.Authentication; using GreenTree.Nachtragsmanagement.Services.User; using GreenTree.Nachtragsmanagement.Web.Models.Admin.User; using GreenTree.Nachtragsmanagement.Core.Domain.User; using GreenTree.Nachtragsmanagement.Core; using GreenTree.Nachtragsmanagement.Core.Plugins; using GreenTree.Nachtragsmanagement.Web.Framework.Authorization; namespace GreenTree.Nachtragsmanagement.Web.Controllers { public class AdminController : Controller { private readonly IUserService _userService; private readonly IUserHelper _userHelper; private readonly IPluginFinder _pluginFinder; public AdminController( IUserService userService, IUserHelper userHelper, IPluginFinder pluginFinder) { _userService = userService; _userHelper = userHelper; _pluginFinder = pluginFinder; ViewData["AllRoles"] = _userService.GetAllRoles(); ViewData["AllFunctions"] = _userService.GetAllFunctions(); } #region Users /// /// Basic user view function /// [FunctionAuthorize(true, "Administration-Users")] public ActionResult ViewUsers() { var users = _userService.GetAllUsers(); var userModels = users .Select(u => UserDataModel.FromUser(u, false)) .ToList(); return View("~/Views/Admin/Users/View.cshtml", userModels); } /// /// Get JSON data of specific user /// /// User id. public ActionResult GetUser(int id = -1) { var user = _userService.GetUserById(id); if (user == null) return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet }; var userModel = UserDataModel.FromUser(user, false); return new JsonResult { Data = JsonConvert.SerializeObject(userModel), JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } /// /// Callback result for user grid /// public ActionResult PartialUsers() { var users = _userService.GetAllUsers(); var userModels = users .Select(u => UserDataModel.FromUser(u, false)) .ToList(); return PartialView("~/Views/Admin/Users/_UserGridPartial.cshtml", userModels); } /// /// Partial edit for editing of existing or for new user /// /// Id for existing user, otherweise -1. public ActionResult EditUser(int id = -1) { var user = _userService.GetUserById(id); var userModel = UserDataModel.FromUser(user, true); return PartialView("~/Views/Admin/Users/_UserEditPartial.cshtml", userModel); } /// /// Partial edit result if ModelState is valid, otherwise simple JSON result for success /// /// User model to be saved. [HttpPost, ValidateInput(false)] public ActionResult EditUser(UserDataModel userModel) { if (!ModelState.IsValid) { foreach (var role in userModel.RoleValues) userModel.RoleDescriptions.Add( ((IList)ViewData["AllRoles"]) .First(r => r.Id == role).Description); return PartialView("~/Views/Admin/Users/_UserEditPartial.cshtml", userModel); } var selectedRoles = _userService.GetRolesByIds(userModel.RoleValues.ToArray()); if (userModel.Id == -1) { var user = userModel.ToUser(); user.SetRoles(selectedRoles); user.Password = StaticHelper.GetMD5Hash(userModel.Password); _userService.InsertUser(user); } else { var user = _userService.GetUserById(userModel.Id); user.CustomNumber = userModel.CustomNumber; user.Forename = userModel.Forename; user.Lastname = userModel.Lastname; user.MailAddress = userModel.MailAddress; if (!String.IsNullOrEmpty(userModel.Password)) user.Password = StaticHelper.GetMD5Hash(userModel.Password); user.SetRoles(selectedRoles); _userService.UpdateUser(user); } return new JsonResult { Data = "success" }; } /// /// Simple JSON result for deleting a specific user /// /// User id. [HttpPost] public ActionResult DeleteUser(int id) { var user = _userService.GetUserById(id); if (user != null) _userService.DeleteUser(user); return new JsonResult { Data = "success" }; } #endregion #region Roles /// /// Basic role view function /// [FunctionAuthorize(true, "Administration-Roles")] public ActionResult ViewRoles() { var roles = _userService.GetAllRoles(); var roleModels = roles .Select(r => RoleDataModel.FromRole(r, false)) .ToList(); return View("~/Views/Admin/Roles/View.cshtml", roleModels); } /// /// Get JSON data of specific role /// /// Role id. public ActionResult GetRole(int id = -1) { var role = _userService.GetRoleById(id); if (role == null) return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet }; var roleModel = RoleDataModel.FromRole(role, false); return new JsonResult { Data = JsonConvert.SerializeObject(roleModel), JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } /// /// Callback result for role grid /// 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); } /// /// Partial edit for editing of existing or for new role /// /// Id for existing role, otherweise -1. 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); } /// /// Partial edit result if ModelState is valid, otherwise simple JSON result for success /// /// Role model to be saved. [HttpPost, ValidateInput(false)] public ActionResult EditRole(RoleDataModel roleModel) { if (!ModelState.IsValid) { foreach (var role in roleModel.FunctionValues) roleModel.FunctionDescriptions.Add( ((IList)ViewData["AllFunctions"]) .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" }; } /// /// Simple JSON result for deleting a specific role /// /// Role id. /// Id of role which user get in place of deleting role. [HttpPost] public ActionResult DeleteRole(int id, int replaceId) { 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 ///// ///// Basic plugin view function ///// //public ActionResult ViewPlugins() //{ // 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/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 } }