2019-12-02 01:13:39 +01:00
|
|
|
from flask import render_template, request, redirect, url_for, flash
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import StringField, validators
|
|
|
|
|
2020-10-15 16:45:28 +02:00
|
|
|
from app.config import EMAIL_SERVERS_WITH_PRIORITY
|
2019-12-02 01:13:39 +01:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2020-01-28 09:16:26 +01:00
|
|
|
from app.email_utils import get_email_domain_part
|
2021-12-31 11:10:36 +01:00
|
|
|
from app.log import LOG
|
2020-10-15 16:51:07 +02:00
|
|
|
from app.models import CustomDomain, Mailbox, DomainMailbox, SLDomain
|
2019-12-02 01:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NewCustomDomainForm(FlaskForm):
|
2020-05-05 12:46:11 +02:00
|
|
|
domain = StringField(
|
|
|
|
"domain", validators=[validators.DataRequired(), validators.Length(max=128)]
|
|
|
|
)
|
2019-12-02 01:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/custom_domain", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def custom_domain():
|
2021-11-17 11:52:33 +01:00
|
|
|
custom_domains = CustomDomain.filter_by(
|
|
|
|
user_id=current_user.id, is_sl_subdomain=False
|
|
|
|
).all()
|
2020-08-01 12:31:02 +02:00
|
|
|
mailboxes = current_user.mailboxes()
|
2019-12-02 01:13:39 +01:00
|
|
|
new_custom_domain_form = NewCustomDomainForm()
|
|
|
|
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if request.method == "POST":
|
2019-12-27 23:50:09 +01:00
|
|
|
if request.form.get("form-name") == "create":
|
2020-01-16 22:21:19 +01:00
|
|
|
if not current_user.is_premium():
|
|
|
|
flash("Only premium plan can add custom domain", "warning")
|
|
|
|
return redirect(url_for("dashboard.custom_domain"))
|
|
|
|
|
2019-12-02 01:13:39 +01:00
|
|
|
if new_custom_domain_form.validate():
|
2020-01-28 09:16:26 +01:00
|
|
|
new_domain = new_custom_domain_form.domain.data.lower().strip()
|
2020-05-05 12:46:32 +02:00
|
|
|
|
|
|
|
if new_domain.startswith("http://"):
|
|
|
|
new_domain = new_domain[len("http://") :]
|
|
|
|
|
|
|
|
if new_domain.startswith("https://"):
|
|
|
|
new_domain = new_domain[len("https://") :]
|
|
|
|
|
2020-10-15 16:51:07 +02:00
|
|
|
if SLDomain.get_by(domain=new_domain):
|
2020-08-04 21:07:45 +02:00
|
|
|
flash("A custom domain cannot be a built-in domain.", "error")
|
|
|
|
elif CustomDomain.get_by(domain=new_domain):
|
2021-10-15 10:32:20 +02:00
|
|
|
flash(f"{new_domain} already used", "error")
|
2020-01-28 09:16:26 +01:00
|
|
|
elif get_email_domain_part(current_user.email) == new_domain:
|
|
|
|
flash(
|
|
|
|
"You cannot add a domain that you are currently using for your personal email. "
|
|
|
|
"Please change your personal email to your real email",
|
|
|
|
"error",
|
|
|
|
)
|
2021-10-15 10:32:20 +02:00
|
|
|
elif Mailbox.filter(
|
2021-10-15 10:39:29 +02:00
|
|
|
Mailbox.verified.is_(True), Mailbox.email.endswith(f"@{new_domain}")
|
2021-10-15 10:32:20 +02:00
|
|
|
).first():
|
|
|
|
flash(
|
|
|
|
f"{new_domain} already used in a SimpleLogin mailbox", "error"
|
|
|
|
)
|
2020-01-01 23:51:40 +01:00
|
|
|
else:
|
|
|
|
new_custom_domain = CustomDomain.create(
|
|
|
|
domain=new_domain, user_id=current_user.id
|
|
|
|
)
|
2021-12-31 11:10:36 +01:00
|
|
|
# new domain has ownership verified if its parent has the ownership verified
|
|
|
|
for root_cd in current_user.custom_domains:
|
|
|
|
if (
|
|
|
|
new_domain.endswith("." + root_cd.domain)
|
|
|
|
and root_cd.ownership_verified
|
|
|
|
):
|
|
|
|
LOG.i(
|
|
|
|
"%s ownership verified thanks to %s",
|
|
|
|
new_custom_domain,
|
|
|
|
root_cd,
|
|
|
|
)
|
|
|
|
new_custom_domain.ownership_verified = True
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2019-12-02 01:13:39 +01:00
|
|
|
|
2020-08-01 12:31:02 +02:00
|
|
|
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_domain"))
|
|
|
|
mailboxes.append(mailbox)
|
|
|
|
|
|
|
|
for mailbox in mailboxes:
|
|
|
|
DomainMailbox.create(
|
|
|
|
domain_id=new_custom_domain.id, mailbox_id=mailbox.id
|
|
|
|
)
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-08-01 12:31:02 +02:00
|
|
|
|
2020-01-01 23:51:40 +01:00
|
|
|
flash(
|
|
|
|
f"New domain {new_custom_domain.domain} is created", "success"
|
|
|
|
)
|
2019-12-15 08:52:20 +01:00
|
|
|
|
2020-01-01 23:51:40 +01:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_dns",
|
|
|
|
custom_domain_id=new_custom_domain.id,
|
|
|
|
)
|
2019-12-02 01:13:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"dashboard/custom_domain.html",
|
|
|
|
custom_domains=custom_domains,
|
|
|
|
new_custom_domain_form=new_custom_domain_form,
|
|
|
|
EMAIL_SERVERS_WITH_PRIORITY=EMAIL_SERVERS_WITH_PRIORITY,
|
|
|
|
errors=errors,
|
2020-08-01 12:31:02 +02:00
|
|
|
mailboxes=mailboxes,
|
2019-12-02 01:13:39 +01:00
|
|
|
)
|