Пример #1
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEdtDto)
        {
            var dbUser = await _userManager.FindByNameAsync(userName);

            if (dbUser == null)
            {
                return(BadRequest("User not found"));
            }

            var dbUserRoles = await _userManager.GetRolesAsync(dbUser);

            // roleEditDto will contain the list of role names that we want our user to have (already linked and new ones)
            var selectedRoles = roleEdtDto.RoleNames;

            // selectedRoles = selectedRoles != null ? selectedRoles : new string[] {};
            selectedRoles = selectedRoles ?? new string[] { }; // the same result as the upper line

            // Add the roles that are not already linked
            var result = await _userManager.AddToRolesAsync(dbUser, selectedRoles.Except(dbUserRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            // remove the roles that are alreay linked but not wanted anymore
            result = await _userManager.RemoveFromRolesAsync(dbUser, dbUserRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove the roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(dbUser)));
        }
Пример #2
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            // get user by username
            var user = await _userManager.FindByNameAsync(userName);

            // get roles of that specific user
            var userRoles = await _userManager.GetRolesAsync(user);

            // which roles have been selected
            var selectedRoles = roleEditDto.RoleNames;

            // ?? is referred to as the null coalescing operator
            //  shortform of: selected = selectedRoles != null ? selectedRoles : new string[] {};
            selectedRoles = selectedRoles ?? new string[] { };

            // add user to specific role except those that the user is already assigned to
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            // remove roles that were not selected
            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove the roles"));
            }


            // return roles of the user
            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #3
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            // roles currenty applied
            var userRoles = await _userManager.GetRolesAsync(user);

            // new roles
            var selectedRoles = roleEditDto.RoleNames;

            // need to cover the fact that a user can be removed from all roles

            // ?? null coalescing operator. same as:
            // selectedRoles = selectedRoles != null? selectedRoles : new string[] {};
            selectedRoles = selectedRoles ?? new string[] {};
            // add roles not already in the string list
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }
            // remove roles that are not specified in the new roles list
            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove from roles"));
            }
            // return roles now applied after changes
            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #4
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            // get user by userName
            var user = await _userManager.FindByNameAsync(userName);

            // get all roles of user
            var userRoles = await _userManager.GetRolesAsync(user);

            var selectedRoles = roleEditDto.RoleNames;


            // selectedRoles = selectedRoles != null ? selectedRoles : new string[] {}
            selectedRoles = selectedRoles ?? new string[] { };
            // add new role for user, except userRoles already
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }
            // remove roles of user, except selectedRoles
            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove the roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #5
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            // which Roles the user belongs to
            var userRoles = await _userManager.GetRolesAsync(user);

            // which Roles has been selected
            var selectedRoles = roleEditDto.RoleNames;

            // user not exist or exist  Roles
            // user remove from all Roles
            //selectedRoles = selectedRoles != null ? selectedRoles : new string[] {};
            selectedRoles = selectedRoles ?? new string[] { }; // if its not Null(has Role may be "admin","member", etc) use the selectedRoles else use new string[] {}
            // add the user and the role except the existing role
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to role"));
            }

            // when we need to remove a Role have been selected from the list//
            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to Remove the Roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #6
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            var userRoles = await _userManager.GetRolesAsync(user);

            // get roles passed in body with post request
            var selectedRoles = roleEditDto.RoleNames;

            selectedRoles = selectedRoles ?? new string[] {};

            // add new roles, passed in body
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            // if new roles added, remove previous roles
            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove the roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #7
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                return(NotFound());
            }

            var userRoles = await _userManager.GetRolesAsync(user);

            var selectedRoles = roleEditDto.RoleNames;

            selectedRoles = selectedRoles ?? new string[] { };
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add user role(s)."));
            }

            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove user role(s)."));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #8
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            var userRoles = await _userManager.GetRolesAsync(user);

            // There is a chance that the user will be removed from all roles.
            // If they are, then we are just going to create a new empty object
            // of type string array
            var selectedRoles = roleEditDto.RoleNames ?? new string[] { };

            // We need to add in the user to roles that they are not
            // yet a member of
            var result = await _userManager.AddToRolesAsync(user,
                                                            selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles."));
            }

            // We need to remove in the roles that has been deselected
            // from the list in the SPA
            result = await _userManager.RemoveFromRolesAsync(user,
                                                             userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove the roles."));
            }

            // Return the new list of roles the user is a member of
            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #9
