Quellcode durchsuchen

add email_already_used() and use it when creating user

Son NK vor 5 Jahren
Ursprung
Commit
821372fdfd

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

@@ -16,7 +16,7 @@ from app.extensions import db
 from app.log import LOG
 from app.log import LOG
 from app.models import User
 from app.models import User
 from .login_utils import after_login
 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"
 _authorization_base_url = "https://www.facebook.com/dialog/oauth"
 _token_url = "https://graph.facebook.com/oauth/access_token"
 _token_url = "https://graph.facebook.com/oauth/access_token"
@@ -112,7 +112,7 @@ def facebook_callback():
             flash("Registration is closed", "error")
             flash("Registration is closed", "error")
             return redirect(url_for("auth.login"))
             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(
             flash(
                 f"You cannot use {email} as your personal inbox.", "error",
                 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.base import auth_bp
 from app.auth.views.login_utils import after_login
 from app.auth.views.login_utils import after_login
 from app.config import GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, URL, DISABLE_REGISTRATION
 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.extensions import db
 from app.log import LOG
 from app.log import LOG
 from app.models import User
 from app.models import User
@@ -89,7 +89,7 @@ def github_callback():
             flash("Registration is closed", "error")
             flash("Registration is closed", "error")
             return redirect(url_for("auth.login"))
             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(
             flash(
                 f"You cannot use {email} as your personal inbox.", "error",
                 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.models import User, File
 from app.utils import random_string
 from app.utils import random_string
 from .login_utils import after_login
 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"
 _authorization_base_url = "https://accounts.google.com/o/oauth2/v2/auth"
 _token_url = "https://www.googleapis.com/oauth2/v4/token"
 _token_url = "https://www.googleapis.com/oauth2/v4/token"
@@ -97,7 +97,7 @@ def google_callback():
             flash("Registration is closed", "error")
             flash("Registration is closed", "error")
             return redirect(url_for("auth.login"))
             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(
             flash(
                 f"You cannot use {email} as your personal inbox.", "error",
                 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 import email_utils, config
 from app.auth.base import auth_bp
 from app.auth.base import auth_bp
 from app.config import URL, DISABLE_REGISTRATION
 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.extensions import db
 from app.log import LOG
 from app.log import LOG
 from app.models import User, ActivationCode
 from app.models import User, ActivationCode
@@ -41,9 +41,7 @@ def register():
                 "You cannot use this email address as your personal inbox.", "error",
                 "You cannot use this email address as your personal inbox.", "error",
             )
             )
         else:
         else:
-            user = User.get_by(email=email)
-
-            if user:
+            if email_already_used(email):
                 flash(f"Email {email} already used", "error")
                 flash(f"Email {email} already used", "error")
             else:
             else:
                 LOG.debug("create user %s", form.email.data)
                 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 import s3, email_utils
 from app.config import URL
 from app.config import URL
 from app.dashboard.base import dashboard_bp
 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.extensions import db
 from app.log import LOG
 from app.log import LOG
 from app.models import (
 from app.models import (
@@ -88,7 +88,7 @@ def setting():
 
 
                     # check if this email is not used by other user, or as alias
                     # check if this email is not used by other user, or as alias
                     if (
                     if (
-                        User.get_by(email=new_email)
+                        email_already_used(new_email)
                         or GenEmail.get_by(email=new_email)
                         or GenEmail.get_by(email=new_email)
                         or DeletedAlias.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,
     SUPPORT_NAME,
 )
 )
 from app.log import LOG
 from app.log import LOG
+from app.models import Mailbox, User
 
 
 
 
 def render(template_name, **kwargs) -> str:
 def render(template_name, **kwargs) -> str:
@@ -330,3 +331,17 @@ def can_be_used_as_personal_email(email: str) -> bool:
         return False
         return False
 
 
     return True
     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