public async Task <IActionResult> DeleteRole(string userName, ModifyRolesDTO dto) { dto.RequestingUser = HttpContext.User; var user = await _userService.RemoveRoleFromUserAsync(dto); return(Ok(user)); }
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); }
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."); }
public async Task <UserToReturnDTO> RemoveRoleFromUserAsync(ModifyRolesDTO dto) { var userToReturn = await RemoveRolesFromUserAsync(dto); return(userToReturn); }
public async Task <UserToReturnDTO> AddRoleToUserAsync(ModifyRolesDTO dto) { var userToReturn = await AddRolesToUserAsync(dto); return(userToReturn); }