mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-06-14 03:55:37 +00:00
new module: keycloak_realm_users_info (#12105)
* init keycloak user info * fix docs * rename * nox -Re formatters * botmeta * update runtime.yml * fix line too long * fix stupid * mv keycloak_realm_users.py keycloak_realm_users_info.py * add integration test * fix integrationtest * fix version_added Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * fix eof * use other file as template * use keycloak_client_rolescope as basis * refactor functions * fix extends_documentation_fragment * clean some things up * improve diff? * docstring * revert to old logic --------- Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
27ed9cf919
commit
7b8ed586da
9 changed files with 289 additions and 12 deletions
|
|
@ -1085,24 +1085,21 @@ class KeycloakAPI:
|
|||
except Exception as e:
|
||||
self.fail_request(e, msg=f"Could not fetch effective rolemappings for user {uid}, realm {realm}: {e}")
|
||||
|
||||
def get_user_by_username(self, username, realm: str = "master"):
|
||||
def get_user_by_username(self, username: str, realm: str = "master") -> dict[str, t.Any] | None:
|
||||
"""Fetch a keycloak user within a realm based on its username.
|
||||
|
||||
If the user does not exist, None is returned.
|
||||
If the username is not found, None is returned.
|
||||
:param username: Username of the user to fetch.
|
||||
:param realm: Realm in which the user resides; default 'master'
|
||||
"""
|
||||
users_url = URL_USERS.format(url=self.baseurl, realm=realm)
|
||||
users_url += f"?username={quote(username, safe='')}&exact=true"
|
||||
try:
|
||||
userrep = None
|
||||
users = self._request_and_deserialize(users_url, method="GET")
|
||||
for user in users:
|
||||
if user["username"] == username:
|
||||
userrep = user
|
||||
break
|
||||
return userrep
|
||||
|
||||
return user
|
||||
return None
|
||||
except ValueError as e:
|
||||
self.module.fail_json(
|
||||
msg=f"API returned incorrect JSON when trying to obtain the user for realm {realm} and username {username}: {e}"
|
||||
|
|
@ -1110,6 +1107,22 @@ class KeycloakAPI:
|
|||
except Exception as e:
|
||||
self.fail_request(e, msg=f"Could not obtain the user for realm {realm} and username {username}: {e}")
|
||||
|
||||
def get_realm_users(self, realm: str = "master") -> list[dict[str, t.Any]]:
|
||||
"""Obtain list of users from the realm
|
||||
|
||||
:param realm: realm id
|
||||
:return: list of user representations
|
||||
"""
|
||||
users_url = URL_USERS.format(url=self.baseurl, realm=realm)
|
||||
try:
|
||||
return self._request_and_deserialize(users_url, method="GET")
|
||||
except ValueError as e:
|
||||
self.module.fail_json(
|
||||
msg=f"API returned incorrect JSON when trying to obtain the users for realm {realm}: {e}"
|
||||
)
|
||||
except Exception as e:
|
||||
self.fail_request(e, msg=f"Could not obtain the users for realm {realm}: {e}")
|
||||
|
||||
def get_service_account_user_by_client_id(self, client_id, realm: str = "master"):
|
||||
"""Fetch a keycloak service account user within a realm based on its client_id.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue