public ActionResult Edit(ChangeAccountInfoModel model) { if (ModelState.IsValid) { // ChangePassword will throw an exception rather // than return false in certain failure scenarios. bool changePasswordSucceeded; var needToChangePassword = !String.IsNullOrEmpty(model.OldPassword) && !String.IsNullOrEmpty(model.NewPassword); try { changePasswordSucceeded = !needToChangePassword || _webSecurity.ChangePassword(UserHelper.CustomerSession.Username, model.OldPassword, model.NewPassword); } catch (Exception) { changePasswordSucceeded = false; } var u = _userClient.GetCurrentCustomer(false) ?? _userClient.NewContact(); u.FullName = model.FullName; var primaryEmail = u.Emails.FirstOrDefault(e => e.Type == EmailType.Primary.ToString()); if (primaryEmail != null) { primaryEmail.Address = model.Email; } else { var newEmail = new Email { Address = model.Email, MemberId = u.MemberId, Type = EmailType.Primary.ToString() }; u.Emails.Add(newEmail); } _userClient.SaveCustomerChanges(u.MemberId); if (needToChangePassword) { if (changePasswordSucceeded) { TempData[GetMessageTempKey(MessageType.Success)] = new[] { "Password was succesfully changed!".Localize() }; return RedirectToAction("Index"); } ModelState.AddModelError("", @"The current password is incorrect or the new password is invalid."); } else { TempData[GetMessageTempKey(MessageType.Success)] = new[] { "Your account was succesfully updated!".Localize() }; return RedirectToAction("Index"); } } // If we got this far, something failed, redisplay form return View("Edit", model); }
public ActionResult Edit(bool changePassword = false) { var contact = _userClient.GetCurrentCustomer(); var model = UserHelper.GetCustomerModel(contact); var chModel = new ChangeAccountInfoModel(); chModel.InjectFrom(model); chModel.FullName = chModel.FullName ?? UserHelper.CustomerSession.CustomerName; chModel.ChangePassword = changePassword; return View(chModel); }