From 8161d89c392fd5932354c26fefe65e6d4c813095 Mon Sep 17 00:00:00 2001
From: Son NK <>
Date: Fri, 5 Jun 2020 22:08:08 +0200
Subject: [PATCH 1/4] Create DirectoryMailbox model
---
app/models.py | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/app/models.py b/app/models.py
index a5b23f04..0bc40075 100644
--- a/app/models.py
+++ b/app/models.py
@@ -1272,6 +1272,17 @@ class Directory(db.Model, ModelMixin):
user = db.relationship(User)
+ _mailboxes = db.relationship(
+ "Mailbox", secondary="directory_mailbox", lazy="joined"
+ )
+
+ @property
+ def mailboxes(self):
+ if self._mailboxes:
+ return self._mailboxes
+ else:
+ return [self.user.default_mailbox]
+
def nb_alias(self):
return Alias.filter_by(directory_id=self.id).count()
@@ -1446,6 +1457,19 @@ class AliasMailbox(db.Model, ModelMixin):
)
+class DirectoryMailbox(db.Model, ModelMixin):
+ __table_args__ = (
+ db.UniqueConstraint("directory_id", "mailbox_id", name="uq_directory_mailbox"),
+ )
+
+ directory_id = db.Column(
+ db.ForeignKey(Directory.id, ondelete="cascade"), nullable=False
+ )
+ mailbox_id = db.Column(
+ db.ForeignKey(Mailbox.id, ondelete="cascade"), nullable=False
+ )
+
+
_NB_RECOVERY_CODE = 8
_RECOVERY_CODE_LENGTH = 8
From 18b530fe6fac7fd8548157fcd2a610789dc43c71 Mon Sep 17 00:00:00 2001
From: Son NK <>
Date: Fri, 5 Jun 2020 22:10:52 +0200
Subject: [PATCH 2/4] Able to set mailboxes when creating directory
---
.../templates/dashboard/directory.html | 22 +++++++++++++++-
app/dashboard/views/directory.py | 26 ++++++++++++++++++-
2 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/app/dashboard/templates/dashboard/directory.html b/app/dashboard/templates/dashboard/directory.html
index 9036063d..c497f27b 100644
--- a/app/dashboard/templates/dashboard/directory.html
+++ b/app/dashboard/templates/dashboard/directory.html
@@ -105,7 +105,24 @@
Only lowercase letters, numbers, dashes (-) and underscores (_) are currently supported.
-
+
+ By default, aliases created with directory are "owned" by your default
+ mailbox {{ current_user.default_mailbox.email }}.
+ You can however choose the mailbox(es) that new alias automatically belongs to by setting this below option.
+
+
+
+
+
+
@@ -142,5 +159,8 @@
})
});
+
+ $('.mailbox-select').multipleSelect();
+
{% endblock %}
diff --git a/app/dashboard/views/directory.py b/app/dashboard/views/directory.py
index 9c992cd5..3674bcb4 100644
--- a/app/dashboard/views/directory.py
+++ b/app/dashboard/views/directory.py
@@ -6,7 +6,7 @@ from wtforms import StringField, validators
from app.config import EMAIL_DOMAIN, ALIAS_DOMAINS, MAX_NB_DIRECTORY
from app.dashboard.base import dashboard_bp
from app.extensions import db
-from app.models import Directory
+from app.models import Directory, Mailbox, DirectoryMailbox
class NewDirForm(FlaskForm):
@@ -24,6 +24,8 @@ def directory():
.all()
)
+ mailboxes = current_user.mailboxes()
+
new_dir_form = NewDirForm()
if request.method == "POST":
@@ -72,6 +74,27 @@ def directory():
name=new_dir_name, user_id=current_user.id
)
db.session.commit()
+ mailbox_ids = request.form.getlist("mailbox_ids")
+ if mailbox_ids:
+ # check if mailbox is not tempered with
+ mailboxes = []
+ for mailbox_id in mailbox_ids:
+ mailbox = Mailbox.get(mailbox_id)
+ if (
+ not mailbox
+ or mailbox.user_id != current_user.id
+ or not mailbox.verified
+ ):
+ flash("Something went wrong, please retry", "warning")
+ return redirect(url_for("dashboard.custom_alias"))
+ mailboxes.append(mailbox)
+
+ for mailbox in mailboxes:
+ DirectoryMailbox.create(
+ directory_id=new_dir.id, mailbox_id=mailbox.id
+ )
+
+ db.session.commit()
flash(f"Directory {new_dir.name} is created", "success")
@@ -81,6 +104,7 @@ def directory():
"dashboard/directory.html",
dirs=dirs,
new_dir_form=new_dir_form,
+ mailboxes=mailboxes,
EMAIL_DOMAIN=EMAIL_DOMAIN,
ALIAS_DOMAINS=ALIAS_DOMAINS,
)
From bc01479a72e5703216f6bdd2e65e6c34b582411c Mon Sep 17 00:00:00 2001
From: Son NK <>
Date: Fri, 5 Jun 2020 22:13:35 +0200
Subject: [PATCH 3/4] user can update directory mailboxes
---
.../templates/dashboard/directory.html | 23 +++++++++++
app/dashboard/views/directory.py | 39 +++++++++++++++++++
2 files changed, 62 insertions(+)
diff --git a/app/dashboard/templates/dashboard/directory.html b/app/dashboard/templates/dashboard/directory.html
index c497f27b..33881bc4 100644
--- a/app/dashboard/templates/dashboard/directory.html
+++ b/app/dashboard/templates/dashboard/directory.html
@@ -66,6 +66,29 @@