GlobalController.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using GreenTree.Nachtragsmanagement.Core.Authentication;
  2. using GreenTree.Nachtragsmanagement.Services.User;
  3. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  10. {
  11. public class GlobalController : Controller
  12. {
  13. private readonly IUserHelper _userHelper;
  14. private readonly IUserService _userService;
  15. public GlobalController(
  16. IUserHelper userHelper,
  17. IUserService userService)
  18. {
  19. _userHelper = userHelper;
  20. _userService = userService;
  21. }
  22. /// <summary>
  23. /// Represents the global Footer
  24. /// </summary>
  25. public ActionResult Footer()
  26. {
  27. var currentUser = _userHelper.FromCookies();
  28. if (currentUser == null)
  29. return View("~/Views/Shared/_Footer.cshtml", null);
  30. var footerModel = new FooterModel
  31. {
  32. CustomNumber = currentUser.CustomNumber,
  33. Forename = currentUser.Forename,
  34. Lastname = currentUser.Lastname,
  35. RoleDescription = currentUser.CurrentRole.Description
  36. };
  37. ViewData["Roles"] =
  38. currentUser.Roles
  39. .Select(r => new
  40. {
  41. r.Id,
  42. r.Description
  43. });
  44. return View("~/Views/Shared/_Footer.cshtml", footerModel);
  45. }
  46. /// <summary>
  47. /// Sets the current role of the logged in user and redirects to the home page
  48. /// </summary>
  49. /// <param name="roleId">The id of the new role.</param>
  50. public ActionResult SetRole(int roleId)
  51. {
  52. var user = _userHelper.FromCookies();
  53. var role = _userService.GetRoleById(roleId);
  54. user.CurrentRole = role;
  55. _userHelper.ToCookies(user);
  56. return RedirectToAction("Index", "Home");
  57. }
  58. /// <summary>
  59. /// Shows an unauthorized message
  60. /// </summary>
  61. public ActionResult NotAuthorized()
  62. {
  63. return View("~/Views/Global/NotAuthorized.cshtml");
  64. }
  65. }
  66. }