瀏覽代碼

add email_already_used() and use it when creating user

Son NK 5 年之前
父節點
當前提交
821372fdfd
共有 6 個文件被更改,包括 25 次插入12 次删除
  1. 2 2
      app/auth/views/facebook.py
  2. 2 2
      app/auth/views/github.py
  3. 2 2
      app/auth/views/google.py
  4. 2 4
      app/auth/views/register.py
  5. 2 2
      app/dashboard/views/setting.py
  6. 15 0
      app/email_utils.py

+ 2 - 2
app/auth/views/facebook.py

@@ -16,7 +16,7 @@ from app.extensions import db
 from app.log import LOG
 from app.models import User
 from .login_utils import after_login
-from ...email_utils import can_be_used_as_personal_email
+from ...email_utils import can_be_used_as_personal_email, email_already_used
 
 _authorization_base_url = "https://www.facebook.com/dialog/oauth"
 _token_url = "https://graph.facebook.com/oauth/access_token"
@@ -112,7 +112,7 @@ def facebook_callback():
             flash("Registration is closed", "error")
             return redirect(url_for("auth.login"))
 
-        if not can_be_used_as_personal_email(email):
+        if not can_be_used_as_personal_email(email) or email_already_used(email):
             flash(
                 f"You cannot use {email} as your personal inbox.", "error",
             )

+ 2 - 2
app/auth/views/github.py

@@ -6,7 +6,7 @@ from app import email_utils
 from app.auth.base import auth_bp
 from app.auth.views.login_utils import after_login
 from app.config import GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, URL, DISABLE_REGISTRATION
-from app.email_utils import can_be_used_as_personal_email
+from app.email_utils import can_be_used_as_personal_email, email_already_used
 from app.extensions import db
 from app.log import LOG
 from app.models import User
@@ -89,7 +89,7 @@ def github_callback():
             flash("Registration is closed", "error")
             return redirect(url_for("auth.login"))
 
-        if not can_be_used_as_personal_email(email):
+        if not can_be_used_as_personal_email(email) or email_already_used(email):
             flash(
                 f"You cannot use {email} as your personal inbox.", "error",
             )

+ 2 - 2
app/auth/views/google.py

@@ -10,7 +10,7 @@ from app.log import LOG
 from app.models import User, File
 from app.utils import random_string
 from .login_utils import after_login
-from ...email_utils import can_be_used_as_personal_email
+from ...email_utils import can_be_used_as_personal_email, email_already_used
 
 _authorization_base_url = "https://accounts.google.com/o/oauth2/v2/auth"
 _token_url = "https://www.googleapis.com/oauth2/v4/token"
@@ -97,7 +97,7 @@ def google_callback():
             flash("Registration is closed", "error")
             return redirect(url_for("auth.login"))
 
-        if not can_be_used_as_personal_email(email):
+        if not can_be_used_as_personal_email(email) or email_already_used(email):
             flash(
                 f"You cannot use {email} as your personal inbox.", "error",
             )

+ 2 - 4
app/auth/views/register.py

@@ -6,7 +6,7 @@ from wtforms import StringField, validators
 from app import email_utils, config
 from app.auth.base import auth_bp
 from app.config import URL, DISABLE_REGISTRATION
-from app.email_utils import can_be_used_as_personal_email
+from app.email_utils import can_be_used_as_personal_email, email_already_used
 from app.extensions import db
 from app.log import LOG
 from app.models import User, ActivationCode
@@ -41,9 +41,7 @@ def register():
                 "You cannot use this email address as your personal inbox.", "error",
             )
         else:
-            user = User.get_by(email=email)
-
-            if user:
+            if email_already_used(email):
                 flash(f"Email {email} already used", "error")
             else:
                 LOG.debug("create user %s", form.email.data)

+ 2 - 2
app/dashboard/views/setting.py

@@ -11,7 +11,7 @@ from wtforms import StringField, validators
 from app import s3, email_utils
 from app.config import URL
 from app.dashboard.base import dashboard_bp
-from app.email_utils import can_be_used_as_personal_email
+from app.email_utils import can_be_used_as_personal_email, email_already_used
 from app.extensions import db
 from app.log import LOG
 from app.models import (
@@ -88,7 +88,7 @@ def setting():
 
                     # check if this email is not used by other user, or as alias
                     if (
-                        User.get_by(email=new_email)
+                        email_already_used(new_email)
                         or GenEmail.get_by(email=new_email)
                         or DeletedAlias.get_by(email=new_email)
                     ):

+ 15 - 0
app/email_utils.py

@@ -18,6 +18,7 @@ from app.config import (
     SUPPORT_NAME,
 )
 from app.log import LOG
+from app.models import Mailbox, User
 
 
 def render(template_name, **kwargs) -> str:
@@ -330,3 +331,17 @@ def can_be_used_as_personal_email(email: str) -> bool:
         return False
 
     return True
+
+
+def email_already_used(email: str) -> bool:
+    """test if an email can be used when:
+    - user signs up
+    - add a new mailbox
+    """
+    if User.get_by(email=email):
+        return True
+
+    if Mailbox.get_by(email=email):
+        return True
+
+    return False