示例#1
0
        public async Task <IActionResult> DeleteRole(string userName, ModifyRolesDTO dto)
        {
            dto.RequestingUser = HttpContext.User;
            var user = await _userService.RemoveRoleFromUserAsync(dto);

            return(Ok(user));
        }
示例#2
0
        private async Task <bool> ValidateModifyRolesDTO(ModifyRolesDTO dto)
        {
            if (dto == null)
            {
                throw new ApplicationException("The data sent was invalid, please check the formatting or contact an administrator if you think this is an error.");
            }

            if (dto.Name == null)
            {
                throw new ApplicationException("The name field in the data sent was empty.");
            }

            var user = await FindUserAsync(dto.Name);

            foreach (var role in dto.Roles)
            {
                if (!await _roleManager.RoleExistsAsync(role))
                {
                    throw new ArgumentNullException(null, $"The given role: {role}, does not exist.");
                }

                if (!dto.RequestingUser.IsInRole(role))
                {
                    throw new ApplicationException($"You are not allowed to modify user {dto.Name}, to the given role: {role}.");
                }
            }

            return(true);
        }
示例#3
0
        public async Task <UserToReturnDTO> RemoveRolesFromUserAsync(ModifyRolesDTO dto)
        {
            await ValidateModifyRolesDTO(dto);

            var user = await FindUserAsync(dto.Name);

            var result = await _userManager.RemoveFromRolesAsync(user, dto.Roles);

            if (result.Succeeded)
            {
                var userToReturn = _mapper.Map <UserToReturnDTO>(user);

                return(userToReturn);
            }

            throw new ApplicationException("Something went wrong trying to remove the given roles, please try again.");
        }
示例#4
0
        public async Task <UserToReturnDTO> RemoveRoleFromUserAsync(ModifyRolesDTO dto)
        {
            var userToReturn = await RemoveRolesFromUserAsync(dto);

            return(userToReturn);
        }
示例#5
0
        public async Task <UserToReturnDTO> AddRoleToUserAsync(ModifyRolesDTO dto)
        {
            var userToReturn = await AddRolesToUserAsync(dto);

            return(userToReturn);
        }