make to not reuse alias

This commit is contained in:
Son NK 2019-11-18 15:10:16 +01:00
parent 5c119a933d
commit 539d87d0a3
4 changed files with 19 additions and 5 deletions

View file

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

View file

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

View file

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

View file

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