0
        public async Task <IActionResult> EditRoles(string username, RoleEditDto editedRoles)
        {
            var resultUser = await _userManager.FindByNameAsync(username);

            if (resultUser == null)
            {
                return(BadRequest("user does not exist"));
            }

            var existedRoles = await _userManager.GetRolesAsync(resultUser);

            var selectedRoles = editedRoles.RolesName;

            selectedRoles = selectedRoles ?? new string[] { };

            var result = await _userManager.AddToRolesAsync(resultUser, selectedRoles.Except(existedRoles));

            if (result.Succeeded)
            {
                result = await _userManager.RemoveFromRolesAsync(resultUser, existedRoles.Except(selectedRoles));

                if (!result.Succeeded)
                {
                    return(BadRequest("failed to remove the roles"));
                }

                return(Ok(await _userManager.GetRolesAsync(resultUser)));
            }

            return(BadRequest("failed to add roles"));
        }
Пример #10
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            var userRoles = await _userManager.GetRolesAsync(user);

            var selectedRoles = roleEditDto.RoleNames;

            // selected = selctedRoles != null ? selectedRoles : new string[] {}
            // if thing on the left of null-coalescing operator is null chose right side otherwise left
            selectedRoles = selectedRoles ?? new string[] { };
            // except resturns new collection with elements from the first collection which don't exist in 2nd collection
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove the roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            // get the user
            var user = await _userManager.FindByNameAsync(userName);

            // get the current user Roles
            var userRoles = await _userManager.GetRolesAsync(user);

            // get roles which are to be added/removed
            var selectedRoles = roleEditDto.RoleNames;

            // selectedRoles = selectedRoled != null ? selectedRoles : new string[] {};
            // modified using null coalescing operator for checking null
            selectedRoles = selectedRoles ?? new string[] { };

            // add the roles expect the ones which are already added to the user
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            // remove the roles which are no selected by admin
            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove to roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #12
0
        public async Task <IActionResult> EditRoles(string username, [FromBody] RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(username);

            //get roles
            var userRoles = await _userManager.GetRolesAsync(user);

            var selectedRole = roleEditDto.RoleNames;

            // selectedRole != null ? selectedROles : new string[] {};
            selectedRole = selectedRole ?? new string[] { };

            //add the roles to the user eexcept the one that already exist
            var result = await _userManager.AddToRolesAsync(user, selectedRole.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            //remove the ones that has been removed
            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRole));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove to roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #13
0
        public async Task <IActionResult> EditRoles(string userName, [FromBody] RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            var userRoles = await _userManager.GetRolesAsync(user);

            var selectedRoles = roleEditDto.RoleNames;

            // selectedRoles != null ? selectedRoles : new string[] {};
            selectedRoles = selectedRoles ?? new string[] { };

            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove the roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #14
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            var userRoles = await _userManager.GetRolesAsync(user);

            var selectedRoles = roleEditDto.RoleNames;

            // if all roles are removed from a user, use a null coalesscing operator which does the below
            // selected = selectedRoles != null ? selectedRoles : new string[] {}
            // if what ever ot the left of ?? is not null then use this, otherwise use whats ever to the right
            selectedRoles = selectedRoles ?? new string[] { };

            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #15
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await userManager.FindByNameAsync(userName);

            if (ReferenceEquals(user, null))
            {
                return(BadRequest("Unknown user name"));
            }

            var userRoles = await userManager.GetRolesAsync(user);

            var selectedRoles = roleEditDto.RoleNames;

            selectedRoles = selectedRoles ?? new string[] { };
            var result = await userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            result = await userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove roles"));
            }

            return(Ok(await userManager.GetRolesAsync(user)));
        }
Пример #16
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await this.userManager.FindByNameAsync(userName);

            var userRoles = await this.userManager.GetRolesAsync(user);

            var selectedRoles = roleEditDto.RoleNames;

            selectedRoles = selectedRoles ?? new string [] {};
            var result = await this.userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            result = await this.userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            return(Ok(await this.userManager.GetRolesAsync(user)));
        }
