public async Task <IActionResult> AssignRolesToUser(string userName, bool createRoleIfNotExists = true, params string[] roleNames) { await _accountsService.AddRolesToUser(userName, createRoleIfNotExists, roleNames); return(Ok()); }
//Delete /api/auth/deleteuser public async Task <IActionResult> DeleteUser([FromBody] DeleteUserViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _accountsService.GetUser(model.Id); if (user == null) { return(new JsonResult(await Errors .GetGenericErrorResponse( new DeleteUserResponse() { Id = model.Id, Email = "no_email", StatusCode = 401, Error = "User not found", Description = String.Format(Constants.APIMessages.NotFoundMessage, model.Id), Code = "user_not_found", }))); } RolesForUser rolesForUser = await _accountsService.GetUserRoles(user); if (rolesForUser.IsNull) { return(new JsonResult(await Errors .GetGenericErrorResponse( new DeleteUserResponse() { Id = "no_id", Email = "no_email", StatusCode = 422, Error = "Get roles for user error", Description = "Unable to get roles for user.", Code = "get_roles_for_user_error", }))); } if (rolesForUser.Roles.Any()) { IdentityResult removeRolesFromUserResult = await _accountsService.RemoveRolesFromUser(user, rolesForUser.Roles); if (!removeRolesFromUserResult.Succeeded) { return(new JsonResult(await Errors .GetGenericErrorResponse( new DeleteUserResponse() { Id = user.Id, Email = user.Email, StatusCode = 422, Error = "Unable to complete delete opretaion of user related roles.", Description = "Roles realted to the current user could not be removed at this time.", Code = "unable_to_complete_delete_operation_of_user_related_roles", }))); } } IdentityResult removeUserResult = await _accountsService.DeleteUser(user); if (!removeUserResult.Succeeded) { IdentityResult reAddRolesToUserResult = await _accountsService.AddRolesToUser(user, rolesForUser.Roles); if (reAddRolesToUserResult.Succeeded) { return(new JsonResult(await Errors.GetGenericErrorResponse( new DeleteUserResponse() { Id = user.Id, Email = user.Email, Code = "unable_to_complete_delete_operation", StatusCode = 422, Description = "User was not deleted. The delete task could not be completed at this time.", Error = "Unable to complete delete opretaion." }))); } return(new JsonResult(await Errors.GetGenericErrorResponse( new DeleteUserResponse() { Id = user.Id, Email = user.Email, Code = "unable_to_complete_delete_operation_USER_DOES_NOT_HAVE_ANY_ROLES", StatusCode = 422, Description = "User was not deleted. The delete task could not be completed at this time. User has no roles assign. Pleas add roles to user for access.", Error = "Unable to complete delete opretaion. User does not have any roles" }))); } await _context.SaveChangesAsync(); return(new OkObjectResult(Wrappyfier.WrapDeleteUserResponse(user.Id, user.Email))); }