mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-09 01:21:05 +00:00
feat: update filds
This commit is contained in:
parent
d171961ef9
commit
1c3e2cc2e0
|
@ -22,10 +22,17 @@ class UserType(Enum):
|
||||||
|
|
||||||
@strawberry.type
|
@strawberry.type
|
||||||
class User:
|
class User:
|
||||||
user_type: UserType
|
displayname: str
|
||||||
username: str
|
username: str
|
||||||
# userHomeFolderspace: UserHomeFolderUsage
|
user_type: UserType
|
||||||
ssh_keys: typing.List[str] = strawberry.field(default_factory=list)
|
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
|
@strawberry.type
|
||||||
|
@ -44,6 +51,11 @@ def get_user_by_username(username: str) -> typing.Optional[User]:
|
||||||
user_type=UserType(user.origin.value),
|
user_type=UserType(user.origin.value),
|
||||||
username=user.username,
|
username=user.username,
|
||||||
ssh_keys=user.ssh_keys,
|
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),
|
user_type=UserType(user.origin.value),
|
||||||
username=user.username,
|
username=user.username,
|
||||||
ssh_keys=user.ssh_keys,
|
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
|
for user in users
|
||||||
]
|
]
|
||||||
|
|
|
@ -14,10 +14,14 @@ class UserDataUserOrigin(Enum):
|
||||||
class UserDataUser(BaseModel):
|
class UserDataUser(BaseModel):
|
||||||
"""The user model from the userdata file"""
|
"""The user model from the userdata file"""
|
||||||
|
|
||||||
uuid: Optional[str]
|
|
||||||
displayname: Optional[str]
|
|
||||||
email: Optional[str]
|
|
||||||
|
|
||||||
username: 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
|
origin: UserDataUserOrigin
|
||||||
|
|
||||||
|
uuid: Optional[str]
|
||||||
|
email: Optional[str]
|
||||||
|
ssh_keys: Optional[list[str]]
|
||||||
|
directmemberof: Optional[list[str]]
|
||||||
|
memberof: Optional[list[str]]
|
||||||
|
|
|
@ -44,16 +44,28 @@ class KanidmUserRepository(AbstractUserRepository):
|
||||||
raise KanidmQueryError(f"Kanidm request failed! Error: {str(error)}")
|
raise KanidmQueryError(f"Kanidm request failed! Error: {str(error)}")
|
||||||
|
|
||||||
@staticmethod
|
@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 = {
|
data = {
|
||||||
"attrs": {
|
"attrs": {
|
||||||
"name": [username],
|
"name": [username],
|
||||||
"displayname": [username],
|
"displayname": [displayname if displayname else username],
|
||||||
"mail": [f"{username}@{get_domain()}"],
|
"mail": [email if email else f"{username}@{get_domain()}"],
|
||||||
"class": ["user"],
|
"class": ["user"], # TODO read more about it
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if directmemberof:
|
||||||
|
data["attrs"]["directmemberof"] = directmemberof
|
||||||
|
if memberof:
|
||||||
|
data["attrs"]["memberof"] = memberof
|
||||||
|
|
||||||
return KanidmUserRepository._send_query(
|
return KanidmUserRepository._send_query(
|
||||||
endpoint="person",
|
endpoint="person",
|
||||||
method="POST",
|
method="POST",
|
||||||
|
@ -71,22 +83,70 @@ class KanidmUserRepository(AbstractUserRepository):
|
||||||
user_type = UserDataUser(
|
user_type = UserDataUser(
|
||||||
uuid=attrs.get("uuid", [None])[0],
|
uuid=attrs.get("uuid", [None])[0],
|
||||||
username=attrs.get("name", [None])[0],
|
username=attrs.get("name", [None])[0],
|
||||||
ssh_keys=["test"], # TODO
|
ssh_keys=["test"], # TODO: подключить реальные SSH-ключи
|
||||||
displayname=attrs.get("displayname", [None])[0],
|
displayname=attrs.get("displayname", [None])[0],
|
||||||
email=attrs.get("mail", [None])[0],
|
email=attrs.get("mail", [None])[0],
|
||||||
origin=UserDataUserOrigin.NORMAL, # TODO
|
origin=UserDataUserOrigin.NORMAL, # TODO
|
||||||
|
directmemberof=attrs.get("directmemberof", []),
|
||||||
|
memberof=attrs.get("memberof", []),
|
||||||
)
|
)
|
||||||
users.append(user_type)
|
users.append(user_type)
|
||||||
return users
|
return users
|
||||||
|
|
||||||
def delete_user(username: str) -> None:
|
def delete_user(username: str) -> None:
|
||||||
"""Deletes an existing user"""
|
"""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"""
|
"""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]:
|
def get_user_by_username(username: str) -> Optional[UserDataUser]:
|
||||||
"""Retrieves user data (UserDataUser) by username"""
|
"""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", []),
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue