From 31b4efd7ab903354bb844d9eec1671dbdaa9dbf0 Mon Sep 17 00:00:00 2001 From: bostonkenne Date: Thu, 17 Jun 2021 15:51:11 +0100 Subject: [PATCH 1/2] add remove user realm role --- keycloak/keycloak_admin.py | 15 ++++++++++++++- keycloak/urls_patterns.py | 4 +++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index ffb5968..4272482 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -47,7 +47,8 @@ from .urls_patterns import URL_ADMIN_SERVER_INFO, URL_ADMIN_CLIENT_AUTHZ_RESOURC URL_ADMIN_REALM_ROLES_MEMBERS, URL_ADMIN_CLIENT_PROTOCOL_MAPPER, URL_ADMIN_CLIENT_SCOPES_MAPPERS, \ URL_ADMIN_FLOWS_EXECUTIONS_EXEUCUTION, URL_ADMIN_FLOWS_EXECUTIONS_FLOW, URL_ADMIN_FLOWS_COPY, \ URL_ADMIN_FLOWS_ALIAS, URL_ADMIN_CLIENT_SERVICE_ACCOUNT_USER, URL_ADMIN_AUTHENTICATOR_CONFIG, \ - URL_ADMIN_CLIENT_ROLES_COMPOSITE_CLIENT_ROLE, URL_ADMIN_CLIENT_ALL_SESSIONS, URL_ADMIN_EVENTS + URL_ADMIN_CLIENT_ROLES_COMPOSITE_CLIENT_ROLE, URL_ADMIN_CLIENT_ALL_SESSIONS, URL_ADMIN_EVENTS,\ + URL_ADMIN_DELETE_USER_ROLE class KeycloakAdmin: @@ -1878,3 +1879,15 @@ class KeycloakAdmin: params_path = {"realm-name": self.realm_name, "id": client_id} data_raw = self.connection.raw_get(URL_ADMIN_CLIENT_ALL_SESSIONS.format(**params_path)) return raise_error_from_response(data_raw, KeycloakGetError) + + + def delete_user_realm_role(self, user_id, payload): + """ + Delete realm-level role mappings + DELETE admin/realms/{realm-name}/users/{id}/role-mappings/realm + + """ + params_path = {"realm-name": self.realm_name, "id": str(user_id) } + data_raw = self.connection.raw_delete(URL_ADMIN_DELETE_USER_ROLE.format(**params_path), + data=json.dumps(payload)) + return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204) \ No newline at end of file diff --git a/keycloak/urls_patterns.py b/keycloak/urls_patterns.py index 8dfbadb..460ae8b 100644 --- a/keycloak/urls_patterns.py +++ b/keycloak/urls_patterns.py @@ -105,4 +105,6 @@ URL_ADMIN_KEYS = "admin/realms/{realm-name}/keys" URL_ADMIN_USER_FEDERATED_IDENTITIES = "admin/realms/{realm-name}/users/{id}/federated-identity" URL_ADMIN_USER_FEDERATED_IDENTITY = "admin/realms/{realm-name}/users/{id}/federated-identity/{provider}" -URL_ADMIN_EVENTS = 'admin/realms/{realm-name}/events' \ No newline at end of file +URL_ADMIN_EVENTS = 'admin/realms/{realm-name}/events' + +URL_ADMIN_DELETE_USER_ROLE = "admin/realms/{realm-name}/users/{id}/role-mappings/realm" \ No newline at end of file From d7d661a38c7a6bf5daaea513616f4f9936460bef Mon Sep 17 00:00:00 2001 From: bostonkenne Date: Thu, 17 Jun 2021 15:53:16 +0100 Subject: [PATCH 2/2] fix json response --- keycloak/keycloak_admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 4272482..15ee759 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -1890,4 +1890,4 @@ class KeycloakAdmin: params_path = {"realm-name": self.realm_name, "id": str(user_id) } data_raw = self.connection.raw_delete(URL_ADMIN_DELETE_USER_ROLE.format(**params_path), data=json.dumps(payload)) - return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204) \ No newline at end of file + return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204]) \ No newline at end of file