GlobalController.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /// Gets a full comment of a given entity
  96. /// </summary>
  97. /// <param name="entityType">The entity type.</param>
  98. /// <param name="id">The entity id.</param>
  99. public ActionResult GetEntityComment(string entityType, int id)
  100. {
  101. var result = new JsonResult
  102. {
  103. JsonRequestBehavior = JsonRequestBehavior.AllowGet,
  104. Data = String.Empty
  105. };
  106. if (String.IsNullOrEmpty(entityType))
  107. return result;
  108. switch (entityType)
  109. {
  110. case "appendix":
  111. var appendix = _appendixService.GetAppendixById(id);
  112. result.Data = appendix.Comment;
  113. break;
  114. case "deviation":
  115. var deviation = _deviationService.GetDeviationById(id);
  116. result.Data = deviation.Comment;
  117. break;
  118. case "site":
  119. var site = _siteService.GetSiteById(id);
  120. result.Data = site.Comment;
  121. break;
  122. default:
  123. return result;
  124. }
  125. return result;
  126. }
  127. }
  128. }