// // GET: /Account/Manage public ActionResult Manage(ManageMessageId? message) { ViewBag.StatusMessage = message == ManageMessageId.AccountChangesSaved ? "Your account changes were saved" : ""; ViewBag.HasLocalPassword = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name)); ViewBag.ReturnUrl = Url.Action("Manage"); if (message == ManageMessageId.ChangePasswordSuccess) { ViewBag.changesSaved = true; } else { ViewBag.changesSaved = false; } try { if (WebSecurity.IsAuthenticated == false) { //Current user is not authorized to manage this account WebSecurity.Logout(); return RedirectToAction("Login"); } using (var db = new CopiosisEntities()) { var dbCurrentUser = db.users.Where(p => p.userID == WebSecurity.CurrentUserId).FirstOrDefault(); if (dbCurrentUser == null) { ACCOUNTERROR.ErrorSubject = "Error while trying to retrieve your user account"; throw new Exception(string.Format("No match for the current user with user name {0}", WebSecurity.CurrentUserId)); } AccountManagerModel model = new AccountManagerModel(); model.errorList = new Dictionary<string, string>(); user CurrentUser = db.users.Where(p => p.userID == WebSecurity.CurrentUserId).FirstOrDefault(); model.currentEmail = CurrentUser.email; model.currentFirstName = CurrentUser.firstName; model.currentLastName = CurrentUser.lastName; ViewBag.isValidatedUser = true; return View(model); } } catch (Exception e) { ACCOUNTERROR.ErrorSubject = "Error when trying to access your account"; if (e.InnerException is InvalidOperationException) { throw new Exception("You do not have an account. Please register with Copiosis."); } throw new Exception(e.Message); } }
public ActionResult Manage(AccountManagerModel model) { if (ModelState.IsValid) { using (var db = new CopiosisEntities()) { var dbCurrentUser = db.users.Where(p => p.userID == WebSecurity.CurrentUserId).FirstOrDefault(); if (dbCurrentUser == null) { ACCOUNTERROR.ErrorSubject = "Error while trying to retrieve your user account"; throw new Exception(string.Format("No match for the current user with user name {0}", WebSecurity.CurrentUserId)); } ViewBag.isValidatedUser = true; string passwordTemp; bool changePassword; bool noPwProvided; validateManageAccountForm(model, db, dbCurrentUser, out passwordTemp, out changePassword, out noPwProvided); if (ModelState.IsValid == true) { if (changePassword == true) { // ChangePassword will throw an exception rather than return false in certain failure scenarios. bool changePasswordSucceeded = true; try { changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, passwordTemp, model.newPassword); } catch (Exception) { changePasswordSucceeded = false; } if (changePasswordSucceeded == false) { ACCOUNTERROR.ErrorSubject = "Error while trying to update your account"; throw new Exception("Could not change your password"); } else { try { WebSecurity.Login(dbCurrentUser.username, passwordTemp); passwordTemp = model.newPassword; } catch (Exception e) { ACCOUNTERROR.ErrorSubject = "Error when logging you in"; throw new Exception(e.Message); } } } db.SaveChanges(); ViewBag.changesSaved = true; return RedirectToAction("Manage", new { Message = ManageMessageId.AccountChangesSaved }); } else { //there was at least one error: ViewBag.changesSaved = false; return View(model); } } } // If we got this far, something failed, redisplay form ViewBag.changesSaved = false; return View(model); }
//Helper method to validate the Manage Account form for the Account/Manage view private void validateManageAccountForm(AccountManagerModel model, CopiosisEntities db, user dbCurrentUser, out string passwordTemp, out bool changePassword, out bool noPwProvided) { string email = model.emailAddress; string firstName = model.firstName; string lastName = model.lastName; string newPassword = model.newPassword; string confirmPassword = model.confirmPassword; string currentPassword = model.currentPassword ?? ""; passwordTemp = new string(currentPassword.ToCharArray()); changePassword = false; noPwProvided = false; model.currentEmail = dbCurrentUser.email; model.currentFirstName = dbCurrentUser.firstName; model.currentLastName = dbCurrentUser.lastName; user conflictUser = null; if (email != null) { conflictUser = db.users.Where(m => m.email == email).FirstOrDefault(); if (conflictUser != null && conflictUser.email.Equals(email)) { ModelState.AddModelError("emailAddress", "That e-mail address is already being used. Please use a different one"); } else { dbCurrentUser.email = email; } } if (firstName != null) { if (firstName.Equals(dbCurrentUser.firstName)) { ModelState.AddModelError("firstName", "Enter a different first name"); } else { dbCurrentUser.firstName = firstName; } } if (lastName != null) { if (lastName.Equals(dbCurrentUser.lastName)) { ModelState.AddModelError("lastName", "Enter a different last name"); } else { dbCurrentUser.lastName = lastName; } } if (newPassword != null) { if (confirmPassword == null) { ModelState.AddModelError("confirmPassword", "Confirmation password cannot be empty"); } else if (!newPassword.Equals(confirmPassword)) { ModelState.AddModelError("confirmPassword", "Confirmation password and new password do not match"); } else if (newPassword.Equals(model.currentPassword)) { ModelState.AddModelError("newPassword", "Your new password cannot be the same as your current password"); } else { changePassword = true; } } if (model.currentPassword == null) { ModelState.AddModelError("currentPassword", "Please enter your current password to commit to the change(s)"); noPwProvided = false; } else if ((Membership.Provider.ValidateUser(db.users.Where(m => m.userID == WebSecurity.CurrentUserId).FirstOrDefault().username, model.currentPassword) == false)) { ModelState.AddModelError("currentPassword", "You entered the wrong current password"); } //build the error list if (model.errorList == null) { model.errorList = new Dictionary<string, string>(); } if (ModelState.IsValid == false) { int i = 0; foreach (ModelState state in ModelState.Values) { if (state.Errors.Count >= 1) { model.errorList.Add(ModelState.Keys.ElementAt(i), state.Errors[0].ErrorMessage); } ++i; } } }