Skip to content

Commit

Permalink
Merge pull request Peter-Slump#24 from MrFoxes/master
Browse files Browse the repository at this point in the history
Assing realm roles
  • Loading branch information
marcospereirampj authored Aug 30, 2019
2 parents 99f69dd + d2b15a7 commit 8461b31
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
17 changes: 17 additions & 0 deletions keycloak/keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,23 @@ def assign_client_role(self, user_id, client_id, roles):
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.
Expand Down
1 change: 1 addition & 0 deletions keycloak/urls_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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}"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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',
Expand Down

0 comments on commit 8461b31

Please sign in to comment.