// Return list of users based on role passed in private static List <AdmUserViewModel> HydrateUsers(string role) { List <AdmUserViewModel> users = new List <AdmUserViewModel>(); using (ApplicationDbContext db = new ApplicationDbContext()) { List <ApplicationUser> allUsers = db.Users.ToList(); UserStore <ApplicationUser> userStore = new UserStore <ApplicationUser>(db); UserManager <ApplicationUser> userManager = new UserManager <ApplicationUser>(userStore); // clean up - move creation and all assignment inside of test foreach (ApplicationUser u in allUsers) { AdmUserViewModel user = new AdmUserViewModel(); user.LastName = u.LastName; user.FirstName = u.FirstName; user.Email = u.Email; user.UserId = u.Id; user.UserName = u.UserName; user.Role = userManager.IsInRole(u.Id, "Admin") ? "Admin" : userManager.IsInRole(u.Id, "Technician") ? "Technician" : "General"; if (user.Role == role || role == "All") { users.Add(user); } } return(users); } }
// This is used to update/add user, the only differenct is that if it is a create, the userName will be null public string AddUpdateUser(AdmUserViewModel model, string userId) { string message = ""; using (ApplicationDbContext db = new ApplicationDbContext()) { UserStore <ApplicationUser> userStore = new UserStore <ApplicationUser>(db); UserManager <ApplicationUser> userManager = new UserManager <ApplicationUser>(userStore); ApplicationUser user = null; if (userId != null) // userName passed in so update existing user { user = db.Users.FirstOrDefault(u => u.Id == userId); user.FirstName = model.FirstName; user.LastName = model.LastName; user.Email = model.Email; // Find the current role the user is in, and if it is different than the role coming in remove from role and add in corret role string role = userManager.IsInRole(user.Id, "Admin") ? "Admin" : userManager.IsInRole(user.Id, "Technician") ? "Technician" : "General"; if (role != model.Role) { userManager.RemoveFromRole(user.Id, role); userManager.AddToRole(user.Id, model.Role); } message = "NewOk"; } else // userName is null - create new user { user = new ApplicationUser() { FirstName = model.FirstName, LastName = model.LastName, UserName = model.Email, Email = model.Email }; userManager.Create(user, "123456"); userManager.AddToRole(user.Id, model.Role); user = userManager.FindByName(user.UserName); message = "EditOk"; } try { db.SaveChanges(); } catch (Exception ex) { message = "An error occured while adding/updating a user (" + model.FirstName + " " + model.LastName + ") => " + ex.Message; } } return(message); }
public string GetUserRole(string userId) { string userRole = null; AdmUserViewModel user = GetUserById(userId); if (user != null) { using (ApplicationDbContext db = new ApplicationDbContext()) { UserStore <ApplicationUser> userStore = new UserStore <ApplicationUser>(db); UserManager <ApplicationUser> userManager = new UserManager <ApplicationUser>(userStore); userRole = user.Role = userManager.IsInRole(user.UserId, "Admin") ? "Admin" : userManager.IsInRole(user.UserId, "Technician") ? "Technician" : "General"; } } return(userRole); }
// If userId is null, this is being called with the intention of creating a tech public AdmUserViewModel GetUserById(string userId) { AdmUserViewModel model = new AdmUserViewModel(); if (userId != null) { using (ApplicationDbContext db = new ApplicationDbContext()) { ApplicationUser user = db.Users.FirstOrDefault(u => u.Id == userId); model.FirstName = user.FirstName; model.LastName = user.LastName; model.Email = user.Email; model.UserName = user.UserName; model.UserId = user.Id; } } model.Roles = HydrateUserTypes(); return(model); }
// This is used to update/add technician, the only differenct is that if it is a create, the userName will be null public ActionResult AddUpdateTech(AdmUserViewModel model, string userId) { string result = _ticketAdapter.AddUpdateUser(model, userId); if (result == "NewOk") { TimeLineHelper.UserOperations(model.FirstName + model.LastName, User.Identity.Name, TimeLineHelper.Operations.Edit); } else if (result == "EditOk") { TimeLineHelper.UserOperations(model.FirstName + model.LastName, User.Identity.Name, TimeLineHelper.Operations.New); } else { TimeLineHelper.ErrorOperations(User.Identity.Name, TimeLineHelper.Operations.Edit, result); } return(RedirectToAction("Users", new { role = model.Role })); }
public void AssignTech(AdmIssueViewModel model, int issueId) { if (model.AssignToId == null) { return; } using (ApplicationDbContext db = new ApplicationDbContext()) { // first see if there is already an assignment with issue and tech, if so don't create another Assignment assignment = db.Assignments.FirstOrDefault(a => a.IssueId == issueId && a.UserId == model.AssignToId); if (assignment == null) // did not find assignment { // create assignment assignment = new Assignment() { IssueId = issueId, UserId = model.AssignToId, }; db.Assignments.Add(assignment); Issue issue = db.Issues.FirstOrDefault(i => i.IssueId == issueId); if (issue != null && issue.IsAssigned == false) { issue.IsAssigned = true; } string tech_name = db.Users.Find(model.AssignToId).FirstName; TimeLineHelper.AssigmentOperations(issue.User.UserName, TimeLineHelper.Operations.New, issue.IssueId, tech_name); string ticketId = issue.IssueId.ToString(); AdmUserViewModel tech = GetUserById(model.AssignToId); string tech_email = tech.Email; // email user and tell them that the issue has been updated EmailManager.SendEmailAdminAssignTicket(ticketId, tech_email, issue.IssueDesc); } db.SaveChanges(); } return; }
// If userId is null, this is being called with the intention of creating a tech public ActionResult AddUpdateTech(string userId) { AdmUserViewModel model = _ticketAdapter.GetUserById(userId); return(View(model)); }