From 821372fdfddc446763bd772a0e744d37e526ed25 Mon Sep 17 00:00:00 2001 From: Son NK Date: Mon, 10 Feb 2020 23:14:36 +0700 Subject: [PATCH] add email_already_used() and use it when creating user --- app/auth/views/facebook.py | 4 ++-- app/auth/views/github.py | 4 ++-- app/auth/views/google.py | 4 ++-- app/auth/views/register.py | 6 ++---- app/dashboard/views/setting.py | 4 ++-- app/email_utils.py | 15 +++++++++++++++ 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/app/auth/views/facebook.py b/app/auth/views/facebook.py index 3d590140..b59beceb 100644 --- a/app/auth/views/facebook.py +++ b/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", ) diff --git a/app/auth/views/github.py b/app/auth/views/github.py index a0488a7f..8f24f132 100644 --- a/app/auth/views/github.py +++ b/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", ) diff --git a/app/auth/views/google.py b/app/auth/views/google.py index 3538fed3..78792e60 100644 --- a/app/auth/views/google.py +++ b/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", ) diff --git a/app/auth/views/register.py b/app/auth/views/register.py index e4efa46d..3acb9a91 100644 --- a/app/auth/views/register.py +++ b/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) diff --git a/app/dashboard/views/setting.py b/app/dashboard/views/setting.py index f83e9a77..b1a6f294 100644 --- a/app/dashboard/views/setting.py +++ b/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) ): diff --git a/app/email_utils.py b/app/email_utils.py index 40accd84..d4ad481a 100644 --- a/app/email_utils.py +++ b/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