2022-08-25 17:03:56 +00:00
|
|
|
"""Actions to manage the users."""
|
2024-07-26 19:59:44 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
import re
|
|
|
|
from typing import Optional
|
|
|
|
|
2024-11-02 23:15:51 +00:00
|
|
|
from selfprivacy_api.models.user import UserDataUser
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2024-11-15 14:30:51 +00:00
|
|
|
from selfprivacy_api.utils import is_username_forbidden
|
2024-11-02 23:15:51 +00:00
|
|
|
|
|
|
|
from selfprivacy_api.repositories.users import ACTIVE_USERS_PROVIDER
|
2024-10-26 18:22:31 +00:00
|
|
|
from selfprivacy_api.repositories.users.exceptions import (
|
|
|
|
UsernameForbidden,
|
|
|
|
UsernameNotAlphanumeric,
|
|
|
|
UsernameTooLong,
|
|
|
|
)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_users(
|
|
|
|
exclude_primary: bool = False,
|
|
|
|
exclude_root: bool = False,
|
|
|
|
) -> list[UserDataUser]:
|
2024-11-02 23:15:51 +00:00
|
|
|
return ACTIVE_USERS_PROVIDER.get_users(
|
|
|
|
exclude_primary=exclude_primary, exclude_root=exclude_root
|
|
|
|
)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
|
2024-11-15 00:19:24 +00:00
|
|
|
def create_user(
|
|
|
|
username: str,
|
|
|
|
password: Optional[str] = None,
|
|
|
|
displayname: Optional[str] = None,
|
|
|
|
email: Optional[str] = None,
|
|
|
|
directmemberof: Optional[list[str]] = None,
|
|
|
|
memberof: Optional[list[str]] = None,
|
|
|
|
) -> None:
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
if is_username_forbidden(username):
|
|
|
|
raise UsernameForbidden("Username is forbidden")
|
|
|
|
|
|
|
|
if not re.match(r"^[a-z_][a-z0-9_]+$", username):
|
|
|
|
raise UsernameNotAlphanumeric(
|
|
|
|
"Username must be alphanumeric and start with a letter"
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(username) >= 32:
|
|
|
|
raise UsernameTooLong("Username must be less than 32 characters")
|
|
|
|
|
2024-11-02 23:15:51 +00:00
|
|
|
return ACTIVE_USERS_PROVIDER.create_user(
|
2024-11-15 00:53:20 +00:00
|
|
|
username=username,
|
2024-11-15 14:30:51 +00:00
|
|
|
password=password,
|
2024-11-15 00:53:20 +00:00
|
|
|
displayname=displayname,
|
|
|
|
email=email,
|
|
|
|
directmemberof=directmemberof,
|
|
|
|
memberof=memberof,
|
2024-11-02 23:15:51 +00:00
|
|
|
)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
|
2024-10-26 18:22:31 +00:00
|
|
|
def delete_user(username: str) -> None:
|
2024-11-02 23:15:51 +00:00
|
|
|
return ACTIVE_USERS_PROVIDER.delete_user(username=username)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
|
2024-11-15 00:19:24 +00:00
|
|
|
def update_user(
|
|
|
|
username: str,
|
|
|
|
password: Optional[str] = None,
|
|
|
|
displayname: Optional[str] = None,
|
|
|
|
email: Optional[str] = None,
|
|
|
|
directmemberof: Optional[list[str]] = None,
|
|
|
|
memberof: Optional[list[str]] = None,
|
|
|
|
) -> None:
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2024-11-02 23:15:51 +00:00
|
|
|
return ACTIVE_USERS_PROVIDER.update_user(
|
2024-11-15 00:53:20 +00:00
|
|
|
username=username,
|
2024-11-15 14:30:51 +00:00
|
|
|
password=password,
|
2024-11-15 00:53:20 +00:00
|
|
|
displayname=displayname,
|
|
|
|
email=email,
|
|
|
|
directmemberof=directmemberof,
|
|
|
|
memberof=memberof,
|
2024-11-02 23:15:51 +00:00
|
|
|
)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_user_by_username(username: str) -> Optional[UserDataUser]:
|
2024-11-02 23:15:51 +00:00
|
|
|
return ACTIVE_USERS_PROVIDER.get_user_by_username(username=username)
|