如何使用python删除用户的keycloak领域角色

问题描述 投票:0回答:1

我正在尝试使用python在密钥斗篷中为用户添加和删除领域角色。

为了向用户添加领域角色,我正在使用以下功能

keycloak_admin.assign_realm_roles(user_id=user_keycloak_id,
                                          client_id=keycloak_admin.client_id,
                                          roles={'id': role_id, "name": role_name})

但是要删除用户角色,我没有任何功能。

即使它的API为用户删除角色,我也想知道如何在python中使用它。

谢谢。

python realm keycloak
1个回答
0
投票

根据我发现的内容,您似乎正在使用this git repository。而且,here是您现在正在调用的方法。但是没有删除/删除。

Keycloak确实将此作为API提供,所以最好的选择是拥有自己的删除功能。像这样的东西(警告-未经测试!!):

def remove_realm_roles(self, user_id, client_id, roles):
    """
    Removes realm roles to a user
    :param user_id: id of user
    :param client_id: id of client containing role (not client-id)
    :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.raw_delete(URL_ADMIN_USER_REALM_ROLES.format(**params_path),
                             data=json.dumps(payload))
    return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)

git库中代码的唯一更改是,它使用的是raw_delete而不是raw_post。 Keycloak Admin REST API文档显示它采用了完全相同的参数,因此只需更改HTTP方法即可。

© www.soinside.com 2019 - 2024. All rights reserved.