2019-08-30 22:42:06 +02:00
|
|
|
from flask import render_template, redirect, url_for, flash, request, session
|
2019-07-06 23:25:52 +02:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
2020-01-22 10:22:59 +01:00
|
|
|
from app.config import (
|
|
|
|
HIGHLIGHT_GEN_EMAIL_ID,
|
|
|
|
DISABLE_ALIAS_SUFFIX,
|
|
|
|
ALIAS_DOMAINS,
|
|
|
|
)
|
2019-07-06 23:25:52 +02:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2020-01-22 10:22:59 +01:00
|
|
|
from app.email_utils import email_belongs_to_alias_domains
|
2019-07-06 23:25:52 +02:00
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
2020-01-22 10:22:59 +01:00
|
|
|
from app.models import GenEmail
|
2019-12-31 17:11:42 +01:00
|
|
|
from app.utils import convert_to_id, random_word, word_exist
|
2019-07-06 23:25:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/custom_alias", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def custom_alias():
|
2020-01-22 00:03:25 +01:00
|
|
|
# check if user has not exceeded the alias quota
|
2019-12-22 17:27:55 +01:00
|
|
|
if not current_user.can_create_new_alias():
|
2019-07-06 23:25:52 +02:00
|
|
|
# notify admin
|
|
|
|
LOG.error("user %s tries to create custom alias", current_user)
|
|
|
|
flash("ony premium user can choose custom alias", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2020-01-22 00:03:25 +01:00
|
|
|
user_custom_domains = [cd.domain for cd in current_user.verified_custom_domains()]
|
2020-01-22 14:04:38 +01:00
|
|
|
# List of (is_custom_domain, alias-suffix)
|
2020-01-22 00:03:25 +01:00
|
|
|
suffixes = []
|
|
|
|
|
|
|
|
# put custom domain first
|
|
|
|
for alias_domain in user_custom_domains:
|
2020-01-22 14:04:38 +01:00
|
|
|
suffixes.append((True, "@" + alias_domain))
|
2020-01-22 00:03:25 +01:00
|
|
|
|
|
|
|
# then default domain
|
2020-01-22 10:22:59 +01:00
|
|
|
for domain in ALIAS_DOMAINS:
|
|
|
|
suffixes.append(
|
2020-01-22 14:04:38 +01:00
|
|
|
(False, ("" if DISABLE_ALIAS_SUFFIX else "." + random_word()) + "@" + domain)
|
2020-01-22 10:22:59 +01:00
|
|
|
)
|
2019-07-06 23:25:52 +02:00
|
|
|
|
2019-12-02 01:34:54 +01:00
|
|
|
if request.method == "POST":
|
2020-01-22 00:03:25 +01:00
|
|
|
alias_prefix = request.form.get("prefix")
|
|
|
|
alias_suffix = request.form.get("suffix")
|
|
|
|
|
|
|
|
if verify_prefix_suffix(
|
|
|
|
current_user, alias_prefix, alias_suffix, user_custom_domains
|
|
|
|
):
|
|
|
|
full_alias = alias_prefix + alias_suffix
|
|
|
|
if GenEmail.get_by(email=full_alias):
|
|
|
|
LOG.d("full alias already used %s", full_alias)
|
|
|
|
flash(
|
|
|
|
f"Alias {full_alias} already exists, please choose another one",
|
|
|
|
"warning",
|
2019-08-30 22:42:06 +02:00
|
|
|
)
|
2020-01-22 00:03:25 +01:00
|
|
|
else:
|
|
|
|
gen_email = GenEmail.create(user_id=current_user.id, email=full_alias)
|
2019-07-06 23:25:52 +02:00
|
|
|
db.session.commit()
|
2020-01-22 00:03:25 +01:00
|
|
|
flash(f"Alias {full_alias} has been created", "success")
|
2019-07-06 23:25:52 +02:00
|
|
|
|
2019-08-30 22:42:06 +02:00
|
|
|
session[HIGHLIGHT_GEN_EMAIL_ID] = gen_email.id
|
2020-01-22 00:03:25 +01:00
|
|
|
|
2019-07-06 23:25:52 +02:00
|
|
|
return redirect(url_for("dashboard.index"))
|
2020-01-22 00:03:25 +01:00
|
|
|
# only happen if the request has been "hacked"
|
|
|
|
else:
|
|
|
|
flash("something went wrong", "warning")
|
2019-07-06 23:25:52 +02:00
|
|
|
|
2020-01-22 00:03:25 +01:00
|
|
|
return render_template("dashboard/custom_alias.html", **locals())
|
|
|
|
|
|
|
|
|
|
|
|
def verify_prefix_suffix(user, alias_prefix, alias_suffix, user_custom_domains) -> bool:
|
|
|
|
"""verify if user could create an alias with the given prefix and suffix"""
|
|
|
|
alias_prefix = alias_prefix.strip()
|
|
|
|
alias_prefix = convert_to_id(alias_prefix)
|
|
|
|
if not alias_prefix: # should be caught on frontend
|
|
|
|
return False
|
|
|
|
|
|
|
|
# make sure alias_suffix is either .random_word@simplelogin.co or @my-domain.com
|
|
|
|
alias_suffix = alias_suffix.strip()
|
|
|
|
if alias_suffix.startswith("@"):
|
|
|
|
alias_domain = alias_suffix[1:]
|
2020-01-22 10:22:59 +01:00
|
|
|
# alias_domain can be either custom_domain or if DISABLE_ALIAS_SUFFIX, one of the default ALIAS_DOMAINS
|
2020-01-22 00:03:25 +01:00
|
|
|
if DISABLE_ALIAS_SUFFIX:
|
2020-01-22 10:22:59 +01:00
|
|
|
if (
|
|
|
|
alias_domain not in user_custom_domains
|
|
|
|
and alias_domain not in ALIAS_DOMAINS
|
|
|
|
):
|
2020-01-22 00:03:25 +01:00
|
|
|
LOG.error("wrong alias suffix %s, user %s", alias_suffix, user)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if alias_domain not in user_custom_domains:
|
|
|
|
LOG.error("wrong alias suffix %s, user %s", alias_suffix, user)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if not alias_suffix.startswith("."):
|
|
|
|
LOG.error("User %s submits a wrong alias suffix %s", user, alias_suffix)
|
|
|
|
return False
|
2020-01-22 10:22:59 +01:00
|
|
|
|
|
|
|
full_alias = alias_prefix + alias_suffix
|
|
|
|
if not email_belongs_to_alias_domains(full_alias):
|
2020-01-22 00:03:25 +01:00
|
|
|
LOG.error(
|
2020-01-22 10:22:59 +01:00
|
|
|
"Alias suffix should end with one of the alias domains %s",
|
2020-01-22 00:03:25 +01:00
|
|
|
user,
|
|
|
|
alias_suffix,
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
random_word_part = alias_suffix[1 : alias_suffix.find("@")]
|
|
|
|
if not word_exist(random_word_part):
|
|
|
|
LOG.error(
|
|
|
|
"alias suffix %s needs to start with a random word, user %s",
|
|
|
|
alias_suffix,
|
|
|
|
user,
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|