From 1c3e2cc2e055f47392c754c32ac66cfb6b903f05 Mon Sep 17 00:00:00 2001 From: dettlaff Date: Fri, 15 Nov 2024 03:30:24 +0400 Subject: [PATCH] feat: update filds --- selfprivacy_api/graphql/common_types/user.py | 21 ++++- selfprivacy_api/models/user.py | 14 ++-- .../users/kanidm_user_repository.py | 78 ++++++++++++++++--- 3 files changed, 97 insertions(+), 16 deletions(-) diff --git a/selfprivacy_api/graphql/common_types/user.py b/selfprivacy_api/graphql/common_types/user.py index 4e6bb63..2d3d9a4 100644 --- a/selfprivacy_api/graphql/common_types/user.py +++ b/selfprivacy_api/graphql/common_types/user.py @@ -22,10 +22,17 @@ class UserType(Enum): @strawberry.type class User: - user_type: UserType + displayname: str username: str - # userHomeFolderspace: UserHomeFolderUsage + user_type: UserType ssh_keys: typing.List[str] = strawberry.field(default_factory=list) + uuid: typing.Optional[str] = None + email: typing.Optional[str] = None + directmemberof: typing.Optional[typing.List[str]] = strawberry.field( + default_factory=list + ) + memberof: typing.Optional[typing.List[str]] = strawberry.field(default_factory=list) + # userHomeFolderspace: UserHomeFolderUsage @strawberry.type @@ -44,6 +51,11 @@ def get_user_by_username(username: str) -> typing.Optional[User]: user_type=UserType(user.origin.value), username=user.username, ssh_keys=user.ssh_keys, + uuid=user.uuid, + displayname=(user.displayname if user.displayname else user.username), + email=user.email, + directmemberof=user.directmemberof, + memberof=user.memberof, ) @@ -55,6 +67,11 @@ def get_users() -> typing.List[User]: user_type=UserType(user.origin.value), username=user.username, ssh_keys=user.ssh_keys, + uuid=user.uuid, + displayname=(user.displayname if user.displayname else user.username), + email=user.email, + directmemberof=user.directmemberof, + memberof=user.memberof, ) for user in users ] diff --git a/selfprivacy_api/models/user.py b/selfprivacy_api/models/user.py index d35d172..d288496 100644 --- a/selfprivacy_api/models/user.py +++ b/selfprivacy_api/models/user.py @@ -14,10 +14,14 @@ class UserDataUserOrigin(Enum): class UserDataUser(BaseModel): """The user model from the userdata file""" - uuid: Optional[str] - displayname: Optional[str] - email: Optional[str] - username: str - ssh_keys: list[str] # TODO WHY NOT OPTIONAL? + displayname: Optional[ + str + ] # in logic graphql will return "username" if "displayname" None origin: UserDataUserOrigin + + uuid: Optional[str] + email: Optional[str] + ssh_keys: Optional[list[str]] + directmemberof: Optional[list[str]] + memberof: Optional[list[str]] diff --git a/selfprivacy_api/repositories/users/kanidm_user_repository.py b/selfprivacy_api/repositories/users/kanidm_user_repository.py index 3e5f80c..eae15d1 100644 --- a/selfprivacy_api/repositories/users/kanidm_user_repository.py +++ b/selfprivacy_api/repositories/users/kanidm_user_repository.py @@ -44,16 +44,28 @@ class KanidmUserRepository(AbstractUserRepository): raise KanidmQueryError(f"Kanidm request failed! Error: {str(error)}") @staticmethod - def create_user(username: str, password: str): + def create_user( + username: str, + password: Optional[str] = None, # TODO legacy? + displayname: Optional[str] = None, + email: Optional[str] = None, + directmemberof: Optional[list[str]] = None, + memberof: Optional[list[str]] = None, + ) -> None: data = { "attrs": { "name": [username], - "displayname": [username], - "mail": [f"{username}@{get_domain()}"], - "class": ["user"], + "displayname": [displayname if displayname else username], + "mail": [email if email else f"{username}@{get_domain()}"], + "class": ["user"], # TODO read more about it } } + if directmemberof: + data["attrs"]["directmemberof"] = directmemberof + if memberof: + data["attrs"]["memberof"] = memberof + return KanidmUserRepository._send_query( endpoint="person", method="POST", @@ -71,22 +83,70 @@ class KanidmUserRepository(AbstractUserRepository): user_type = UserDataUser( uuid=attrs.get("uuid", [None])[0], username=attrs.get("name", [None])[0], - ssh_keys=["test"], # TODO + ssh_keys=["test"], # TODO: подключить реальные SSH-ключи displayname=attrs.get("displayname", [None])[0], email=attrs.get("mail", [None])[0], origin=UserDataUserOrigin.NORMAL, # TODO + directmemberof=attrs.get("directmemberof", []), + memberof=attrs.get("memberof", []), ) users.append(user_type) return users def delete_user(username: str) -> None: """Deletes an existing user""" - return KanidmUserRepository._send_query() + return KanidmUserRepository._send_query( + endpoint=f"person/{username}", method="DELETE" + ) - def update_user(username: str, password: str) -> None: + def update_user( + username: str, + password: Optional[str] = None, # TODO legacy? + displayname: Optional[str] = None, + email: Optional[str] = None, + directmemberof: Optional[list[str]] = None, + memberof: Optional[list[str]] = None, + ) -> None: """Updates the password of an existing user""" - return KanidmUserRepository._send_query() + + data = { + "attrs": { + "displayname": [displayname if displayname else username], + "mail": [email if email else f"{username}@{get_domain()}"], + "class": ["user"], # TODO read more about it + } + } + + if directmemberof: + data["attrs"]["directmemberof"] = directmemberof + if memberof: + data["attrs"]["memberof"] = memberof + + return KanidmUserRepository._send_query( + endpoint=f"person/{username}", + method="PATCH", + data=data, + ) def get_user_by_username(username: str) -> Optional[UserDataUser]: """Retrieves user data (UserDataUser) by username""" - return KanidmUserRepository._send_query() + user_data = KanidmUserRepository._send_query( + endpoint=f"person/{username}", + method="GET", + ) + + if not user_data or "attrs" not in user_data: + return None + + attrs = user_data["attrs"] + + return UserDataUser( + uuid=attrs.get("uuid", [None])[0], + username=attrs.get("name", [None])[0], + displayname=attrs.get("displayname", [None])[0], + email=attrs.get("mail", [None])[0], + ssh_keys=attrs.get("ssh_keys", []), + origin=UserDataUserOrigin.NORMAL, # TODO + directmemberof=attrs.get("directmemberof", []), + memberof=attrs.get("memberof", []), + )