diff --git a/app/dashboard/templates/dashboard/directory.html b/app/dashboard/templates/dashboard/directory.html
index 8fe2c951..b1cc830a 100644
--- a/app/dashboard/templates/dashboard/directory.html
+++ b/app/dashboard/templates/dashboard/directory.html
@@ -10,6 +10,12 @@
Directories
+ {% if not current_user.is_premium() %}
+
+ This feature is only available in premium plan.
+
+ {% endif %}
+
Directory allows you to create aliases on the fly.
Simply use directory/anything@{{ EMAIL_DOMAIN }}
diff --git a/app/dashboard/views/custom_domain.py b/app/dashboard/views/custom_domain.py
index e9ccd1b3..c28cfb84 100644
--- a/app/dashboard/views/custom_domain.py
+++ b/app/dashboard/views/custom_domain.py
@@ -17,11 +17,6 @@ class NewCustomDomainForm(FlaskForm):
@dashboard_bp.route("/custom_domain", methods=["GET", "POST"])
@login_required
def custom_domain():
- # only premium user can add custom domain
- if not current_user.is_premium():
- flash("Only premium user can add custom domains", "warning")
- return redirect(url_for("dashboard.index"))
-
custom_domains = CustomDomain.query.filter_by(user_id=current_user.id).all()
new_custom_domain_form = NewCustomDomainForm()
@@ -30,6 +25,10 @@ def custom_domain():
if request.method == "POST":
if request.form.get("form-name") == "create":
+ if not current_user.is_premium():
+ flash("Only premium plan can add custom domain", "warning")
+ return redirect(url_for("dashboard.custom_domain"))
+
if new_custom_domain_form.validate():
new_domain = new_custom_domain_form.domain.data.strip()
if CustomDomain.get_by(domain=new_domain):
diff --git a/app/dashboard/views/directory.py b/app/dashboard/views/directory.py
index 8f3befee..66eaa23f 100644
--- a/app/dashboard/views/directory.py
+++ b/app/dashboard/views/directory.py
@@ -18,11 +18,6 @@ class NewDirForm(FlaskForm):
@dashboard_bp.route("/directory", methods=["GET", "POST"])
@login_required
def directory():
- # only premium user can add directory
- if not current_user.is_premium():
- flash("Only premium user can add directories", "warning")
- return redirect(url_for("dashboard.index"))
-
dirs = Directory.query.filter_by(user_id=current_user.id).all()
new_dir_form = NewDirForm()
@@ -47,6 +42,10 @@ def directory():
return redirect(url_for("dashboard.directory"))
elif request.form.get("form-name") == "create":
+ if not current_user.is_premium():
+ flash("Only premium plan can add directory", "warning")
+ return redirect(url_for("dashboard.directory"))
+
if new_dir_form.validate():
new_dir_name = new_dir_form.name.data.lower()
@@ -60,7 +59,7 @@ def directory():
flash(f"Directory {new_dir.name} is created", "success")
- return redirect(url_for("dashboard.directory",))
+ return redirect(url_for("dashboard.directory"))
return render_template(
"dashboard/directory.html",
diff --git a/app/dashboard/views/domain_detail.py b/app/dashboard/views/domain_detail.py
index 88bcf98b..86983f8c 100644
--- a/app/dashboard/views/domain_detail.py
+++ b/app/dashboard/views/domain_detail.py
@@ -16,11 +16,6 @@ from app.models import CustomDomain, GenEmail
@dashboard_bp.route("/domains//dns", methods=["GET", "POST"])
@login_required
def domain_detail_dns(custom_domain_id):
- # only premium user can see custom domain
- if not current_user.is_premium():
- flash("Only premium user can add custom domains", "warning")
- return redirect(url_for("dashboard.index"))
-
custom_domain = CustomDomain.get(custom_domain_id)
if not custom_domain or custom_domain.user_id != current_user.id:
flash("You cannot see this page", "warning")
@@ -103,11 +98,6 @@ def domain_detail_dns(custom_domain_id):
@dashboard_bp.route("/domains//info", methods=["GET", "POST"])
@login_required
def domain_detail(custom_domain_id):
- # only premium user can see custom domain
- if not current_user.is_premium():
- flash("Only premium user can add custom domains", "warning")
- return redirect(url_for("dashboard.index"))
-
custom_domain = CustomDomain.get(custom_domain_id)
if not custom_domain or custom_domain.user_id != current_user.id:
flash("You cannot see this page", "warning")
diff --git a/app/email_utils.py b/app/email_utils.py
index b9e1f7ab..3a205ff6 100644
--- a/app/email_utils.py
+++ b/app/email_utils.py
@@ -101,6 +101,47 @@ def send_test_email_alias(email, name):
)
+def send_cannot_create_directory_alias(user, alias, directory):
+ """when user cancels their subscription, they cannot create alias on the fly.
+ If this happens, send them an email to notify
+ """
+ send_email(
+ user.email,
+ f"Alias {alias} cannot be created",
+ _render(
+ "cannot-create-alias-directory.txt",
+ name=user.name,
+ alias=alias,
+ directory=directory,
+ ),
+ _render(
+ "cannot-create-alias-directory.html",
+ name=user.name,
+ alias=alias,
+ directory=directory,
+ ),
+ )
+
+
+def send_cannot_create_domain_alias(user, alias, domain):
+ """when user cancels their subscription, they cannot create alias on the fly with custom domain.
+ If this happens, send them an email to notify
+ """
+ send_email(
+ user.email,
+ f"Alias {alias} cannot be created",
+ _render(
+ "cannot-create-alias-domain.txt", name=user.name, alias=alias, domain=domain
+ ),
+ _render(
+ "cannot-create-alias-domain.html",
+ name=user.name,
+ alias=alias,
+ domain=domain,
+ ),
+ )
+
+
def send_email(to_email, subject, plaintext, html):
if NOT_SEND_EMAIL:
LOG.d(
diff --git a/app/models.py b/app/models.py
index 11c88897..e4795196 100644
--- a/app/models.py
+++ b/app/models.py
@@ -720,6 +720,8 @@ class CustomDomain(db.Model, ModelMixin):
# an alias is created automatically the first time it receives an email
catch_all = db.Column(db.Boolean, nullable=False, default=False, server_default="0")
+ user = db.relationship(User)
+
def nb_alias(self):
return GenEmail.filter_by(custom_domain_id=self.id).count()
diff --git a/email_handler.py b/email_handler.py
index d29a8063..a96fca55 100644
--- a/email_handler.py
+++ b/email_handler.py
@@ -47,6 +47,8 @@ from app.email_utils import (
get_email_domain_part,
add_or_replace_header,
delete_header,
+ send_cannot_create_directory_alias,
+ send_cannot_create_domain_alias,
)
from app.extensions import db
from app.log import LOG
@@ -110,7 +112,9 @@ class MailHandler:
gen_email = GenEmail.get_by(email=alias)
if not gen_email:
- LOG.d("alias %s not exist. Try to see if it can created on the fly", alias)
+ LOG.d(
+ "alias %s not exist. Try to see if it can be created on the fly", alias
+ )
# try to see if alias could be created on-the-fly
on_the_fly = False
@@ -122,31 +126,56 @@ class MailHandler:
LOG.d("directory_name %s", directory_name)
directory = Directory.get_by(name=directory_name)
- if directory:
- LOG.d("create alias %s for directory %s", alias, directory)
- on_the_fly = True
- gen_email = GenEmail.create(
- email=alias,
- user_id=directory.user_id,
- directory_id=directory.id,
- )
- db.session.commit()
+ # Only premium user can continue using the directory feature
+ if directory:
+ dir_user = directory.user
+ if dir_user.is_premium():
+ LOG.d("create alias %s for directory %s", alias, directory)
+ on_the_fly = True
+
+ gen_email = GenEmail.create(
+ email=alias,
+ user_id=directory.user_id,
+ directory_id=directory.id,
+ )
+ db.session.commit()
+ else:
+ LOG.error(
+ "User %s is not premium anymore and cannot create alias with directory",
+ dir_user,
+ )
+ send_cannot_create_directory_alias(
+ dir_user, alias, directory_name
+ )
else:
# check if alias is custom-domain alias and if the custom-domain has catch-all enabled
alias_domain = get_email_domain_part(alias)
custom_domain = CustomDomain.get_by(domain=alias_domain)
- if custom_domain and custom_domain.catch_all:
- LOG.d("create alias %s for domain %s", alias, custom_domain)
- on_the_fly = True
- gen_email = GenEmail.create(
- email=alias,
- user_id=custom_domain.user_id,
- custom_domain_id=custom_domain.id,
- automatic_creation=True,
- )
- db.session.commit()
+ # Only premium user can continue using the catch-all feature
+ if custom_domain and custom_domain.catch_all:
+ domain_user = custom_domain.user
+ if domain_user.is_premium():
+ LOG.d("create alias %s for domain %s", alias, custom_domain)
+ on_the_fly = True
+
+ gen_email = GenEmail.create(
+ email=alias,
+ user_id=custom_domain.user_id,
+ custom_domain_id=custom_domain.id,
+ automatic_creation=True,
+ )
+ db.session.commit()
+ else:
+ LOG.error(
+ "User %s is not premium anymore and cannot create alias with domain %s",
+ domain_user,
+ alias_domain,
+ )
+ send_cannot_create_domain_alias(
+ domain_user, alias, alias_domain
+ )
if not on_the_fly:
LOG.d("alias %s not exist, return 510", alias)
diff --git a/server.py b/server.py
index e51272c2..3848f60b 100644
--- a/server.py
+++ b/server.py
@@ -46,6 +46,7 @@ from app.models import (
ApiKey,
CustomDomain,
LifetimeCoupon,
+ Directory,
)
from app.monitor.base import monitor_bp
from app.oauth.base import oauth_bp
@@ -158,6 +159,10 @@ def fake_data():
)
db.session.commit()
+ Directory.create(user_id=user.id, name="abcd")
+ Directory.create(user_id=user.id, name="xyzt")
+ db.session.commit()
+
# Create a client
client1 = Client.create_new(name="Demo", user_id=user.id)
client1.oauth_client_id = "client-id"
diff --git a/templates/emails/cannot-create-alias-directory.html b/templates/emails/cannot-create-alias-directory.html
new file mode 100644
index 00000000..48e28bb6
--- /dev/null
+++ b/templates/emails/cannot-create-alias-directory.html
@@ -0,0 +1,8 @@
+Hi {{name}}
+
+An email has been sent to the alias {{alias}} that would be created automatically as you own the directory {{directory}}. However as your plan is no longer premium, this creation cannot happen.
+
+Please upgrade to premium plan in order to use this feature.
+
+Best,
+SimpleLogin team.
diff --git a/templates/emails/cannot-create-alias-directory.txt b/templates/emails/cannot-create-alias-directory.txt
new file mode 100644
index 00000000..25641494
--- /dev/null
+++ b/templates/emails/cannot-create-alias-directory.txt
@@ -0,0 +1,7 @@
+Hi {{name}}
+
+An email has been sent to the alias {{alias}} that would be created automatically as you own the directory {{directory}}. However as your plan is no longer premium, this creation cannot happen.
+
+Please upgrade to premium plan in order to use this feature.
+Best,
+SimpleLogin team.
diff --git a/templates/emails/cannot-create-alias-domain.html b/templates/emails/cannot-create-alias-domain.html
new file mode 100644
index 00000000..cf21d1ca
--- /dev/null
+++ b/templates/emails/cannot-create-alias-domain.html
@@ -0,0 +1,8 @@
+Hi {{name}}
+
+An email has been sent to the alias {{alias}} that would be created automatically as you own the domain {{domain}}. However as your plan is no longer premium, this creation cannot happen.
+
+Please upgrade to premium plan in order to use this feature.
+
+Best,
+SimpleLogin team.
diff --git a/templates/emails/cannot-create-alias-domain.txt b/templates/emails/cannot-create-alias-domain.txt
new file mode 100644
index 00000000..300a327f
--- /dev/null
+++ b/templates/emails/cannot-create-alias-domain.txt
@@ -0,0 +1,7 @@
+Hi {{name}}
+
+An email has been sent to the alias {{alias}} that would be created automatically as you own the domain {{domain}}. However as your plan is no longer premium, this creation cannot happen.
+
+Please upgrade to premium plan in order to use this feature.
+Best,
+SimpleLogin team.