GlobalController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using GreenTree.Nachtragsmanagement.Core;
  2. using GreenTree.Nachtragsmanagement.Core.Authentication;
  3. using GreenTree.Nachtragsmanagement.Services.Appendix;
  4. using GreenTree.Nachtragsmanagement.Services.Configuration;
  5. using GreenTree.Nachtragsmanagement.Services.Deviation;
  6. using GreenTree.Nachtragsmanagement.Services.Logging;
  7. using GreenTree.Nachtragsmanagement.Services.Site;
  8. using GreenTree.Nachtragsmanagement.Services.User;
  9. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  10. using Newtonsoft.Json;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Web;
  15. using System.Web.Mvc;
  16. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  17. {
  18. public class GlobalController : Controller
  19. {
  20. private readonly IUserHelper _userHelper;
  21. private readonly IUserService _userService;
  22. private readonly IAppendixService _appendixService;
  23. private readonly IDeviationService _deviationService;
  24. private readonly ISiteService _siteService;
  25. private readonly IConfigurationService _configurationService;
  26. private readonly ILogger _logger;
  27. public GlobalController(
  28. IUserHelper userHelper,
  29. IUserService userService,
  30. IAppendixService appendixService,
  31. IDeviationService deviationService,
  32. ISiteService siteService,
  33. IConfigurationService configurationService,
  34. ILogger logger)
  35. {
  36. _userHelper = userHelper;
  37. _userService = userService;
  38. _appendixService = appendixService;
  39. _deviationService = deviationService;
  40. _siteService = siteService;
  41. _configurationService = configurationService;
  42. _logger = logger;
  43. }
  44. /// <summary>
  45. /// Represents the global Footer
  46. /// </summary>
  47. public ActionResult Footer()
  48. {
  49. var cookieUser = _userHelper.FromCookiesOrSession();
  50. if (cookieUser == null)
  51. return View("~/Views/Shared/_Footer.cshtml", null);
  52. var dbUser = _userService.GetUserById(cookieUser.Id);
  53. if (dbUser == null)
  54. return View("~/Views/Shared/_Footer.cshtml", null);
  55. var footerModel = new FooterModel
  56. {
  57. CustomNumber = dbUser.CustomNumber,
  58. Forename = dbUser.Forename,
  59. Lastname = dbUser.Lastname,
  60. RoleDescription = dbUser.CurrentRole.Description
  61. };
  62. ViewData["Roles"] =
  63. dbUser.Roles
  64. .Select(r => new
  65. {
  66. r.Id,
  67. r.Description
  68. });
  69. return View("~/Views/Shared/_Footer.cshtml", footerModel);
  70. }
  71. /// <summary>
  72. /// Sets the current role of the logged in user and redirects to the home page
  73. /// </summary>
  74. /// <param name="roleId">The id of the new role.</param>
  75. public ActionResult SetRole(int roleId = -1)
  76. {
  77. try
  78. {
  79. if (roleId == -1)
  80. return RedirectToAction("Index", "Home");
  81. var user = _userHelper.FromCookiesOrSession();
  82. var role = _userService.GetRoleById(roleId);
  83. user.CurrentRole = role;
  84. _userHelper.ToCookiesAndSession(user);
  85. }
  86. catch (Exception ex)
  87. {
  88. _logger.Error("Fehler bei Wechsel einer Rolle.", ex, _userHelper.FromCookiesOrSession());
  89. }
  90. return RedirectToAction("Index", "Home");
  91. }
  92. /// <summary>
  93. /// Shows an unauthorized message
  94. /// </summary>
  95. public ActionResult NotAuthorized()
  96. {
  97. return View("~/Views/Global/NotAuthorized.cshtml");
  98. }
  99. /// <summary>
  100. /// Shows a popup for changing the current user password
  101. /// </summary>
  102. public ActionResult ChangePassword()
  103. {
  104. var model = new PasswordChangeDataModel();
  105. return View("~/Views/Shared/_ChangePasswordPartial.cshtml", model);
  106. }
  107. /// <summary>
  108. /// Shows a popup for changing the current user password
  109. /// </summary>
  110. [HttpPost, ValidateInput(false)]
  111. public ActionResult ChangePassword(PasswordChangeDataModel passwordChangeModel)
  112. {
  113. try
  114. {
  115. if (!ModelState.IsValid)
  116. {
  117. passwordChangeModel.CurrentPassword = String.Empty;
  118. passwordChangeModel.NewPassword = String.Empty;
  119. passwordChangeModel.ConfirmedPassword = String.Empty;
  120. return PartialView("~/Views/Shared/_ChangePasswordPartial.cshtml", passwordChangeModel);
  121. }
  122. var currentUser = _userHelper.FromCookiesOrSession();
  123. if (currentUser == null)
  124. throw new Exception("Kein Benutzer angemeldet.");
  125. currentUser = _userService.GetUserById(currentUser.Id);
  126. if (currentUser == null)
  127. throw new Exception("Angemeldeter Benutzer kann nicht gefunden werden.");
  128. currentUser.Password = StaticHelper.GetMD5Hash(passwordChangeModel.NewPassword);
  129. _userService.UpdateUser(currentUser);
  130. return new JsonResult
  131. {
  132. Data = "success"
  133. };
  134. }
  135. catch (Exception ex)
  136. {
  137. _logger.Error("Fehler bei Änderung des Passworts.", ex, _userHelper.FromCookiesOrSession());
  138. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  139. }
  140. }
  141. /// <summary>
  142. /// Deletes the specified cookies and session variables from request
  143. /// </summary>
  144. /// <param name="cookies">Cookie names.</param>
  145. /// <param name="sessionVariables">Session variable names.</param>
  146. /// <param name="userConfigItemNames">Names of userConfigItems.</param>
  147. [HttpPost]
  148. public ActionResult DeleteCookiesSessionVariablesAndUserConfigs(string[] cookies, string[] sessionVariables,
  149. string[] userConfigItemNames)
  150. {
  151. if (cookies != null && cookies.Length > 0)
  152. {
  153. foreach (var cookieName in cookies)
  154. {
  155. var cookie = Request.Cookies[cookieName];
  156. if (cookie != null)
  157. {
  158. cookie.Expires = DateTime.Now.AddDays(-1);
  159. Request.Cookies.Set(cookie);
  160. Response.Cookies.Set(cookie);
  161. }
  162. }
  163. }
  164. if (sessionVariables != null && sessionVariables.Length > 0)
  165. {
  166. foreach (var sessionVariable in sessionVariables)
  167. {
  168. if (Session[sessionVariable] != null)
  169. Session.Remove(sessionVariable);
  170. }
  171. }
  172. if (userConfigItemNames != null && userConfigItemNames.Length > 0)
  173. {
  174. var user = CommonHelper.UserContext().CurrentUser;
  175. foreach (var userConfigItemName in userConfigItemNames)
  176. {
  177. var userConfigItem = _configurationService.GetUserConfigItemByNameAndUserId(userConfigItemName, user.Id);
  178. if (userConfigItem != null)
  179. _configurationService.DeleteUserConfigItem(userConfigItem);
  180. }
  181. }
  182. return new JsonResult
  183. {
  184. Data = "success"
  185. };
  186. }
  187. #region Comments
  188. /// <summary>
  189. /// Gets a full comment of a given entity
  190. /// </summary>
  191. /// <param name="entityType">The entity type.</param>
  192. /// <param name="id">The entity id.</param>
  193. public ActionResult GetEntityComment(string entityType, int id)
  194. {
  195. var result = new JsonResult
  196. {
  197. JsonRequestBehavior = JsonRequestBehavior.AllowGet,
  198. Data = String.Empty
  199. };
  200. if (String.IsNullOrEmpty(entityType))
  201. return result;
  202. switch (entityType)
  203. {
  204. case "appendix":
  205. var appendix = _appendixService.GetAppendixById(id);
  206. result.Data = appendix.Comment;
  207. break;
  208. case "deviation":
  209. var deviation = _deviationService.GetDeviationById(id);
  210. result.Data = deviation.Comment;
  211. break;
  212. case "site":
  213. var site = _siteService.GetSiteById(id);
  214. result.Data = site.Comment;
  215. break;
  216. default:
  217. return result;
  218. }
  219. return result;
  220. }
  221. /// <summary>
  222. /// Gets a full comment of a given entity and provides an edit form
  223. /// </summary>
  224. /// <param name="entityType">The entity type.</param>
  225. /// <param name="id">The entity id.</param>
  226. public ActionResult EditEntityComment(string entityType, int id)
  227. {
  228. var editCommentModel = new EditEntityCommentModel
  229. {
  230. EntityType = entityType,
  231. EntityId = id
  232. };
  233. switch (entityType)
  234. {
  235. case "appendix":
  236. var appendix = _appendixService.GetAppendixById(id);
  237. editCommentModel.Comment = appendix.Comment;
  238. break;
  239. case "deviation":
  240. var deviation = _deviationService.GetDeviationById(id);
  241. editCommentModel.Comment = deviation.Comment;
  242. break;
  243. case "site":
  244. var site = _siteService.GetSiteById(id);
  245. editCommentModel.Comment = site.Comment;
  246. break;
  247. default:
  248. return new EmptyResult();
  249. }
  250. return PartialView("~/Views/Shared/_EditCommentPartial.cshtml", editCommentModel);
  251. }
  252. /// <summary>
  253. /// Sets a comment for a given entity
  254. /// </summary>
  255. /// <param name="entityType">The entity type.</param>
  256. /// <param name="id">The entity id.</param>
  257. [HttpPost, ValidateInput(false)]
  258. public ActionResult EditEntityComment(EditEntityCommentModel model)
  259. {
  260. if (model == null)
  261. return new EmptyResult();
  262. switch (model.EntityType)
  263. {
  264. case "appendix":
  265. var appendix = _appendixService.GetAppendixById(model.EntityId);
  266. appendix.Comment = model.Comment;
  267. _appendixService.UpdateAppendix(appendix);
  268. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookiesOrSession());
  269. break;
  270. case "deviation":
  271. var deviation = _deviationService.GetDeviationById(model.EntityId);
  272. deviation.Comment = model.Comment;
  273. _deviationService.UpdateDeviation(deviation);
  274. _logger.Entity(deviation, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookiesOrSession());
  275. break;
  276. case "site":
  277. var site = _siteService.GetSiteById(model.EntityId);
  278. site.Comment = model.Comment;
  279. _siteService.UpdateSite(site);
  280. _logger.Entity(site, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookiesOrSession());
  281. break;
  282. default:
  283. return new EmptyResult();
  284. }
  285. return new JsonResult
  286. {
  287. Data = "success"
  288. };
  289. }
  290. #endregion
  291. }
  292. }