2022-08-01 10:40:40 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Users management module"""
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
import strawberry
|
2024-10-26 18:22:31 +00:00
|
|
|
|
2022-08-01 10:40:40 +00:00
|
|
|
from selfprivacy_api.graphql import IsAuthenticated
|
|
|
|
from selfprivacy_api.graphql.common_types.user import (
|
|
|
|
UserMutationReturn,
|
|
|
|
get_user_by_username,
|
|
|
|
)
|
2023-06-21 03:46:56 +00:00
|
|
|
from selfprivacy_api.actions.ssh import (
|
|
|
|
InvalidPublicKey,
|
|
|
|
KeyAlreadyExists,
|
|
|
|
KeyNotFound,
|
|
|
|
create_ssh_key,
|
|
|
|
remove_ssh_key,
|
|
|
|
)
|
2022-08-01 10:40:40 +00:00
|
|
|
from selfprivacy_api.graphql.mutations.mutation_interface import (
|
|
|
|
GenericMutationReturn,
|
|
|
|
)
|
2024-10-26 18:22:31 +00:00
|
|
|
from selfprivacy_api.repositories.users import ACTIVE_USERS_PROVIDER as users_actions
|
|
|
|
from selfprivacy_api.repositories.users.exceptions import (
|
|
|
|
PasswordIsEmpty,
|
|
|
|
UsernameForbidden,
|
|
|
|
InvalidConfiguration,
|
|
|
|
UserAlreadyExists,
|
|
|
|
UserIsProtected,
|
|
|
|
UsernameNotAlphanumeric,
|
|
|
|
UsernameTooLong,
|
|
|
|
UserNotFound,
|
|
|
|
)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@strawberry.input
|
|
|
|
class UserMutationInput:
|
|
|
|
"""Input type for user mutation"""
|
|
|
|
|
|
|
|
username: str
|
|
|
|
password: str
|
|
|
|
|
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
@strawberry.input
|
|
|
|
class SshMutationInput:
|
|
|
|
"""Input type for ssh mutation"""
|
|
|
|
|
|
|
|
username: str
|
|
|
|
ssh_key: str
|
|
|
|
|
|
|
|
|
2022-08-01 10:40:40 +00:00
|
|
|
@strawberry.type
|
2023-06-21 03:46:56 +00:00
|
|
|
class UsersMutations:
|
2022-08-01 10:40:40 +00:00
|
|
|
"""Mutations change user settings"""
|
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def create_user(self, user: UserMutationInput) -> UserMutationReturn:
|
2022-08-25 17:03:56 +00:00
|
|
|
try:
|
|
|
|
users_actions.create_user(user.username, user.password)
|
2024-10-26 18:22:31 +00:00
|
|
|
except PasswordIsEmpty as e:
|
2022-08-25 17:03:56 +00:00
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=400,
|
|
|
|
)
|
2024-10-26 18:22:31 +00:00
|
|
|
except UsernameForbidden as e:
|
2022-08-25 17:03:56 +00:00
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=409,
|
|
|
|
)
|
2024-10-26 18:22:31 +00:00
|
|
|
except UsernameNotAlphanumeric as e:
|
2022-08-25 17:03:56 +00:00
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=400,
|
|
|
|
)
|
2024-10-26 18:22:31 +00:00
|
|
|
except UsernameTooLong as e:
|
2022-08-25 17:03:56 +00:00
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=400,
|
|
|
|
)
|
2024-10-26 18:22:31 +00:00
|
|
|
except InvalidConfiguration as e:
|
2023-12-27 13:44:39 +00:00
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=400,
|
|
|
|
)
|
2024-10-26 18:22:31 +00:00
|
|
|
except UserAlreadyExists as e:
|
2022-08-25 17:03:56 +00:00
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=409,
|
|
|
|
user=get_user_by_username(user.username),
|
|
|
|
)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
return UserMutationReturn(
|
2022-08-25 17:03:56 +00:00
|
|
|
success=True,
|
|
|
|
message="User created",
|
|
|
|
code=201,
|
2022-08-01 10:40:40 +00:00
|
|
|
user=get_user_by_username(user.username),
|
|
|
|
)
|
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def delete_user(self, username: str) -> GenericMutationReturn:
|
2022-08-25 17:03:56 +00:00
|
|
|
try:
|
|
|
|
users_actions.delete_user(username)
|
2024-10-26 18:22:31 +00:00
|
|
|
except UserNotFound as e:
|
2022-08-25 17:03:56 +00:00
|
|
|
return GenericMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=404,
|
|
|
|
)
|
2024-10-26 18:22:31 +00:00
|
|
|
except UserIsProtected as e:
|
2022-08-25 17:03:56 +00:00
|
|
|
return GenericMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=400,
|
|
|
|
)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
return GenericMutationReturn(
|
2022-08-25 17:03:56 +00:00
|
|
|
success=True,
|
|
|
|
message="User deleted",
|
|
|
|
code=200,
|
2022-08-01 10:40:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def update_user(self, user: UserMutationInput) -> UserMutationReturn:
|
|
|
|
"""Update user mutation"""
|
2022-08-25 17:03:56 +00:00
|
|
|
try:
|
|
|
|
users_actions.update_user(user.username, user.password)
|
2024-10-26 18:22:31 +00:00
|
|
|
except PasswordIsEmpty as e:
|
2022-08-25 17:03:56 +00:00
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=400,
|
|
|
|
)
|
2024-10-26 18:22:31 +00:00
|
|
|
except UserNotFound as e:
|
2022-08-25 17:03:56 +00:00
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=404,
|
|
|
|
)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
return UserMutationReturn(
|
2022-08-25 17:03:56 +00:00
|
|
|
success=True,
|
|
|
|
message="User updated",
|
|
|
|
code=200,
|
2022-08-01 10:40:40 +00:00
|
|
|
user=get_user_by_username(user.username),
|
|
|
|
)
|
2023-06-21 03:46:56 +00:00
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def add_ssh_key(self, ssh_input: SshMutationInput) -> UserMutationReturn:
|
|
|
|
"""Add a new ssh key"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
create_ssh_key(ssh_input.username, ssh_input.ssh_key)
|
|
|
|
except KeyAlreadyExists:
|
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="Key already exists",
|
|
|
|
code=409,
|
|
|
|
)
|
|
|
|
except InvalidPublicKey:
|
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
2023-10-03 13:51:06 +00:00
|
|
|
message="Invalid key type. Only ssh-ed25519, ssh-rsa and ecdsa are supported",
|
2023-06-21 03:46:56 +00:00
|
|
|
code=400,
|
|
|
|
)
|
|
|
|
except UserNotFound:
|
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="User not found",
|
|
|
|
code=404,
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=500,
|
|
|
|
)
|
|
|
|
|
|
|
|
return UserMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="New SSH key successfully written",
|
|
|
|
code=201,
|
|
|
|
user=get_user_by_username(ssh_input.username),
|
|
|
|
)
|
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def remove_ssh_key(self, ssh_input: SshMutationInput) -> UserMutationReturn:
|
|
|
|
"""Remove ssh key from user"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
remove_ssh_key(ssh_input.username, ssh_input.ssh_key)
|
|
|
|
except KeyNotFound:
|
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="Key not found",
|
|
|
|
code=404,
|
|
|
|
)
|
|
|
|
except UserNotFound:
|
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="User not found",
|
|
|
|
code=404,
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
return UserMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=500,
|
|
|
|
)
|
|
|
|
|
|
|
|
return UserMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="SSH key successfully removed",
|
|
|
|
code=200,
|
|
|
|
user=get_user_by_username(ssh_input.username),
|
|
|
|
)
|