diff --git a/app/dashboard/views/custom_alias.py b/app/dashboard/views/custom_alias.py index 4c36fd77..e06ce105 100644 --- a/app/dashboard/views/custom_alias.py +++ b/app/dashboard/views/custom_alias.py @@ -7,7 +7,7 @@ from app.config import EMAIL_DOMAIN, HIGHLIGHT_GEN_EMAIL_ID from app.dashboard.base import dashboard_bp from app.extensions import db from app.log import LOG -from app.models import GenEmail +from app.models import GenEmail, DeletedAlias from app.utils import convert_to_id, random_string @@ -38,7 +38,9 @@ def custom_alias(): else: full_email = f"{email}.{email_suffix}@{EMAIL_DOMAIN}" # check if email already exists - if GenEmail.get_by(email=full_email): + if GenEmail.get_by(email=full_email) or DeletedAlias.get_by( + email=full_email + ): error = "email already chosen, please choose another one" else: # create the new alias diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index 1376bf6a..a2551b93 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -9,7 +9,7 @@ from app.config import HIGHLIGHT_GEN_EMAIL_ID from app.dashboard.base import dashboard_bp from app.extensions import db from app.log import LOG -from app.models import GenEmail, ClientUser, ForwardEmail, ForwardEmailLog +from app.models import GenEmail, ClientUser, ForwardEmail, ForwardEmailLog, DeletedAlias @dataclass @@ -86,6 +86,10 @@ def index(): LOG.d("delete gen email %s", gen_email) email = gen_email.email GenEmail.delete(gen_email.id) + + # save deleted alias + DeletedAlias.create(user_id=current_user.id, email=gen_email.email) + db.session.commit() flash(f"Email alias {email} has been deleted", "success") diff --git a/app/models.py b/app/models.py index 47250130..d6e7b08a 100644 --- a/app/models.py +++ b/app/models.py @@ -380,7 +380,9 @@ def generate_email() -> str: random_email = random_words() + "@" + EMAIL_DOMAIN # check that the client does not exist yet - if not GenEmail.get_by(email=random_email): + if not GenEmail.get_by(email=random_email) and not DeletedAlias.get_by( + email=random_email + ): LOG.debug("generate email %s", random_email) return random_email diff --git a/app/oauth/views/authorize.py b/app/oauth/views/authorize.py index d983b785..22c038a6 100644 --- a/app/oauth/views/authorize.py +++ b/app/oauth/views/authorize.py @@ -2,7 +2,7 @@ import random from typing import Dict from urllib.parse import urlparse -from flask import request, render_template, redirect +from flask import request, render_template, redirect, flash from flask_login import current_user from app.config import EMAIL_DOMAIN @@ -16,6 +16,7 @@ from app.models import ( GenEmail, RedirectUri, OauthToken, + DeletedAlias, ) from app.oauth.base import oauth_bp from app.oauth_models import ( @@ -156,6 +157,11 @@ def authorize(): email = f"{convert_to_id(custom_email_prefix)}.{email_suffix}@{EMAIL_DOMAIN}" LOG.d("create custom email alias %s for user %s", email, current_user) + if GenEmail.get_by(email=email) or DeletedAlias.get_by(email=email): + LOG.error("email %s already used, very rare!", email) + flash(f"alias {email} already used", "error") + return redirect(request.url) + gen_email = GenEmail.create( email=email, user_id=current_user.id, custom=True )