Пример #17
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                throw new Exception("This user does not exist");
            }

            var userRoles = await _userManager.GetRolesAsync(user);

            var selectedRoles = roleEditDto.RoleNames;

            //in case the enduser deletes all his roles so it could be null
            selectedRoles = selectedRoles ?? new string[] { };

            /* // seems to be quick and reckless?
             * var result = await _userManager.RemoveFromRolesAsync(user, userRoles);
             * if (!result.Succeeded)
             *  return BadRequest("Failed to remove the roles");
             *
             * result = await _userManager.AddToRolesAsync(user, selectedRoles);
             * if (!result.Succeeded)
             *  return BadRequest("Failed to add to roles");
             */
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove the roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #18
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            Console.WriteLine("---------> is roleEditDto null? " + (roleEditDto == null).ToString());
            // Console.WriteLine("---------> roleEditDto.RoleNames: " + string.Join(", ", roleEditDto.RoleNames));
            Console.WriteLine("---------> string roleEditDto: " + roleEditDto.ToString());
            // Console.WriteLine("---------> string roleEditDto.RoleNames: " + roleEditDto.RoleNames.ToString());

            var user = await _userManager.FindByNameAsync(userName);

            var userRoles = await _userManager.GetRolesAsync(user);

            // var selectedRoles = roleEditDto.RoleNames;

            var selectedRoles = roleEditDto.RoleNames ?? new string[] { };

            // selectedRoles = selectedRoles != null ? selectedRoles : new string[] {};
            selectedRoles = selectedRoles ?? new string[] { };

            Console.WriteLine("---------> user: "******". Id " + user.Id);
            Console.WriteLine("---------> to add : elements selectedRoles: " + string.Join(", ", selectedRoles));
            Console.WriteLine("---------> to add : elements userRoles: " + string.Join(", ", userRoles));
            Console.WriteLine("---------> to add : elements selectedRoles.Except(userRoles): " + string.Join(", ", selectedRoles.Except(userRoles)));
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add the roles"));
            }

            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            Console.WriteLine("---------> to remove : elements userRoles.Except(selectedRoles): " + string.Join(", ", userRoles.Except(selectedRoles)));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove the roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #19
0
        public async Task <IActionResult> EditUserRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await userManager.FindByNameAsync(userName);

            var existingUserRoles = await userManager.GetRolesAsync(user);

            var resultRemove = await userManager.RemoveFromRolesAsync(user, existingUserRoles);

            if (resultRemove.Succeeded == false)
            {
                return(BadRequest($"Error removing {userName}'s old roles"));
            }

            var newRoles = roleEditDto.RoleNames ?? new string[] { };

            var resultAddRoles = await userManager.AddToRolesAsync(user, newRoles);

            if (resultAddRoles.Succeeded == false)
            {
                return(BadRequest($"Error updating {userName}'s roles"));
            }
            return(Ok(await userManager.GetRolesAsync(user)));
        }
Пример #20
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            var userRoles = await _userManager.GetRolesAsync(user);

            var selectedRoles = roleEditDto.RoleNames;

            selectedRoles = selectedRoles ?? new string[] { };
            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Ocurrió un error al agregar los roles"));
            }
            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Ocurrió un error al eliminar los roles"));
            }
            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #21
0
        public async Task <IActionResult> UpdateUserRoles(int id, RoleEditDto dto)
        {
            var user = await _userManager.FindByIdAsync(id.ToString());

            var userRoles = await _userManager.GetRolesAsync(user);

            var addedRoles       = dto.Roles.Except(userRoles);
            var addRolesResponse = await _userManager.AddToRolesAsync(user, addedRoles);

            if (!addRolesResponse.Succeeded)
            {
                return(BadRequest("Falied to add roles"));
            }

            var removedRoles        = userRoles.Except(dto.Roles);
            var removeRolesResponse = await _userManager.RemoveFromRolesAsync(user, removedRoles);

            if (!removeRolesResponse.Succeeded)
            {
                return(BadRequest("Falied to remove roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #22
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            var user = await _userManager.FindByNameAsync(userName);

            var userRole = await _userManager.GetRolesAsync(user);

            var selectedRoles = roleEditDto.rolesName ?? new string[] {};

            var result = await _userManager.AddToRolesAsync(user, selectedRoles.Except(userRole));

            if (!result.Succeeded)
            {
                BadRequest("Fail to add user roles.");
            }

            result = await _userManager.RemoveFromRolesAsync(user, userRole.Except(selectedRoles));

            if (!result.Succeeded)
            {
                BadRequest("Fail to remove user role");
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }
Пример #23
0
        public async Task <IActionResult> EditRoles(string userName, RoleEditDto roleEditDto)
        {
            // lookup user by name
            var user = await _userManager.FindByNameAsync(userName);

            //find current user roles
            var userRoles = await _userManager.GetRolesAsync(user);

            // selected roles
            var selectedRoles = roleEditDto.RoleNames;

            // in case of all roles are removed (selection is NONE)
            // ?? - null coalescing operator
            // selectedRoles = selectedRoles != null ? selectedRoles : new string[] {};
            selectedRoles = selectedRoles ?? new string[] { };

            //add roles that user doesn't have
            var result = await _userManager.AddToRolesAsync(user,
                                                            selectedRoles.Except(userRoles)); //except those roles that user has

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            //remove roles that has been de-selected
            result = await _userManager.RemoveFromRolesAsync(user,
                                                             userRoles.Except(selectedRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove the roles"));
            }

            return(Ok(await _userManager.GetRolesAsync(user)));
        }