From 58af7f30e8f3ae7425513ca49f3812c8492949a4 Mon Sep 17 00:00:00 2001 From: orenzhang Date: Tue, 14 Jan 2025 13:43:15 +0800 Subject: [PATCH 1/2] feat(login): support registry lock --- apps/account/exceptions.py | 5 ++ apps/account/views.py | 8 +++ apps/home/constants.py | 1 + locale/zh_Hans/LC_MESSAGES/django.po | 74 +++++++++++++++++++++++++++- 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/apps/account/exceptions.py b/apps/account/exceptions.py index d1fccad..86f6fa8 100644 --- a/apps/account/exceptions.py +++ b/apps/account/exceptions.py @@ -46,3 +46,8 @@ class OriginPasswordInvalid(APIException): class PhoneVerifyCodeInvalid(APIException): status_code = status.HTTP_400_BAD_REQUEST default_detail = gettext_lazy("Phone Verify Code Invalid") + + +class RegistryLocked(APIException): + status_code = status.HTTP_406_NOT_ACCEPTABLE + default_detail = gettext_lazy("Registry Disabled") diff --git a/apps/account/views.py b/apps/account/views.py index 9419d1d..986580b 100644 --- a/apps/account/views.py +++ b/apps/account/views.py @@ -21,6 +21,7 @@ WeChatAuthType, ) from apps.account.exceptions import ( + RegistryLocked, StateInvalid, UserNotExist, WeChatLoginFailed, @@ -38,6 +39,8 @@ VerifyCodeRequestSerializer, WeChatLoginReqSerializer, ) +from apps.home.constants import BuildInKeys +from apps.home.models import MetaConfig from core.auth import ApplicationAuthenticate from core.utils import is_wechat @@ -112,6 +115,11 @@ def sign_up(self, request, *args, **kwargs): sign up """ + # check registry locked + registry_lock_config: MetaConfig | None = MetaConfig.objects.filter(key=BuildInKeys.REGISTRY_LOCKED[0]).first() + if registry_lock_config and registry_lock_config.val: + raise RegistryLocked() + # validate request request_serializer = UserRegistrySerializer(data=request.data, context={"user_ip": get_ip(request)}) request_serializer.is_valid(raise_exception=True) diff --git a/apps/home/constants.py b/apps/home/constants.py index 744a179..c33d282 100644 --- a/apps/home/constants.py +++ b/apps/home/constants.py @@ -31,3 +31,4 @@ class BuildInKeys: PRIVACY_AGREEMENT: CONFIG_DEFAULT_T = "privacy_agreement", "" BACKGROUND_IMAGE: CONFIG_DEFAULT_T = "background_image", "" USERNAME_EXTRA_REGEX: CONFIG_DEFAULT_T = "username_extra_regex", "" + REGISTRY_LOCKED: CONFIG_DEFAULT_T = "registry_locked", "" diff --git a/locale/zh_Hans/LC_MESSAGES/django.po b/locale/zh_Hans/LC_MESSAGES/django.po index 9da88f5..f81f5d0 100644 --- a/locale/zh_Hans/LC_MESSAGES/django.po +++ b/locale/zh_Hans/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-12-27 13:19+0800\n" +"POT-Creation-Date: 2025-01-14 13:29+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -78,6 +78,9 @@ msgstr "原密码不正确" msgid "Phone Verify Code Invalid" msgstr "电话验证码错误" +msgid "Registry Disabled" +msgstr "未开放注册" + msgid "Username Cannot be Empty" msgstr "用户名不能为空" @@ -369,3 +372,72 @@ msgstr "认证 Token 非法" msgid "App Auth Failed" msgstr "应用鉴权失败" + +msgid "Mail Address" +msgstr "邮件地址" + +msgid "Code Verify Failed" +msgstr "代码验证失败" + +msgid "LIST" +msgstr "LIST" + +msgid "CREATE" +msgstr "CREATE" + +msgid "UPDATE" +msgstr "UPDATE" + +msgid "PARTIAL_UPDATE" +msgstr "PARTIAL_UPDATE" + +msgid "DESTROY" +msgstr "DESTROY" + +msgid "Request Failed" +msgstr "请求失败" + +msgid "Server Error" +msgstr "服务器异常" + +msgid "Login Required" +msgstr "未登录" + +msgid "Login Failed" +msgstr "登录失败" + +msgid "Permission Denied" +msgstr "未经授权" + +msgid "Operate Error" +msgstr "异常操作" + +msgid "Resource Not Found" +msgstr "资源未找到" + +msgid "Service Closed" +msgstr "服务已关闭" + +msgid "Soft Delete" +msgstr "软删除" + +msgid "Invalid Page Number" +msgstr "页码错误" + +msgid "TCaptcha" +msgstr "腾讯云验证码" + +msgid "Success" +msgstr "成功" + +msgid "Low" +msgstr "低" + +msgid "High" +msgstr "高" + +msgid "TCaptcha Verify Failed" +msgstr "验证码校验失败" + +msgid "App Performance" +msgstr "应用性能" From d12b2f75c9fb1246570f770f9304a3e35591f24b Mon Sep 17 00:00:00 2001 From: orenzhang Date: Tue, 14 Jan 2025 13:50:15 +0800 Subject: [PATCH 2/2] refactor(login): change registry lock status code --- apps/account/exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/account/exceptions.py b/apps/account/exceptions.py index 86f6fa8..3cafd0f 100644 --- a/apps/account/exceptions.py +++ b/apps/account/exceptions.py @@ -49,5 +49,5 @@ class PhoneVerifyCodeInvalid(APIException): class RegistryLocked(APIException): - status_code = status.HTTP_406_NOT_ACCEPTABLE + status_code = status.HTTP_503_SERVICE_UNAVAILABLE default_detail = gettext_lazy("Registry Disabled")