From 571e39bb30ebef8aad0b1011ecba55bf08731537 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Mon, 28 Sep 2020 21:09:20 +0200 Subject: [PATCH] user can add/remove authorized address --- .../templates/dashboard/mailbox_detail.html | 41 +++++++++++++++++-- app/dashboard/views/mailbox_detail.py | 34 ++++++++++++++- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/app/dashboard/templates/dashboard/mailbox_detail.html b/app/dashboard/templates/dashboard/mailbox_detail.html index a9869438..0c763d5f 100644 --- a/app/dashboard/templates/dashboard/mailbox_detail.html +++ b/app/dashboard/templates/dashboard/mailbox_detail.html @@ -106,10 +106,10 @@ - {% if spf_available %} -
-

Advanced Options

+
+

Advanced Options

+ {% if spf_available %}
@@ -145,6 +145,41 @@
{% endif %} + +
+
+
+ Authorized addresses +
+ Emails sent from these addresses to a reverse-alias are considered as being sent + from {{ mailbox.email }} +
+
+ {% if mailbox.authorized_addresses | length == 0 %} + + {% else %} + + {% endif %} + +
+ + + +
+
+
+ {% endblock %} diff --git a/app/dashboard/views/mailbox_detail.py b/app/dashboard/views/mailbox_detail.py index bb264813..fc0a519d 100644 --- a/app/dashboard/views/mailbox_detail.py +++ b/app/dashboard/views/mailbox_detail.py @@ -14,7 +14,7 @@ from app.email_utils import email_domain_can_be_used_as_mailbox from app.email_utils import mailbox_already_used, render, send_email from app.extensions import db from app.log import LOG -from app.models import Alias, DeletedAlias +from app.models import Alias, DeletedAlias, AuthorizedAddress from app.models import Mailbox from app.pgp_utils import PGPException, load_public_key @@ -88,6 +88,38 @@ def mailbox_detail_route(mailbox_id): else "disabled" + " successfully", "success", ) + return redirect( + url_for("dashboard.mailbox_detail_route", mailbox_id=mailbox_id) + ) + elif request.form.get("form-name") == "add-authorized-address": + address = request.form.get("email").lower().strip().replace(" ", "") + if AuthorizedAddress.get_by(mailbox_id=mailbox.id, email=address): + flash(f"{address} already added", "error") + else: + AuthorizedAddress.create( + user_id=current_user.id, + mailbox_id=mailbox.id, + email=address, + commit=True, + ) + flash(f"{address} added as authorized address", "success") + + return redirect( + url_for("dashboard.mailbox_detail_route", mailbox_id=mailbox_id) + ) + elif request.form.get("form-name") == "delete-authorized-address": + authorized_address_id = request.form.get("authorized-address-id") + authorized_address: AuthorizedAddress = AuthorizedAddress.get( + authorized_address_id + ) + if not authorized_address or authorized_address.mailbox_id != mailbox.id: + flash("Unknown error. Refresh the page", "warning") + else: + address = authorized_address.email + AuthorizedAddress.delete(authorized_address_id) + db.session.commit() + flash(f"{address} has been deleted", "success") + return redirect( url_for("dashboard.mailbox_detail_route", mailbox_id=mailbox_id) )