2020-12-27 13:03:07 +00:00
|
|
|
import typing as t
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2020-09-27 06:42:01 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2024-06-08 21:51:44 +00:00
|
|
|
from django.db.models import QuerySet
|
|
|
|
|
2022-05-08 13:10:48 +00:00
|
|
|
from etebase_server.myauth.models import UserType, get_typed_user_model
|
2020-09-27 06:42:01 +00:00
|
|
|
|
2020-07-13 11:30:18 +00:00
|
|
|
from . import app_settings
|
|
|
|
|
2020-12-29 11:22:36 +00:00
|
|
|
User = get_typed_user_model()
|
2020-07-13 11:30:18 +00:00
|
|
|
|
|
|
|
|
2020-12-27 13:03:07 +00:00
|
|
|
@dataclass
|
|
|
|
class CallbackContext:
|
|
|
|
"""Class for passing extra context to callbacks"""
|
|
|
|
|
|
|
|
url_kwargs: t.Dict[str, t.Any]
|
2020-12-29 11:22:36 +00:00
|
|
|
user: t.Optional[UserType] = None
|
2020-12-27 13:03:07 +00:00
|
|
|
|
|
|
|
|
2020-12-29 15:18:09 +00:00
|
|
|
def get_user_queryset(queryset: QuerySet[UserType], context: CallbackContext) -> QuerySet[UserType]:
|
2020-07-13 13:08:46 +00:00
|
|
|
custom_func = app_settings.GET_USER_QUERYSET_FUNC
|
2020-07-13 11:30:18 +00:00
|
|
|
if custom_func is not None:
|
2020-12-27 13:03:07 +00:00
|
|
|
return custom_func(queryset, context)
|
2020-07-13 11:30:18 +00:00
|
|
|
return queryset
|
2020-07-13 13:20:46 +00:00
|
|
|
|
|
|
|
|
2020-12-29 15:18:09 +00:00
|
|
|
def create_user(context: CallbackContext, *args, **kwargs) -> UserType:
|
2020-07-13 13:20:46 +00:00
|
|
|
custom_func = app_settings.CREATE_USER_FUNC
|
|
|
|
if custom_func is not None:
|
2020-12-28 15:46:20 +00:00
|
|
|
return custom_func(context, *args, **kwargs)
|
2020-07-13 13:20:46 +00:00
|
|
|
return User.objects.create_user(*args, **kwargs)
|
2020-09-27 06:42:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_user_blocked(*args, **kwargs):
|
2020-11-14 15:04:41 +00:00
|
|
|
raise PermissionDenied("Signup is disabled for this server. Please refer to the README for more information.")
|