AdminController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Newtonsoft.Json;
  7. using GreenTree.Nachtragsmanagement.Core.Authentication;
  8. using GreenTree.Nachtragsmanagement.Services.User;
  9. using GreenTree.Nachtragsmanagement.Web.Models.Admin.User;
  10. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  11. using GreenTree.Nachtragsmanagement.Core;
  12. using GreenTree.Nachtragsmanagement.Core.Plugins;
  13. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  14. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  15. {
  16. public class AdminController : Controller
  17. {
  18. private readonly IUserService _userService;
  19. private readonly IUserHelper _userHelper;
  20. private readonly IPluginFinder _pluginFinder;
  21. public AdminController(
  22. IUserService userService,
  23. IUserHelper userHelper,
  24. IPluginFinder pluginFinder)
  25. {
  26. _userService = userService;
  27. _userHelper = userHelper;
  28. _pluginFinder = pluginFinder;
  29. ViewData["AllRoles"] = _userService.GetAllRoles();
  30. ViewData["AllFunctions"] = _userService.GetAllFunctions();
  31. }
  32. #region Users
  33. /// <summary>
  34. /// Basic user view function
  35. /// </summary>
  36. [FunctionAuthorize(true, "Administration-Users")]
  37. public ActionResult ViewUsers()
  38. {
  39. var users = _userService.GetAllUsers();
  40. var userModels = users
  41. .Select(u => UserDataModel.FromUser(u, false))
  42. .ToList();
  43. return View("~/Views/Admin/Users/View.cshtml", userModels);
  44. }
  45. /// <summary>
  46. /// Get JSON data of specific user
  47. /// </summary>
  48. /// <param name="id">User id.</param>
  49. public ActionResult GetUser(int id = -1)
  50. {
  51. var user = _userService.GetUserById(id);
  52. if (user == null)
  53. return new JsonResult
  54. {
  55. Data = "notFound",
  56. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  57. };
  58. var userModel = UserDataModel.FromUser(user, false);
  59. return new JsonResult
  60. {
  61. Data = JsonConvert.SerializeObject(userModel),
  62. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  63. };
  64. }
  65. /// <summary>
  66. /// Callback result for user grid
  67. /// </summary>
  68. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  69. public ActionResult PartialUsers(int scrollHeight = -1)
  70. {
  71. var users = _userService.GetAllUsers();
  72. var userModels = users
  73. .Select(u => UserDataModel.FromUser(u, false))
  74. .ToList();
  75. ViewData["ScrollHeight"] = scrollHeight;
  76. return PartialView("~/Views/Admin/Users/_UserGridPartial.cshtml", userModels);
  77. }
  78. /// <summary>
  79. /// Partial edit for editing of existing or for new user
  80. /// </summary>
  81. /// <param name="id">Id for existing user, otherweise -1.</param>
  82. public ActionResult EditUser(int id = -1)
  83. {
  84. var user = _userService.GetUserById(id);
  85. var userModel = UserDataModel.FromUser(user, true);
  86. return PartialView("~/Views/Admin/Users/_UserEditPartial.cshtml", userModel);
  87. }
  88. /// <summary>
  89. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  90. /// </summary>
  91. /// <param name="userModel">User model to be saved.</param>
  92. [HttpPost, ValidateInput(false)]
  93. public ActionResult EditUser(UserDataModel userModel)
  94. {
  95. if (!ModelState.IsValid)
  96. {
  97. foreach (var role in userModel.RoleValues)
  98. userModel.RoleDescriptions.Add(
  99. ((IList<Role>)ViewData["AllRoles"])
  100. .First(r => r.Id == role).Description);
  101. return PartialView("~/Views/Admin/Users/_UserEditPartial.cshtml", userModel);
  102. }
  103. var selectedRoles = _userService.GetRolesByIds(userModel.RoleValues.ToArray());
  104. if (userModel.Id == -1)
  105. {
  106. var user = userModel.ToUser();
  107. user.SetRoles(selectedRoles);
  108. user.Password = StaticHelper.GetMD5Hash(userModel.Password);
  109. _userService.InsertUser(user);
  110. }
  111. else
  112. {
  113. var user = _userService.GetUserById(userModel.Id);
  114. user.CustomNumber = userModel.CustomNumber;
  115. user.Forename = userModel.Forename;
  116. user.Lastname = userModel.Lastname;
  117. user.MailAddress = userModel.MailAddress;
  118. if (!String.IsNullOrEmpty(userModel.Password))
  119. user.Password = StaticHelper.GetMD5Hash(userModel.Password);
  120. user.SetRoles(selectedRoles);
  121. _userService.UpdateUser(user);
  122. }
  123. return new JsonResult
  124. {
  125. Data = "success"
  126. };
  127. }
  128. /// <summary>
  129. /// Simple JSON result for deleting a specific user
  130. /// </summary>
  131. /// <param name="id">User id.</param>
  132. [HttpPost]
  133. public ActionResult DeleteUser(int id)
  134. {
  135. var user = _userService.GetUserById(id);
  136. if (user != null)
  137. _userService.DeleteUser(user);
  138. return new JsonResult
  139. {
  140. Data = "success"
  141. };
  142. }
  143. #endregion
  144. #region Roles
  145. /// <summary>
  146. /// Basic role view function
  147. /// </summary>
  148. [FunctionAuthorize(true, "Administration-Roles")]
  149. public ActionResult ViewRoles()
  150. {
  151. var roles = _userService.GetAllRoles();
  152. var roleModels = roles
  153. .Select(r => RoleDataModel.FromRole(r, false))
  154. .ToList();
  155. return View("~/Views/Admin/Roles/View.cshtml", roleModels);
  156. }
  157. /// <summary>
  158. /// Get JSON data of specific role
  159. /// </summary>
  160. /// <param name="id">Role id.</param>
  161. public ActionResult GetRole(int id = -1)
  162. {
  163. var role = _userService.GetRoleById(id);
  164. if (role == null)
  165. return new JsonResult
  166. {
  167. Data = "notFound",
  168. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  169. };
  170. var roleModel = RoleDataModel.FromRole(role, false);
  171. return new JsonResult
  172. {
  173. Data = JsonConvert.SerializeObject(roleModel),
  174. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  175. };
  176. }
  177. /// <summary>
  178. /// Callback result for role grid
  179. /// </summary>
  180. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  181. public ActionResult PartialRoles(int scrollHeight = -1)
  182. {
  183. var roles = _userService.GetAllRoles();
  184. var roleModels = roles
  185. .Select(r => RoleDataModel.FromRole(r, false))
  186. .ToList();
  187. ViewData["ScrollHeight"] = scrollHeight;
  188. return PartialView("~/Views/Admin/Roles/_RoleGridPartial.cshtml", roleModels);
  189. }
  190. /// <summary>
  191. /// Partial edit for editing of existing or for new role
  192. /// </summary>
  193. /// <param name="id">Id for existing role, otherweise -1.</param>
  194. public ActionResult EditRole(int id = -1)
  195. {
  196. var role = _userService.GetRoleById(id);
  197. var roleModel = RoleDataModel.FromRole(role, true);
  198. return PartialView("~/Views/Admin/Roles/_RoleEditPartial.cshtml", roleModel);
  199. }
  200. /// <summary>
  201. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  202. /// </summary>
  203. /// <param name="roleModel">Role model to be saved.</param>
  204. [HttpPost, ValidateInput(false)]
  205. public ActionResult EditRole(RoleDataModel roleModel)
  206. {
  207. if (!ModelState.IsValid)
  208. {
  209. foreach (var role in roleModel.FunctionValues)
  210. roleModel.FunctionDescriptions.Add(
  211. ((IList<Function>)ViewData["AllFunctions"])
  212. .First(r => r.Id == role).Description);
  213. return PartialView("~/Views/Admin/Roles/_RoleEditPartial.cshtml", roleModel);
  214. }
  215. var selectedFunctions = _userService.GetFunctionsByIds(roleModel.FunctionValues.ToArray());
  216. if (roleModel.Id == -1)
  217. {
  218. var role = roleModel.ToRole();
  219. role.SetFunctions(selectedFunctions);
  220. _userService.InsertRole(role);
  221. }
  222. else
  223. {
  224. var role = _userService.GetRoleById(roleModel.Id);
  225. role.Description = roleModel.Description;
  226. role.Level = roleModel.Level;
  227. role.SetFunctions(selectedFunctions);
  228. _userService.UpdateRole(role);
  229. }
  230. return new JsonResult
  231. {
  232. Data = "success"
  233. };
  234. }
  235. /// <summary>
  236. /// Simple JSON result for deleting a specific role
  237. /// </summary>
  238. /// <param name="id">Role id.</param>
  239. /// <param name="replaceId">Id of role which user get in place of deleting role.</param>
  240. [HttpPost]
  241. public ActionResult DeleteRole(int id, int replaceId)
  242. {
  243. var role = _userService.GetRoleById(id);
  244. var replaceRole = _userService.GetRoleById(replaceId);
  245. var roleUsers = _userService.GetUsersByRole(id);
  246. foreach (var user in roleUsers)
  247. {
  248. if (replaceId == -1)
  249. user.Roles.Remove(role);
  250. else
  251. user.Roles.Add(replaceRole);
  252. _userService.UpdateUser(user);
  253. }
  254. if (role != null)
  255. _userService.DeleteRole(role);
  256. return new JsonResult
  257. {
  258. Data = "success"
  259. };
  260. }
  261. #endregion
  262. #region Plugins
  263. ///// <summary>
  264. ///// Basic plugin view function
  265. ///// </summary>
  266. //public ActionResult ViewPlugins()
  267. //{
  268. // var model = new PluginModel
  269. // {
  270. // PluginNames = new List<string[]>()
  271. // };
  272. // var uninstalledPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.NotInstalledOnly);
  273. // var installedPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.InstalledOnly);
  274. // if (installedPlugins.Any())
  275. // model.PluginNames.AddRange(new List<string[]>()
  276. // {
  277. // new [] { installedPlugins.First().PluginDescriptor.SystemName, "installed" }
  278. // });
  279. // if (uninstalledPlugins.Any())
  280. // model.PluginNames.AddRange(new List<string[]>()
  281. // {
  282. // new [] { uninstalledPlugins.First().PluginDescriptor.SystemName, "uninstalled" }
  283. // });
  284. // return View("~/Views/Admin/Plugins/View.cshtml");
  285. //}
  286. //[HttpPost]
  287. //public ActionResult InstallPlugin(string pluginName)
  288. //{
  289. // var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  290. // if (pluginDescriptor == null)
  291. // return RedirectToAction("Plugins");
  292. // if (pluginDescriptor.Installed)
  293. // return RedirectToAction("Plugins");
  294. // var routes = System.Web.Routing.RouteTable.Routes;
  295. // pluginDescriptor.Instance().Install();
  296. // _webHelper.RestartAppDomain();
  297. // return RedirectToAction("Plugins");
  298. //}
  299. //[HttpPost]
  300. //public ActionResult UninstallPlugin(string pluginName)
  301. //{
  302. // var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  303. // if (pluginDescriptor == null)
  304. // return RedirectToAction("Plugins");
  305. // if (!pluginDescriptor.Installed)
  306. // return RedirectToAction("Plugins");
  307. // pluginDescriptor.Instance().Uninstall();
  308. // _webHelper.RestartAppDomain();
  309. // return RedirectToAction("Plugins");
  310. //}
  311. #endregion
  312. }
  313. }