feat: update filds

This commit is contained in:
dettlaff 2024-11-15 03:30:24 +04:00
parent d171961ef9
commit 1c3e2cc2e0
3 changed files with 97 additions and 16 deletions

View file

@ -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
]

View file

@ -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]]

View file

@ -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", []),
)