From 60e4b19d07ff46ec26a9c3fc109014ae058dbc33 Mon Sep 17 00:00:00 2001 From: jiangweidong Date: Thu, 7 Mar 2024 16:40:16 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=88=9B=E5=BB=BA=E7=9A=84=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E9=82=AE=E7=AE=B1=E4=BC=9A=E6=A0=A1=E9=AA=8C=EF=BC=8C=E9=9D=9E?= =?UTF-8?q?=E6=B3=95=E9=82=AE=E7=AE=B1=E4=BC=9A=E9=87=8D=E7=BD=AE=E6=88=90?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/users/utils.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/apps/users/utils.py b/apps/users/utils.py index 9ddb5509d..badecfdfc 100644 --- a/apps/users/utils.py +++ b/apps/users/utils.py @@ -230,16 +230,24 @@ class LoginIpBlockUtil(BlockGlobalIpUtilBase): BLOCK_KEY_TMPL = "_LOGIN_BLOCK_{}" +def validate_email(addr): + addr = addr or '' + pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' + if re.match(pattern, addr): + return addr + else: + return '' + + def construct_user_email(username, email, email_suffix=''): - if email is None: - email = '' - if '@' in email: - return email - if '@' in username: - return username - if not email_suffix: - email_suffix = settings.EMAIL_SUFFIX - email = f'{username}@{email_suffix}' + email_suffix = email_suffix or settings.EMAIL_SUFFIX + + email = validate_email(email) + if not email: + email = validate_email(username) + + if not email: + email = f'{username}@{email_suffix}' return email