public ActionResult AccountEdit(VMEditingUser model) { bool isValidInput = true; List<ContactInfo> contacts = new List<ContactInfo>(); if (!AllContactInfosVerified(out contacts)) { isValidInput = false; } List<Education> educations = new List<Education>(); if (!AllEducationsVerified(out educations)) { isValidInput = false; } List<Link> links = new List<Link>(); if (!AllLinksVerified(out links)) { isValidInput = false; } if (!string.IsNullOrEmpty(model.NewPassword)) { if (model.NewPassword.Length < 6) { isValidInput = false; ModelState.AddModelError("NewPassword", "New Password must contain at least six characters."); } else if (string.IsNullOrEmpty(model.ConfirmNewPassword) || !model.NewPassword.Equals(model.ConfirmNewPassword)) { isValidInput = false; ModelState.AddModelError("ConfirmNewPassword", "New Password and Confirm New Password do not match."); } } if (isValidInput && ModelState.IsValid) { if (!WebSecurity.Login(model.Email, model.CurrentPassword)) { isValidInput = false; ModelState.AddModelError("CurrentPassword", "The Password you entered is incorrect."); } else { if (!string.IsNullOrEmpty(model.NewPassword)) { if (!WebSecurity.ChangePassword(model.Email, model.CurrentPassword, model.NewPassword)) { isValidInput = false; ModelState.AddModelError("NewPassword", "Could not change password to the new one provided. Please try again with a different password."); } } } } #region Update User's Properties if (isValidInput && ModelState.IsValid) { int UserId = WebSecurity.CurrentUserId; PortfolioUnleashed.User updatedUser = db.retrieveUser(UserId); updatedUser.FirstName = model.FirstName; updatedUser.LastName = model.LastName; db.updateUser(updatedUser); foreach (ContactInfo c in contacts) { if (c.UserId == UserId)//It's an existing entry { if (string.IsNullOrEmpty(c.Title) && string.IsNullOrEmpty(c.Information)) { db.deleteContactInfo(c, UserId); } else { db.updateContactInfo(c); } } else//new entry { db.addContactInfo(c, UserId); } } foreach (Education e in educations) { if (e.UserId == UserId)//It's an existing entry { if (string.IsNullOrEmpty(e.School) && string.IsNullOrEmpty(e.Degree)) { //They blanked out the fields, want to remove the entry db.deleteEducation(e, UserId); } else { //edit the user's education info via e.Id and UserId db.updateEducation(e, UserId); } } else//new entry { db.addEducation(e, UserId); } } foreach (Link l in links) { if (l.UserId == UserId)//It's an existing entry { if (string.IsNullOrEmpty(l.DisplayText) && string.IsNullOrEmpty(l.URL)) { //They blanked out the fields, want to remove the entry db.deleteLink(l, UserId); } else { //edit the user's education info via e.Id and UserId db.updateLink(l, UserId); } } else//new entry { db.addLink(l, UserId); } } return RedirectToAction("Account", "User", new {id = UserId}); } #endregion User u = new User(); u.Id = model.UserId; u.FirstName = model.FirstName; u.LastName = model.LastName; u.Email = model.Email; u.ContactInfoes = contacts; u.Links = links; u.Educations = educations; return View(new VMEditingUser(u)); }
public static User userFromVMEditingUser(VMEditingUser vmUser) { User user = new User() { FirstName = vmUser.FirstName, LastName = vmUser.LastName, Email = vmUser.Email, ContactInfoes = contactInfoListFromVMContactInfoList(vmUser.ContactInfos), Educations = educationListFromVMEducationList(vmUser.Education), Links = linkListFromVMLinkList(vmUser.Links), Id = vmUser.UserId, }; return user; }
public ActionResult AccountEdit(int id) { if (RouteData.Values["id"] != null) { if (int.TryParse(RouteData.Values["id"].ToString(), out id)) { } } PortfolioUnleashed.User user = db.retrieveUser(id); if (user == null) { string error1 = "The User Account you tried to edit either does not exist or could not be found."; string error2 = "User Id: " + id; ViewBag.ErrorMessages = new string[] { error1, error2 }; return View("PageNotFound"); } VMEditingUser userToEdit = new VMEditingUser(user); return View(model: userToEdit); }