public UAUserViewModel MapToViewModel(User dbUser) { UAUserViewModel vmUser = this.createUAUserViewModel(dbUser); foreach (UserRole dbUserRole in dbUser.UserRoles) { vmUser.Roles.Add(this.createUARoleViewModel(dbUserRole)); } List <Role> allActiveDbRoles = _roleService.RetrieveActiveRoles(); foreach (Role activeDbRole in allActiveDbRoles) { vmUser.CheckboxRoles.Add(this.createUARoleViewModel(activeDbRole)); } List <int> rolesForUserIdList = new List <int>(); foreach (UARoleViewModel vmRole in vmUser.Roles) { rolesForUserIdList.Add(vmRole.RoleId); } HashSet <int> rolesForUserIdSet = new HashSet <int>(rolesForUserIdList); foreach (UARoleViewModel checkboxRole in vmUser.CheckboxRoles) { checkboxRole.Selected = rolesForUserIdSet.Contains(checkboxRole.RoleId); } return(vmUser); }
public void PopulateCheckboxRolesForCreateUser(UAUserViewModel newUser) { List <Role> allActiveDbRoles = _roleService.RetrieveActiveRoles(); foreach (Role activeDbRole in allActiveDbRoles) { newUser.CheckboxRoles.Add(this.createUARoleViewModel(activeDbRole)); } }
private UAUserViewModel createUAUserViewModel(User dbUser) { UAUserViewModel vmUser = new UAUserViewModel(); vmUser.UserId = dbUser.UserId; vmUser.ManagerId = dbUser.ManagerId; vmUser.OrgId = dbUser.OrgId; vmUser.EmailAddress = dbUser.EmailAddress; vmUser.DisplayName = dbUser.DisplayName; return(vmUser); }
public IActionResult Update( [Bind("UserId, ManagerId, OrgId, EmailAddress, DisplayName")] UAUserViewModel userVM, string[] selectedRoleIdsForUser) { string logSnippet = new StringBuilder("[") .Append(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")) .Append("][UserController][Update][HttpPost] => ") .ToString(); Console.WriteLine(logSnippet + $"(userVM.UserId)................: {userVM.UserId}"); Console.WriteLine(logSnippet + $"(userVM.OrgId).................: {userVM.OrgId}"); Console.WriteLine(logSnippet + $"(userVM.ManagerId).............: {userVM.ManagerId}"); Console.WriteLine(logSnippet + $"(userVM.EmailAddress)..........: {userVM.EmailAddress}"); Console.WriteLine(logSnippet + $"(userVM.DisplayName)...........: {userVM.DisplayName}"); Console.WriteLine(logSnippet + $"(selectedRoleIdsForUser.Length): {selectedRoleIdsForUser.Length}"); Console.WriteLine(logSnippet + $"(ModelState.IsValid)...........: {ModelState.IsValid}"); foreach (string selectedRoleIdForUser in selectedRoleIdsForUser) { Console.WriteLine(logSnippet + $"(selectedRoleIdForUser): {selectedRoleIdForUser}"); } /////////////////////////////////////////////////////////////////////////////////// // User's Role Selection Validation /////////////////////////////////////////////////////////////////////////////////// if (selectedRoleIdsForUser == null || selectedRoleIdsForUser.Length == 0) { Console.WriteLine(logSnippet + "No roles selected for update user. Adding a model error."); ModelState.AddModelError(string.Empty, "Please select at least one role."); } Console.WriteLine(logSnippet + $"(ModelState.IsValid): {ModelState.IsValid}"); if (ModelState.IsValid) { userVM.ShowUpdateUserForm = false; userVM.ShowCreateUserForm = false; userVM.AlertType = UserAdminConstants.AlertTypeConstants.SUCCESS; userVM.ShowAlert = true; userVM.AlertMessage = $"QMS user identified by '{userVM.DisplayName} - [{userVM.EmailAddress}]' has been successfully updated."; HttpContext.Session.SetObject(UserAdminConstants.UA_USER_VIEW_MODEL, userVM); return(RedirectToAction("Index", "User")); } return(View()); }
public IActionResult Index() { ///////////////////////////////////////////////////////////////////////////////////////// // AUTHENTICATION CHECK ///////////////////////////////////////////////////////////////////////////////////////// if (HttpContext == null || HttpContext.Session == null || HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY) == null) { return(RedirectToAction("Warning", "Home")); } string logSnippet = new StringBuilder("[") .Append(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")) .Append("][UserController][Index][HttpGet] => ") .ToString(); UserViewModel qmsUserVM = HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY); Console.WriteLine(logSnippet + $"(qmsUserVM == null): {qmsUserVM == null}"); if (qmsUserVM == null) { Console.WriteLine(logSnippet + "QMS User NOT IN SESSION, redirecting to [LoginConroller][LogoutAsync]"); return(RedirectToAction("LogoutAsync", "Login")); } Console.WriteLine(logSnippet + $"(qmsUserVM.IsSysAdmin): {qmsUserVM.IsSysAdmin}"); Console.WriteLine(logSnippet + $"(qmsUserVM)...........: {qmsUserVM}"); ///////////////////////////////////////////////////////////////////////////////////////// // AUTHORIZATION CHECK ///////////////////////////////////////////////////////////////////////////////////////// if (qmsUserVM.CanRetrieveUser == false) { return(RedirectToAction("UnauthorizedAccess", "Home")); } ///////////////////////////////////////////////////////////////////////////////////////// // BEGIN USER ADMIN LOGIC ///////////////////////////////////////////////////////////////////////////////////////// UAUserViewModel userVM = HttpContext.Session.GetObject <UAUserViewModel>(UserAdminConstants.UA_USER_VIEW_MODEL); Console.WriteLine(logSnippet + $"(userVM == null): {userVM == null}"); //////////////////////////////////////////////////////////////////////////////////////////////////// // RENDER CREATE USER FORM //////////////////////////////////////////////////////////////////////////////////////////////////// if (userVM != null && userVM.ShowCreateUserForm == true) { // ORGANIZATIONS List <Organization> activeOrganizations = _organizationService.RetrieveActiveOrganizations(); ViewBag.ActiveOrganizations = new SelectList(activeOrganizations, "OrgId", "OrgLabel"); // POTENTIAL MANAGERS //List<User> allActiveUsers = _userService.RetrieveActiveUsers(); List <User> usersInOrg = new List <User>(); ViewBag.PotentialManagers = new SelectList(usersInOrg, "UserId", "DisplayLabel"); HttpContext.Session.Remove(UserAdminConstants.UA_USER_VIEW_MODEL); return(View(userVM)); } //////////////////////////////////////////////////////////////////////////////////////////////////// // RENDER UPDATE USER FORM //////////////////////////////////////////////////////////////////////////////////////////////////// if (userVM != null && userVM.ShowUpdateUserForm == true) { // ORGANIZATIONS List <Organization> activeOrganizations = _organizationService.RetrieveActiveOrganizations(); ViewBag.ActiveOrganizations = new SelectList(activeOrganizations, "OrgId", "OrgLabel", userVM.OrgId); // POTENTIAL MANAGERS List <User> usersInOrg = _userService.RetrieveUsersByOrganizationId(userVM.OrgId.Value); if (userVM.ManagerId.HasValue) { ViewBag.PotentialManagers = new SelectList(usersInOrg, "UserId", "DisplayLabel", userVM.ManagerId); } else { ViewBag.PotentialManagers = new SelectList(usersInOrg, "UserId", "DisplayLabel"); } HttpContext.Session.Remove(UserAdminConstants.UA_USER_VIEW_MODEL); return(View(userVM)); } //////////////////////////////////////////////////////////////////////////////////////////////////// // RENDER ALERT, ADVISING OF SUCCESSFUL USER CREATE OR UPDATE TRANSACTION //////////////////////////////////////////////////////////////////////////////////////////////////// if (userVM != null && userVM.ShowAlert == true) { HttpContext.Session.Remove(UserAdminConstants.UA_USER_VIEW_MODEL); return(View(userVM)); } //////////////////////////////////////////////////////////////////////////////////////////////////// // RENDER SEARCH USER FORM //////////////////////////////////////////////////////////////////////////////////////////////////// userVM = new UAUserViewModel(); userVM.SearchUserSuccessful = false; userVM.ShowUpdateUserForm = false; userVM.ShowCreateUserForm = false; userVM.AspAction = null; userVM.ShowAlert = false; userVM.AlertMessage = null; userVM.UserAdminModule = UserAdminConstants.UserAdminModuleConstants.ACTIVE_USER; userVM.UserNavItemNavLink = UserAdminConstants.UserAdminCssConstants.ACTIVE_NAVITEM_NAVLINK_VALUE; userVM.UserTabPadFade = UserAdminConstants.UserAdminCssConstants.ACTIVE_TABPANE_FADE_VALUE; return(View(userVM)); }
public IActionResult Search(string userSearchEmailAddress) { if (HttpContext == null || HttpContext.Session == null || HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY) == null) { return(RedirectToAction("Warning", "Home")); } string logSnippet = new StringBuilder("[") .Append(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")) .Append("][UserController][Search][HttpPost] => ") .ToString(); User qmsUser = HttpContext.Session.GetObject <User>(MiscConstants.USER_SESSION_KEY); UserViewModel qmsUserVM = HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY); Console.WriteLine(logSnippet + $"(userSearchEmailAddress)...: '{userSearchEmailAddress}'"); Console.WriteLine(logSnippet + $"(qmsUser == null)..: {qmsUser == null}"); Console.WriteLine(logSnippet + $"(qmsUserVM == null): {qmsUserVM == null}"); if (qmsUserVM.CanRetrieveUser == false) { return(RedirectToAction("UnauthorizedAccess", "Home")); } UAUserViewModel userVM = null; try { User dbUser = _userService.RetrieveByEmailAddress(userSearchEmailAddress); Console.WriteLine(logSnippet + $"(dbUser == null): {dbUser == null}"); userVM = _userUtil.MapToViewModel(dbUser); Console.WriteLine(logSnippet + $"(userVM == null): {userVM == null}"); userVM.SearchUserSuccessful = true; userVM.ShowUpdateUserForm = true; userVM.ShowCreateUserForm = false; userVM.AspAction = "Update"; userVM.SubmitButtonLabel = "Update"; userVM.CardHeader = "Update QMS User:"******"Create"; userVM.SubmitButtonLabel = "Create"; userVM.CardHeader = "Create QMS User:"******"'{userSearchEmailAddress}' not found."; } HttpContext.Session.SetObject(UserAdminConstants.UA_USER_VIEW_MODEL, userVM); return(RedirectToAction("Index", "User")); }