user can set custom_domain.auto_create_regex

This commit is contained in:
Son Nguyen Kim 2021-09-17 17:41:36 +02:00
parent 58d36e9cd8
commit 0b127216ee
4 changed files with 97 additions and 14 deletions

View File

@ -12,23 +12,13 @@
<div class="small-text">Created {{ custom_domain.created_at | dt }}. {{ nb_alias }} aliases</div>
<hr>
<h3 class="mb-1">Catch All</h3>
<div class="">
Create aliases <b>on the fly</b>.
<h3 class="mb-1">Auto create/on the fly alias </h3>
Simply use <em>anything@{{ custom_domain.domain }}</em>
next time you need an alias: it'll be <b>automatically</b>
created the first time it receives an email. <br>
The new alias will belong to
{% for mailbox in custom_domain.mailboxes %}
<b>{{ mailbox.email }}</b>
{% if not loop.last %},{% endif %}
{% endfor %}
</div>
<div>
<form method="post">
<input type="hidden" name="form-name" value="switch-catch-all">
<label class="custom-switch cursor mt-2 pl-0"
data-toggle="tooltip"
{% if custom_domain.catch_all %}
@ -41,11 +31,76 @@
{{ "checked" if custom_domain.catch_all else "" }}>
<span class="custom-switch-indicator"></span>
<spam class="ml-2">
Catch All
</spam>
</label>
</form>
<div class="">
Simply use <b>anything@{{ custom_domain.domain }}</b>
next time you need an alias: it'll be <b>automatically</b>
created the first time it receives an email.
To have more fine-grained control, you can also use the
<a data-toggle="collapse" href="#regex-section">regular expression <i class="fe fe-chevrons-down"></i></a>.
</div>
<div class="{% if custom_domain.auto_create_regex is none %} collapse {% endif %}
{% if custom_domain.catch_all %} disabled-content {% endif %}
border border-info p-2"
id="regex-section">
<span class="badge badge-info">Advanced</span> <br>
You can also set a regular expression (regex): if an alias matches the expression, it'll be automatically created.
<br>
Please note that only the local part of the alias (i.e. <b>@{{ custom_domain.domain }}</b> is ignored) during the
regex
test.
<form method="post" class="form-inline" data-parsley-validate>
<input type="hidden" name="form-name" value="set-auto_create_regex">
<div class="form-group">
<input class="form-control mr-2"
value="{{ custom_domain.auto_create_regex or "" }}"
name="auto_create_regex"
required
data-parsley-pattern="[0-9a-z-_.(\\)(\*)]{1,}"
data-parsley-trigger="change"
data-parsley-error-message="Only lowercase letter, number, dot (.), dash (-), underscore (_), backslash (\), star (*) are currently supported."
placeholder="prefix\..*">
</div>
<button class="btn btn-outline-primary" name="action" value="save">Save</button>
{% if custom_domain.auto_create_regex %}
<button class="btn btn-outline-danger float-right ml-2" name="action" value="remove">Remove</button>
{% endif %}
</form>
For example, if you want aliases that starts with <b>prefix.</b> to be automatically created, you can set the
regex to <em data-toggle="tooltip"
title="Click to copy"
class="clipboard"
data-clipboard-text="prefix\..*">prefix\..*</em>
<br>
If you want aliases that ends with <b>.suffix</b> to be automatically created, you can use the regex
<em data-toggle="tooltip"
title="Click to copy"
class="clipboard"
data-clipboard-text=".*\.suffix">.*\.suffix</em>
<br>
To test out regex, we recommend using regex tester tool like <a href="https://regex101.com" target="_blank">https://regex101.com↗</a>
</div>
<div>
The new alias will belong to
{% for mailbox in custom_domain.mailboxes %}
<b>{{ mailbox.email }}</b>
{% if not loop.last %},{% endif %}
{% endfor %}
</div>
</div>
<div class="{% if not custom_domain.catch_all %} disabled-content {% endif %}">
<div class="{% if not custom_domain.auto_create_alias_enabled %} disabled-content {% endif %}">
<div>Auto-created aliases are automatically owned by these mailboxes</div>
{% set domain_mailboxes=custom_domain.mailboxes %}
<form method="post" class="mt-2">

View File

@ -158,7 +158,7 @@ def domain_detail_dns(custom_domain_id):
@dashboard_bp.route("/domains/<int:custom_domain_id>/info", methods=["GET", "POST"])
@login_required
def domain_detail(custom_domain_id):
custom_domain = CustomDomain.get(custom_domain_id)
custom_domain: CustomDomain = CustomDomain.get(custom_domain_id)
mailboxes = current_user.mailboxes()
if not custom_domain or custom_domain.user_id != current_user.id:
@ -261,6 +261,26 @@ def domain_detail(custom_domain_id):
return redirect(
url_for("dashboard.domain_detail", custom_domain_id=custom_domain.id)
)
elif request.form.get("form-name") == "set-auto_create_regex":
if request.form.get("action") == "save":
auto_create_regex = request.form.get("auto_create_regex")
if auto_create_regex:
custom_domain.auto_create_regex = auto_create_regex
db.session.commit()
flash("The auto create regex has been updated", "success")
else:
flash("The auto create regex cannot be empty", "error")
else:
custom_domain.auto_create_regex = None
db.session.commit()
flash(
f"The auto create regex has been has been removed",
"info",
)
return redirect(
url_for("dashboard.domain_detail", custom_domain_id=custom_domain.id)
)
elif request.form.get("form-name") == "delete":
name = custom_domain.domain
LOG.d("Schedule deleting %s", custom_domain)

View File

@ -1848,6 +1848,10 @@ class CustomDomain(db.Model, ModelMixin):
user = db.relationship(User, foreign_keys=[user_id])
@property
def auto_create_alias_enabled(self) -> bool:
return self.catch_all or self.auto_create_regex is not None
@property
def mailboxes(self):
if self._mailboxes:

4
static/style.css vendored
View File

@ -169,4 +169,8 @@ textarea.parsley-error {
.domain_detail_content {
font-size: 15px;
}
.domain_detail_content .parsley-errors-list {
max-width: 20em;
}