GlobalController.cs 9.0 KB

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