From 6b30631378ad4d8d7b64d219d313be4e10429ba2 Mon Sep 17 00:00:00 2001 From: Igli Manaj Date: Fri, 14 Oct 2022 02:34:31 +0200 Subject: [PATCH 1/4] feat: helping functions for disabling users --- src/keycloak/keycloak_admin.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index 994b97d..1b8426c 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -762,6 +762,28 @@ class KeycloakAdmin: ) return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[204]) + def disable_user(self, user_id): + """Disable the user from the realm. Disabled users can not log in. + + :param user_id: User id + :type user_id: str + + :return: Http response + :rtype: bytes + """ + return self.update_user(user_id=user_id, payload={"enabled": False}) + + + def disable_all_users(self): + """Disable all existing users. + """ + users = self.get_users() + for user in users: + user_id = user["id"] + print(f"Disabling user with id: {user_id}") + self.disable_user(user_id=user_id) + + def delete_user(self, user_id): """Delete the user. From 477e0c5a3c712b74c29953f82b8c925e8ea567df Mon Sep 17 00:00:00 2001 From: Igli Manaj Date: Fri, 14 Oct 2022 02:44:02 +0200 Subject: [PATCH 2/4] feat: option for enabling users --- src/keycloak/keycloak_admin.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index 1b8426c..55bb6d6 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -772,7 +772,17 @@ class KeycloakAdmin: :rtype: bytes """ return self.update_user(user_id=user_id, payload={"enabled": False}) + + def enable_user(self, user_id): + """Enable the user from the realm. + + :param user_id: User id + :type user_id: str + :return: Http response + :rtype: bytes + """ + return self.update_user(user_id=user_id, payload={"enabled": True}) def disable_all_users(self): """Disable all existing users. @@ -783,6 +793,14 @@ class KeycloakAdmin: print(f"Disabling user with id: {user_id}") self.disable_user(user_id=user_id) + def enable_all_users(self): + """Disable all existing users. + """ + users = self.get_users() + for user in users: + user_id = user["id"] + print(f"Enabling user with id: {user_id}") + self.enable_user(user_id=user_id) def delete_user(self, user_id): """Delete the user. From e9b173024bdcfc30c824d0e03f0636c3fd877ebe Mon Sep 17 00:00:00 2001 From: Igli Manaj Date: Tue, 20 Dec 2022 02:17:21 +0100 Subject: [PATCH 3/4] refactor: remove print statements --- src/keycloak/keycloak_admin.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index 55bb6d6..0e0cca0 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -790,7 +790,6 @@ class KeycloakAdmin: users = self.get_users() for user in users: user_id = user["id"] - print(f"Disabling user with id: {user_id}") self.disable_user(user_id=user_id) def enable_all_users(self): @@ -799,7 +798,6 @@ class KeycloakAdmin: users = self.get_users() for user in users: user_id = user["id"] - print(f"Enabling user with id: {user_id}") self.enable_user(user_id=user_id) def delete_user(self, user_id): From 4fe06af67745d0322ba9214fd9fc91b4bcde3f23 Mon Sep 17 00:00:00 2001 From: iglimanaj Date: Sat, 24 Dec 2022 00:47:53 +0100 Subject: [PATCH 4/4] refactor: code formatting after tox checks --- src/keycloak/keycloak_admin.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index 0e0cca0..ac1f46e 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -772,7 +772,7 @@ class KeycloakAdmin: :rtype: bytes """ return self.update_user(user_id=user_id, payload={"enabled": False}) - + def enable_user(self, user_id): """Enable the user from the realm. @@ -783,18 +783,16 @@ class KeycloakAdmin: :rtype: bytes """ return self.update_user(user_id=user_id, payload={"enabled": True}) - + def disable_all_users(self): - """Disable all existing users. - """ + """Disable all existing users.""" users = self.get_users() for user in users: user_id = user["id"] self.disable_user(user_id=user_id) - + def enable_all_users(self): - """Disable all existing users. - """ + """Disable all existing users.""" users = self.get_users() for user in users: user_id = user["id"]