selfprivacy-rest-api/selfprivacy_api/graphql/common_types/user.py

67 lines
1.7 KiB
Python
Raw Normal View History

2022-07-21 00:23:59 +03:00
import typing
import strawberry
from selfprivacy_api.graphql.mutations.mutation_interface import (
MutationReturnInterface,
)
2022-07-22 13:33:32 +03:00
from enum import Enum
2022-07-21 00:23:59 +03:00
2022-07-24 17:26:13 +03:00
from selfprivacy_api.utils import ReadUserData
2022-07-22 13:36:11 +03:00
2022-07-22 13:33:32 +03:00
@strawberry.enum
class UserType(Enum):
NORMAL = "NORMAL"
PRIMARY = "PRIMARY"
ROOT = "ROOT"
2022-07-21 00:23:59 +03:00
2022-07-22 13:36:11 +03:00
2022-07-21 00:23:59 +03:00
@strawberry.type
class User:
"""Users management"""
2022-07-22 13:33:32 +03:00
user_type: UserType
2022-07-21 00:23:59 +03:00
username: str
# userHomeFolderspace: UserHomeFolderUsage
2022-07-24 18:38:39 +03:00
ssh_keys: typing.List[str] = strawberry.field(default_factory=list)
2022-07-21 00:23:59 +03:00
@strawberry.type
class UserMutationReturn(MutationReturnInterface):
"""Return type for user mutation"""
user: typing.Optional[User]
2022-07-24 17:26:13 +03:00
2022-07-24 18:38:39 +03:00
def get_user_by_username(username: str) -> typing.Optional[User]:
2022-07-24 17:26:13 +03:00
with ReadUserData() as data:
if username == "root":
if data["ssh"]["rootKeys"] not in data:
data["ssh"]["rootKeys"] = []
return User(
user_type=UserType.ROOT,
username="root",
ssh_keys=data["ssh"]["rootKeys"],
)
elif username == data["username"]:
if "sshKeys" not in data:
data["sshKeys"] = []
return User(
user_type=UserType.PRIMARY,
username=username,
ssh_keys=data["sshKeys"],
)
else:
for user in data["users"]:
if user["username"] == username:
if "sshKeys" not in user:
user["sshKeys"] = []
return User(
user_type=UserType.NORMAL,
username=username,
ssh_keys=user["sshKeys"],
)
2022-07-24 18:38:39 +03:00
return None