This project is read-only.

Update User has gone crazy

Topics: Developer Forum
Aug 7, 2009 at 10:31 AM

I signed in with admin account. Then I chose to edit one user in Manage Users. In Edit User Roles, only User role was checked. After Update User button was clicked, I saw that all roles were checked. It is very strange.

Aug 13, 2009 at 9:22 AM

Hi,

I fixed this bug. Code has been changed in UserController.cs.

[AcceptVerbs("POST")]
        public ActionResult EditUser(string id, bool approved)
        {
            //Is a list of all the user roles
            ArrayList removeRoleList = new ArrayList(Roles.GetAllRoles());

            //We are requesting the form variables directly from the form
            foreach (string key in Request.Form.Keys)
            {
                if (key.StartsWith("role."))
                {
                    String userRole = key.Substring(5, key.Length - 5);
                                        
                    if (Request.Form[key] == "true" || Request.Form[key] == "true,false")
                    {
                        removeRoleList.Remove(userRole);
                        if (!Roles.IsUserInRole(id, userRole))
                        {
                            Roles.AddUserToRole(id, userRole);
                        }
                    }
                    else
                        if (Request.Form[key] == "false")
                        {
                            
                        }    
                }
            }

            foreach (string removeRole in removeRoleList)
            {
                if (Roles.IsUserInRole(id, removeRole))
                {
                    Roles.RemoveUserFromRole(id, removeRole);
                }
            }

            MembershipUser membershipUser = Membership.GetUser(id);
            membershipUser.IsApproved = approved;
            Membership.UpdateUser(membershipUser);

            TempData["SuccessMessage"] = "User Information has been updated";
            ViewData["roles"] = (String[])Roles.GetAllRoles();
            ViewData["PageTitle"] = "Edit " + id;
            return View(membershipUser);
        }