public ActionResult EditCurrentUser(CurrentUserModel model) { if (ModelState.IsValid) { bool emailAddressChanged = false; // The user to edit is the logged in user LayersCmsUser editUser = LoggedInUser; // If the email address has changed, check it's not already in use if (model.EmailAddress != editUser.EmailAddress) { // Search the database for a different user with the new email address LayersCmsUser otherUserMatchingEmailAddress = _userReads.GetByEmailAddress(model.EmailAddress, editUser.Id); // If no match has been found, update the email address, otherwise show an error if (otherUserMatchingEmailAddress == null) { editUser.EmailAddress = model.EmailAddress; emailAddressChanged = true; } else { ModelState.AddModelError("EmailAddress", "This email address is already used by another user"); } } // Check if the custom validation has been passed if (ModelState.IsValid) { // Set the new password if one has been entered if (model.BothPasswordsEntered) { editUser.Password = _hashHelper.HashString(model.Password); } // Save the changes to the user to the database _userWrites.Update(editUser); // If the email address for the current user has changed, the User.Identity.Name must change also, // so the forms authentication cookie must be updated if (emailAddressChanged) { FormsAuthentication.SetAuthCookie(editUser.EmailAddress, false); } // Return to the list of users return RedirectToAction("List"); } } // Validation failed, display the view again return View(model); }
public ActionResult EditCurrentUser() { var model = new CurrentUserModel() { EmailAddress = LoggedInUser.EmailAddress }; return View(model); }