Skip to content

Commit d2b15a7

Browse files
Implement assign_realm_roles method
Increment package version Update docstring Remove client_id from path params Fix raw_post with correct url
1 parent 99f69dd commit d2b15a7

4 files changed

Lines changed: 22 additions & 1 deletion

File tree

docs/source/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ Main methods::
235235
# Assign client role to user. Note that BOTH role_name and role_id appear to be required.
236236
keycloak_admin.assign_client_role(client_id="client_id", user_id="user_id", role_id="role_id", role_name="test")
237237

238+
# Assign realm roles to user. Note that BOTH role_name and role_id appear to be required.
239+
keycloak_admin.assign_realm_roles(client_id="client_id", user_id="user_id", roles=[{"roles_representation"}])
240+
238241
# Create new group
239242
group = keycloak_admin.create_group(name="Example Group")
240243

keycloak/keycloak_admin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,23 @@ def assign_client_role(self, user_id, client_id, roles):
823823
data=json.dumps(payload))
824824
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
825825

826+
def assign_realm_roles(self, user_id, client_id, roles):
827+
"""
828+
Assign realm roles to a user
829+
830+
:param client_id: id of client (not client-id)
831+
:param user_id: id of user
832+
:param client_id: id of client containing role,
833+
:param roles: roles list or role (use RoleRepresentation)
834+
:return Keycloak server response
835+
"""
836+
837+
payload = roles if isinstance(roles, list) else [roles]
838+
params_path = {"realm-name": self.realm_name, "id": user_id}
839+
data_raw = self.connection.raw_post(URL_ADMIN_USER_REALM_ROLES.format(**params_path),
840+
data=json.dumps(payload))
841+
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
842+
826843
def get_client_roles_of_user(self, user_id, client_id):
827844
"""
828845
Get all client roles for a user.

keycloak/urls_patterns.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
URL_ADMIN_RESET_PASSWORD = "admin/realms/{realm-name}/users/{id}/reset-password"
4242
URL_ADMIN_GET_SESSIONS = "admin/realms/{realm-name}/users/{id}/sessions"
4343
URL_ADMIN_USER_CLIENT_ROLES = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}"
44+
URL_ADMIN_USER_REALM_ROLES = "admin/realms/{realm-name}/users/{id}/role-mappings/realm"
4445
URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}/available"
4546
URL_ADMIN_USER_CLIENT_ROLES_COMPOSITE = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}/composite"
4647
URL_ADMIN_USER_GROUP = "admin/realms/{realm-name}/users/{id}/groups/{group-id}"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='python-keycloak',
10-
version='0.17.4',
10+
version='0.17.5',
1111
url='https://github.com/marcospereirampj/python-keycloak',
1212
license='The MIT License',
1313
author='Marcos Pereira',

0 commit comments

Comments
 (0)