using rgbc_sds.services.Models; using rgbc_sds.services.Services; using rgbc_sds.services.Enums; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Identity; using System.DirectoryServices; using System.Security.Principal; namespace rgbc_sds.website.Controllers { public class AccountController : Controller { private readonly UserService _userService; private readonly ILogger _logger; public AccountController(UserService userService, ILogger logger) { _userService = userService; _logger = logger; } public IActionResult Index() { return View(); } [Route("/")] [Route("account")] [Route("account/login")] public ActionResult Login() { return View(); } [HttpPost] [Route("account/login")] public IActionResult Login(LoginViewModel model) { var controller = "Shipment"; var action = "ShipmentList"; try { // PRODUCTION model.Username = User.Identity.Name; // LOCAL // model.Username = "rgbc\\maanie"; // validate that user exists in database var user = _userService.ValidateUser(model.Username); if (user != null) { //if both conditions are met, check user role switch (user.refUserGroupSDSID) { case (int)RoleEnum.Admin: HttpContext.Session.SetString("LoggedInUsername", user.strUserName); HttpContext.Session.SetString("LoggedInUserRole", "ADMIN"); HttpContext.Session.SetInt32("LoggedInUserId", user.Id); break; case (int)RoleEnum.Viewer: HttpContext.Session.SetString("LoggedInUsername", user.strUserName); HttpContext.Session.SetString("LoggedInUserRole", "VIEWER"); HttpContext.Session.SetInt32("LoggedInUserId", user.Id); break; case (int)RoleEnum.Logistics: HttpContext.Session.SetString("LoggedInUsername", user.strUserName); HttpContext.Session.SetString("LoggedInUserRole", "LOGISTICS"); HttpContext.Session.SetInt32("LoggedInUserId", user.Id); break; default: break; } _logger.LogInformation("User logged in successfully."); return RedirectToAction(action, controller); } else { TempData["LoginMessage"] = "User could not be Authenticated. Please contact your System Administrator."; return View(); } } catch (Exception ex) { _logger.LogError(ex, "Error occurred during login."); Console.WriteLine(ex.Message.ToString()); ModelState.AddModelError(string.Empty, "An error occurred. Please try again."); } return View(model); } [Route("account/logout")] public ActionResult Logout() { var model = new LoginViewModel(); return View("~/Views/Account/Login.cshtml", model); } } }