AdminController.cs 12 KB

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