示例#1
0
        public ActionResult Edit(string username, [Bind("Gender", "UserName", "Email, IsAdmin")] User user)
        {
            if (!IsAdminConnected())
            {
                return(RedirectToAction("Index", "Error", new { error = "You must be an admin to edit." }));
            }

            try
            {
                var errorMessage = GetErrorIfInvalid(user, false);

                if (!string.IsNullOrWhiteSpace(errorMessage))
                {
                    return(RedirectToAction("Index", "Error", new { error = errorMessage }));
                }

                User userToEdit = _userBl.GetById(username);

                if (userToEdit == null)
                {
                    return(RedirectToAction("Index", "Error", new { error = string.Format("Could not find user with username {0}", username) }));
                }

                // in case of last admin make himself not admin
                if (_userBl.GetHowManyAdmins() == 1 && HttpContext.Session.GetString("ConnectedUserId") == user.UserName && user.IsAdmin == false)
                {
                    user.IsAdmin = true;
                }

                userToEdit.Gender   = user.Gender;
                userToEdit.UserName = user.UserName;
                userToEdit.Email    = user.Email;
                userToEdit.IsAdmin  = user.IsAdmin;

                _userBl.UpdateUser(userToEdit);

                return(RedirectToAction("Details", "User"));
            }
            catch
            {
                return(RedirectToAction("Index", "Error"));
            }
        }