AdminController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. public ActionResult PartialUsers()
  69. {
  70. var users = _userService.GetAllUsers();
  71. var userModels = users
  72. .Select(u => UserDataModel.FromUser(u, false))
  73. .ToList();
  74. return PartialView("~/Views/Admin/Users/_UserGridPartial.cshtml", userModels);
  75. }
  76. /// <summary>
  77. /// Partial edit for editing of existing or for new user
  78. /// </summary>
  79. /// <param name="id">Id for existing user, otherweise -1.</param>
  80. public ActionResult EditUser(int id = -1)
  81. {
  82. var user = _userService.GetUserById(id);
  83. var userModel = UserDataModel.FromUser(user, true);
  84. return PartialView("~/Views/Admin/Users/_UserEditPartial.cshtml", userModel);
  85. }
  86. /// <summary>
  87. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  88. /// </summary>
  89. /// <param name="userModel">User model to be saved.</param>
  90. [HttpPost, ValidateInput(false)]
  91. public ActionResult EditUser(UserDataModel userModel)
  92. {
  93. if (!ModelState.IsValid)
  94. {
  95. foreach (var role in userModel.RoleValues)
  96. userModel.RoleDescriptions.Add(
  97. ((IList<Role>)ViewData["AllRoles"])
  98. .First(r => r.Id == role).Description);
  99. return PartialView("~/Views/Admin/Users/_UserEditPartial.cshtml", userModel);
  100. }
  101. var selectedRoles = _userService.GetRolesByIds(userModel.RoleValues.ToArray());
  102. if (userModel.Id == -1)
  103. {
  104. var user = userModel.ToUser();
  105. user.SetRoles(selectedRoles);
  106. user.Password = StaticHelper.GetMD5Hash(userModel.Password);
  107. _userService.InsertUser(user);
  108. }
  109. else
  110. {
  111. var user = _userService.GetUserById(userModel.Id);
  112. user.CustomNumber = userModel.CustomNumber;
  113. user.Forename = userModel.Forename;
  114. user.Lastname = userModel.Lastname;
  115. user.MailAddress = userModel.MailAddress;
  116. if (!String.IsNullOrEmpty(userModel.Password))
  117. user.Password = StaticHelper.GetMD5Hash(userModel.Password);
  118. user.SetRoles(selectedRoles);
  119. _userService.UpdateUser(user);
  120. }
  121. return new JsonResult
  122. {
  123. Data = "success"
  124. };
  125. }
  126. /// <summary>
  127. /// Simple JSON result for deleting a specific user
  128. /// </summary>
  129. /// <param name="id">User id.</param>
  130. [HttpPost]
  131. public ActionResult DeleteUser(int id)
  132. {
  133. var user = _userService.GetUserById(id);
  134. if (user != null)
  135. _userService.DeleteUser(user);
  136. return new JsonResult
  137. {
  138. Data = "success"
  139. };
  140. }
  141. #endregion
  142. #region Roles
  143. /// <summary>
  144. /// Basic role view function
  145. /// </summary>
  146. [FunctionAuthorize(true, "Administration-Roles")]
  147. public ActionResult ViewRoles()
  148. {
  149. var roles = _userService.GetAllRoles();
  150. var roleModels = roles
  151. .Select(r => RoleDataModel.FromRole(r, false))
  152. .ToList();
  153. return View("~/Views/Admin/Roles/View.cshtml", roleModels);
  154. }
  155. /// <summary>
  156. /// Get JSON data of specific role
  157. /// </summary>
  158. /// <param name="id">Role id.</param>
  159. public ActionResult GetRole(int id = -1)
  160. {
  161. var role = _userService.GetRoleById(id);
  162. if (role == null)
  163. return new JsonResult
  164. {
  165. Data = "notFound",
  166. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  167. };
  168. var roleModel = RoleDataModel.FromRole(role, false);
  169. return new JsonResult
  170. {
  171. Data = JsonConvert.SerializeObject(roleModel),
  172. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  173. };
  174. }
  175. /// <summary>
  176. /// Callback result for role grid
  177. /// </summary>
  178. public ActionResult PartialRoles()
  179. {
  180. var roles = _userService.GetAllRoles();
  181. var roleModels = roles
  182. .Select(r => RoleDataModel.FromRole(r, false))
  183. .ToList();
  184. return PartialView("~/Views/Admin/Roles/_RoleGridPartial.cshtml", roleModels);
  185. }
  186. /// <summary>
  187. /// Partial edit for editing of existing or for new role
  188. /// </summary>
  189. /// <param name="id">Id for existing role, otherweise -1.</param>
  190. public ActionResult EditRole(int id = -1)
  191. {
  192. var role = _userService.GetRoleById(id);
  193. var roleModel = RoleDataModel.FromRole(role, true);
  194. return PartialView("~/Views/Admin/Roles/_RoleEditPartial.cshtml", roleModel);
  195. }
  196. /// <summary>
  197. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  198. /// </summary>
  199. /// <param name="roleModel">Role model to be saved.</param>
  200. [HttpPost, ValidateInput(false)]
  201. public ActionResult EditRole(RoleDataModel roleModel)
  202. {
  203. if (!ModelState.IsValid)
  204. {
  205. foreach (var role in roleModel.FunctionValues)
  206. roleModel.FunctionDescriptions.Add(
  207. ((IList<Function>)ViewData["AllFunctions"])
  208. .First(r => r.Id == role).Description);
  209. return PartialView("~/Views/Admin/Roles/_RoleEditPartial.cshtml", roleModel);
  210. }
  211. var selectedFunctions = _userService.GetFunctionsByIds(roleModel.FunctionValues.ToArray());
  212. if (roleModel.Id == -1)
  213. {
  214. var role = roleModel.ToRole();
  215. role.SetFunctions(selectedFunctions);
  216. _userService.InsertRole(role);
  217. }
  218. else
  219. {
  220. var role = _userService.GetRoleById(roleModel.Id);
  221. role.Description = roleModel.Description;
  222. role.Level = roleModel.Level;
  223. role.SetFunctions(selectedFunctions);
  224. _userService.UpdateRole(role);
  225. }
  226. return new JsonResult
  227. {
  228. Data = "success"
  229. };
  230. }
  231. /// <summary>
  232. /// Simple JSON result for deleting a specific role
  233. /// </summary>
  234. /// <param name="id">Role id.</param>
  235. /// <param name="replaceId">Id of role which user get in place of deleting role.</param>
  236. [HttpPost]
  237. public ActionResult DeleteRole(int id, int replaceId)
  238. {
  239. var role = _userService.GetRoleById(id);
  240. var replaceRole = _userService.GetRoleById(replaceId);
  241. var roleUsers = _userService.GetUsersByRole(id);
  242. foreach (var user in roleUsers)
  243. {
  244. if (replaceId == -1)
  245. user.Roles.Remove(role);
  246. else
  247. user.Roles.Add(replaceRole);
  248. _userService.UpdateUser(user);
  249. }
  250. if (role != null)
  251. _userService.DeleteRole(role);
  252. return new JsonResult
  253. {
  254. Data = "success"
  255. };
  256. }
  257. #endregion
  258. #region Plugins
  259. ///// <summary>
  260. ///// Basic plugin view function
  261. ///// </summary>
  262. //public ActionResult ViewPlugins()
  263. //{
  264. // var model = new PluginModel
  265. // {
  266. // PluginNames = new List<string[]>()
  267. // };
  268. // var uninstalledPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.NotInstalledOnly);
  269. // var installedPlugins = _pluginFinder.GetPlugins<IPlugin>(LoadPluginsMode.InstalledOnly);
  270. // if (installedPlugins.Any())
  271. // model.PluginNames.AddRange(new List<string[]>()
  272. // {
  273. // new [] { installedPlugins.First().PluginDescriptor.SystemName, "installed" }
  274. // });
  275. // if (uninstalledPlugins.Any())
  276. // model.PluginNames.AddRange(new List<string[]>()
  277. // {
  278. // new [] { uninstalledPlugins.First().PluginDescriptor.SystemName, "uninstalled" }
  279. // });
  280. // return View("~/Views/Admin/Plugins/View.cshtml");
  281. //}
  282. //[HttpPost]
  283. //public ActionResult InstallPlugin(string pluginName)
  284. //{
  285. // var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  286. // if (pluginDescriptor == null)
  287. // return RedirectToAction("Plugins");
  288. // if (pluginDescriptor.Installed)
  289. // return RedirectToAction("Plugins");
  290. // var routes = System.Web.Routing.RouteTable.Routes;
  291. // pluginDescriptor.Instance().Install();
  292. // _webHelper.RestartAppDomain();
  293. // return RedirectToAction("Plugins");
  294. //}
  295. //[HttpPost]
  296. //public ActionResult UninstallPlugin(string pluginName)
  297. //{
  298. // var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(pluginName, LoadPluginsMode.All);
  299. // if (pluginDescriptor == null)
  300. // return RedirectToAction("Plugins");
  301. // if (!pluginDescriptor.Installed)
  302. // return RedirectToAction("Plugins");
  303. // pluginDescriptor.Instance().Uninstall();
  304. // _webHelper.RestartAppDomain();
  305. // return RedirectToAction("Plugins");
  306. //}
  307. #endregion
  308. }
  309. }