Explorar o código

support the case user wants to re-add their real email as mailbox

Son NK %!s(int64=5) %!d(string=hai) anos
pai
achega
fac833b8e6
Modificáronse 2 ficheiros con 19 adicións e 3 borrados
  1. 4 3
      app/dashboard/views/mailbox.py
  2. 15 0
      app/email_utils.py

+ 4 - 3
app/dashboard/views/mailbox.py

@@ -8,10 +8,11 @@ from wtforms.fields.html5 import EmailField
 from app.config import EMAIL_DOMAIN, ALIAS_DOMAINS, MAILBOX_SECRET, URL
 from app.dashboard.base import dashboard_bp
 from app.email_utils import (
-    send_email,
-    render,
     can_be_used_as_personal_email,
     email_already_used,
+    mailbox_already_used,
+    render,
+    send_email,
 )
 from app.extensions import db
 from app.log import LOG
@@ -80,7 +81,7 @@ def mailbox_route():
             if new_mailbox_form.validate():
                 mailbox_email = new_mailbox_form.email.data.lower()
 
-                if email_already_used(mailbox_email):
+                if mailbox_already_used(mailbox_email, current_user):
                     flash(f"{mailbox_email} already used", "error")
                 elif not can_be_used_as_personal_email(mailbox_email):
                     flash(f"You cannot use {mailbox_email}.", "error")

+ 15 - 0
app/email_utils.py

@@ -348,3 +348,18 @@ def email_already_used(email: str) -> bool:
         return True
 
     return False
+
+
+def mailbox_already_used(email: str, user) -> bool:
+    if Mailbox.get_by(email=email):
+        return True
+
+    # support the case user wants to re-add their real email as mailbox
+    # can happen when user changes their root email and wants to add this new email as mailbox
+    if email == user.email:
+        return False
+
+    if User.get_by(email=email):
+        return True
+
+    return False