python-keycloak icon indicating copy to clipboard operation
python-keycloak copied to clipboard

Remove a role of an user

Open huyenntn opened this issue 3 years ago • 5 comments

How to remove a role from an user I only found the function: delete_client_roles_of_user(self, user_id, client_id, roles). It delete client roles from a user. But in my case, "client_id" is none. So if I use the function, it will be occurred error: keycloak.exceptions.KeycloakGetError: 404: b'{"error":"Client not found"}' Before, I assigned a role to an user by the function keycloak_admin.assign_realm_roles(user_id, None, role). It succeeds

huyenntn avatar Aug 11 '21 02:08 huyenntn

Roles can be from the realm or a specific client, thus the two different functions. If you assigned a realm role you need to remove it using delete_realm_roles()

double-a avatar Aug 22 '21 18:08 double-a

No, is not it. I mean just remove(detach) a role of an user. delete_realm_role() will be delete a role for the realm

huyenntn avatar Aug 22 '21 23:08 huyenntn

As far as I understand the documentation the functions assign_realm_roles() and delete_realm_roles() are meant to attach/detach a realm role from a user. You might confuse that with create_realm_role() and delete_realm_role().

double-a avatar Aug 23 '21 10:08 double-a

@double-a you have mentioned that we can use delete_realm_roles(), I do not see this function in keycloak_admin.py file. Is this function available somewhere else within the package or...?

Muntasir2001 avatar Sep 03 '21 15:09 Muntasir2001

Hi @huyenntn

@double-a is almost correct, in order to remove a realm role from a user you have to use the method called delete_realm_roles_of_user (https://github.com/marcospereirampj/python-keycloak/blob/master/src/keycloak/keycloak_admin.py#L1671)

ryshoooo avatar Jun 22 '22 20:06 ryshoooo