Son NK 5 лет назад
Родитель
Сommit
539d87d0a3

+ 4 - 2
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

+ 5 - 1
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")
 

+ 3 - 1
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
 

+ 7 - 1
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
                 )