add email_already_used() and use it when creating user

This commit is contained in:
Son NK 2020-02-10 23:14:36 +07:00
parent eca2422be4
commit 821372fdfd
6 changed files with 25 additions and 12 deletions

View File

@ -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",
)

View File

@ -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",
)

View File

@ -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",
)

View File

@ -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)

View File

@ -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)
):

View File

@ -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