GlobalController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /// Deletes the specified cookies and session variables from request
  101. /// </summary>
  102. /// <param name="cookies">Cookie names.</param>
  103. /// <param name="sessionVariables">Session variable names.</param>
  104. /// <param name="userConfigItemNames">Names of userConfigItems.</param>
  105. [HttpPost]
  106. public ActionResult DeleteCookiesSessionVariablesAndUserConfigs(string[] cookies, string[] sessionVariables,
  107. string[] userConfigItemNames)
  108. {
  109. if (cookies != null && cookies.Length > 0)
  110. {
  111. foreach (var cookieName in cookies)
  112. {
  113. var cookie = Request.Cookies[cookieName];
  114. if (cookie != null)
  115. {
  116. cookie.Expires = DateTime.Now.AddDays(-1);
  117. Request.Cookies.Set(cookie);
  118. Response.Cookies.Set(cookie);
  119. }
  120. }
  121. }
  122. if (sessionVariables != null && sessionVariables.Length > 0)
  123. {
  124. foreach (var sessionVariable in sessionVariables)
  125. {
  126. if (Session[sessionVariable] != null)
  127. Session.Remove(sessionVariable);
  128. }
  129. }
  130. if (userConfigItemNames != null && userConfigItemNames.Length > 0)
  131. {
  132. var user = CommonHelper.UserContext().CurrentUser;
  133. foreach (var userConfigItemName in userConfigItemNames)
  134. {
  135. var userConfigItem = _configurationService.GetUserConfigItemByNameAndUserId(userConfigItemName, user.Id);
  136. if (userConfigItem != null)
  137. _configurationService.DeleteUserConfigItem(userConfigItem);
  138. }
  139. }
  140. return new JsonResult
  141. {
  142. Data = "success"
  143. };
  144. }
  145. #region Comments
  146. /// <summary>
  147. /// Gets a full comment of a given entity
  148. /// </summary>
  149. /// <param name="entityType">The entity type.</param>
  150. /// <param name="id">The entity id.</param>
  151. public ActionResult GetEntityComment(string entityType, int id)
  152. {
  153. var result = new JsonResult
  154. {
  155. JsonRequestBehavior = JsonRequestBehavior.AllowGet,
  156. Data = String.Empty
  157. };
  158. if (String.IsNullOrEmpty(entityType))
  159. return result;
  160. switch (entityType)
  161. {
  162. case "appendix":
  163. var appendix = _appendixService.GetAppendixById(id);
  164. result.Data = appendix.Comment;
  165. break;
  166. case "deviation":
  167. var deviation = _deviationService.GetDeviationById(id);
  168. result.Data = deviation.Comment;
  169. break;
  170. case "site":
  171. var site = _siteService.GetSiteById(id);
  172. result.Data = site.Comment;
  173. break;
  174. default:
  175. return result;
  176. }
  177. return result;
  178. }
  179. /// <summary>
  180. /// Gets a full comment of a given entity and provides an edit form
  181. /// </summary>
  182. /// <param name="entityType">The entity type.</param>
  183. /// <param name="id">The entity id.</param>
  184. public ActionResult EditEntityComment(string entityType, int id)
  185. {
  186. var editCommentModel = new EditEntityCommentModel
  187. {
  188. EntityType = entityType,
  189. EntityId = id
  190. };
  191. switch (entityType)
  192. {
  193. case "appendix":
  194. var appendix = _appendixService.GetAppendixById(id);
  195. editCommentModel.Comment = appendix.Comment;
  196. break;
  197. case "deviation":
  198. var deviation = _deviationService.GetDeviationById(id);
  199. editCommentModel.Comment = deviation.Comment;
  200. break;
  201. case "site":
  202. var site = _siteService.GetSiteById(id);
  203. editCommentModel.Comment = site.Comment;
  204. break;
  205. default:
  206. return new EmptyResult();
  207. }
  208. return PartialView("~/Views/Shared/_EditCommentPartial.cshtml", editCommentModel);
  209. }
  210. /// <summary>
  211. /// Sets a comment for a given entity
  212. /// </summary>
  213. /// <param name="entityType">The entity type.</param>
  214. /// <param name="id">The entity id.</param>
  215. [HttpPost, ValidateInput(false)]
  216. public ActionResult EditEntityComment(EditEntityCommentModel model)
  217. {
  218. if (model == null)
  219. return new EmptyResult();
  220. switch (model.EntityType)
  221. {
  222. case "appendix":
  223. var appendix = _appendixService.GetAppendixById(model.EntityId);
  224. appendix.Comment = model.Comment;
  225. _appendixService.UpdateAppendix(appendix);
  226. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookiesOrSession());
  227. break;
  228. case "deviation":
  229. var deviation = _deviationService.GetDeviationById(model.EntityId);
  230. deviation.Comment = model.Comment;
  231. _deviationService.UpdateDeviation(deviation);
  232. _logger.Entity(deviation, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookiesOrSession());
  233. break;
  234. case "site":
  235. var site = _siteService.GetSiteById(model.EntityId);
  236. site.Comment = model.Comment;
  237. _siteService.UpdateSite(site);
  238. _logger.Entity(site, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookiesOrSession());
  239. break;
  240. default:
  241. return new EmptyResult();
  242. }
  243. return new JsonResult
  244. {
  245. Data = "success"
  246. };
  247. }
  248. #endregion
  249. }
  250. }