//TODO This shouldn't be user. that's the current user signed in, not the user for edit. public ActionResult Edit(EditViewModelUsersManager model, IEnumerable <string> SelectedCurrentClients, IEnumerable <string> SelectedOtherClients ) { if (ModelState.IsValid) { List <License> userLicenses = db.Licenses.Where(s => s.UserID == model.ID).ToList(); //Make sure the user has at least one license associated with them in order to assign RightsID. if (userLicenses != null) { foreach (var userLicense in userLicenses) { userLicense.RightsID = model.RightID.Value; } } if ((userLicenses.Count() != 0 && model.RightID == 0) //User does have a licenses and no RightId assigned || //User doesn't have any licenses (userLicenses.Count() == 0 && ((model.SelectedOtherClients == null && //No clients are being assigned model.RightID != 0) //RightsID has been selected. || (model.SelectedOtherClients != null && //Clients are being assigned model.RightID == 0)))) //No RightsID has been selected. { //TODO: This needs to be handled on front end. RightID is none null on licesnse table. return(RedirectToAction("Edit")); } //List of Clients to be removed from user association if (model.SelectedCurrentClients != null) { foreach (var selectedCurrentClient in model.SelectedCurrentClients) { foreach (var userLicense in userLicenses) { if (userLicense.ClientID.Value == Convert.ToInt32(selectedCurrentClient)) { userLicense.ClientID = null; userLicense.UserID = null; } } } } //List of Clients to be added to user association if (model.SelectedOtherClients != null) { List <License> unassignedLicenses = db.Licenses.Where(s => s.UserID == null).ToList(); if (unassignedLicenses.Count() <= 5) { NewLicenseMaker(5); unassignedLicenses = db.Licenses.Where(s => s.UserID == null).ToList(); } int unassignedLicensesCounter = 0; foreach (var selectedCurrentClient in model.SelectedOtherClients) { unassignedLicenses[unassignedLicensesCounter].ClientID = Convert.ToInt32(selectedCurrentClient); unassignedLicenses[unassignedLicensesCounter].RightsID = model.RightID.Value; unassignedLicenses[unassignedLicensesCounter].UserID = model.ID; unassignedLicensesCounter++; } } User user = db.Users.Find(model.ID); // Save any new UserName, MustChange, Email user.UserName = model.UserName; user.Active = model.Active; user.MustChange = model.MustChange; user.Email = model.Email; db.SaveChanges(); return(RedirectToAction("Index")); } //Postback the model with attribute errors else { //Error in from return(RedirectToAction("Edit")); } }
// GET: CCS/UsersManager/Edit/5 public async Task <ActionResult> Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } User user = await db.Users.FindAsync(id); if (user == null) { return(HttpNotFound()); } else { List <SelectListItem> currentClients = new List <SelectListItem>(); List <int> currentClientIDs = new List <int>(); List <SelectListItem> otherClients = new List <SelectListItem>(); //Retrieves list of all the licenses associated with the user List <License> userLicenses = db.Licenses.Where(s => s.UserID == user.ID).ToList(); //In case the User isn't currently assigned to any accounts if (userLicenses.Count > 0) { //Retrieves list of current clientIDs and clientNames foreach (var userLicense in userLicenses) { int clientID = userLicense.ClientID.GetValueOrDefault(0); Client client = db.Clients.Where(s => s.ID == clientID).FirstOrDefault(); string clientName = client.Name; var item = new SelectListItem { Value = clientID.ToString(), Text = clientName, Selected = false }; currentClients.Add(item); currentClientIDs.Add(clientID); } } else { var item = new SelectListItem { Value = "", Text = "", Selected = false }; currentClients.Add(item); } List <Client> clients = db.Clients.ToList(); foreach (var client in clients) { if (!currentClientIDs.Contains(client.ID)) { var item = new SelectListItem { Value = client.ID.ToString(), Text = client.Name, Selected = false }; otherClients.Add(item); } } EditViewModelUsersManager model = new EditViewModelUsersManager(); var dbRightsData = db.Rights.ToList(); string userRightName = ""; try { var userLicense = userLicenses[0]; var userRightsId = userLicense.RightsID; var userRight = db.Rights.Where(s => s.ID == userRightsId).FirstOrDefault(); userRightName = userRight.Right1; } catch { userRightName = "null"; } model.Active = user.Active; model.ID = user.ID; model.CurrentClients = currentClients.OrderBy(s => s.Text); model.OtherClients = otherClients.OrderBy(s => s.Text); model.Rights = GetSelectRightsListItems(dbRightsData, userRightName); model.UserName = user.UserName; model.MustChange = user.MustChange; model.Email = user.Email; //Must be set to null in order for the default selected to work. model.RightID = null; return(View(model)); } }