Пример #1
0
        public ActionResult Edit(int id)
        {
            // Redirect to the current user edit action if the
            // id matches that of the logged in user
            if (id == LoggedInUser.Id)
            {
                return RedirectToAction("EditCurrentUser");
            }

            // Retrieve the user to edit from the database
            LayersCmsUser user = _userReads.GetById(id);

            // If the user doesn't exist redirect to the list action
            if (user == null)
                return RedirectToAction("List");

            // Return the view pre-populated with the user data
            var model = new EditUserModel()
                {
                    Active = user.Active,
                    EmailAddress = user.EmailAddress,
                    Id = user.Id
                };

            return View(model);
        }
Пример #2
0
        public ActionResult Edit(int id, EditUserModel model)
        {
            if (ModelState.IsValid)
            {
                // Retrieve the user to edit from the database
                LayersCmsUser editUser = _userReads.GetById(id);

                // Update the 'active' status for the user
                editUser.Active = model.Active;

                // 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, id);

                    // If no match has been found, update the email address, otherwise show an error
                    if (otherUserMatchingEmailAddress == null)
                    {
                        editUser.EmailAddress = model.EmailAddress;
                    }
                    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);

                    // Return to the list of users
                    return RedirectToAction("List");
                }
            }

            // Validation failed, display the view again
            return View(model);
        }