From d2b15a734eeb6d1b20e1a547fd65fc6681d40c26 Mon Sep 17 00:00:00 2001 From: Andrea Volpi Date: Thu, 29 Aug 2019 16:07:34 +0200 Subject: [PATCH] Implement assign_realm_roles method Increment package version Update docstring Remove client_id from path params Fix raw_post with correct url --- docs/source/index.rst | 3 +++ keycloak/keycloak_admin.py | 17 +++++++++++++++++ keycloak/urls_patterns.py | 1 + setup.py | 2 +- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/source/index.rst b/docs/source/index.rst index c4e464c..f524920 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -235,6 +235,9 @@ Main methods:: # Assign client role to user. Note that BOTH role_name and role_id appear to be required. keycloak_admin.assign_client_role(client_id="client_id", user_id="user_id", role_id="role_id", role_name="test") + # Assign realm roles to user. Note that BOTH role_name and role_id appear to be required. + keycloak_admin.assign_realm_roles(client_id="client_id", user_id="user_id", roles=[{"roles_representation"}]) + # Create new group group = keycloak_admin.create_group(name="Example Group") diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index b8c7de8..56f3e9b 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -823,6 +823,23 @@ class KeycloakAdmin: data=json.dumps(payload)) return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204) + def assign_realm_roles(self, user_id, client_id, roles): + """ + Assign realm roles to a user + + :param client_id: id of client (not client-id) + :param user_id: id of user + :param client_id: id of client containing role, + :param roles: roles list or role (use RoleRepresentation) + :return Keycloak server response + """ + + payload = roles if isinstance(roles, list) else [roles] + params_path = {"realm-name": self.realm_name, "id": user_id} + data_raw = self.connection.raw_post(URL_ADMIN_USER_REALM_ROLES.format(**params_path), + data=json.dumps(payload)) + return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204) + def get_client_roles_of_user(self, user_id, client_id): """ Get all client roles for a user. diff --git a/keycloak/urls_patterns.py b/keycloak/urls_patterns.py index 6a06757..fad3455 100644 --- a/keycloak/urls_patterns.py +++ b/keycloak/urls_patterns.py @@ -41,6 +41,7 @@ URL_ADMIN_SEND_VERIFY_EMAIL = "admin/realms/{realm-name}/users/{id}/send-verify- URL_ADMIN_RESET_PASSWORD = "admin/realms/{realm-name}/users/{id}/reset-password" URL_ADMIN_GET_SESSIONS = "admin/realms/{realm-name}/users/{id}/sessions" URL_ADMIN_USER_CLIENT_ROLES = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}" +URL_ADMIN_USER_REALM_ROLES = "admin/realms/{realm-name}/users/{id}/role-mappings/realm" URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}/available" URL_ADMIN_USER_CLIENT_ROLES_COMPOSITE = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}/composite" URL_ADMIN_USER_GROUP = "admin/realms/{realm-name}/users/{id}/groups/{group-id}" diff --git a/setup.py b/setup.py index 7a175f2..68f1aae 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open("README.md", "r") as fh: setup( name='python-keycloak', - version='0.17.4', + version='0.17.5', url='https://github.com/marcospereirampj/python-keycloak', license='The MIT License', author='Marcos Pereira',