From 3f0aae6f02d1b3385da12680f99139dfbfdcced1 Mon Sep 17 00:00:00 2001 From: Son NK Date: Thu, 16 Jan 2020 22:06:36 +0100 Subject: [PATCH 1/4] send email notifying user that alias creation works only in premium plan --- app/email_utils.py | 38 ++++++++++ app/models.py | 2 + email_handler.py | 69 +++++++++++++------ .../emails/cannot-create-alias-directory.txt | 7 ++ .../emails/cannot-create-alias-domain.txt | 7 ++ 5 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 templates/emails/cannot-create-alias-directory.txt create mode 100644 templates/emails/cannot-create-alias-domain.txt diff --git a/app/email_utils.py b/app/email_utils.py index b9e1f7ab..3a8cbceb 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -101,6 +101,44 @@ 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.txt", + 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.txt", 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/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.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. From 13283b63270cbe8ad483328fbbf5b3d871c3bfbe Mon Sep 17 00:00:00 2001 From: Son NK Date: Thu, 16 Jan 2020 22:21:19 +0100 Subject: [PATCH 2/4] Support cancelled premium users in custom domain & directory - Freemium user can see custom domain and directory but could not add new. - user who has added custom domain or directory before could delete them --- app/dashboard/templates/dashboard/custom_domain.html | 6 ++++++ app/dashboard/templates/dashboard/directory.html | 6 ++++++ app/dashboard/views/custom_domain.py | 9 ++++----- app/dashboard/views/directory.py | 11 +++++------ app/dashboard/views/domain_detail.py | 10 ---------- server.py | 4 ++++ 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app/dashboard/templates/dashboard/custom_domain.html b/app/dashboard/templates/dashboard/custom_domain.html index 6423fe44..f62df05d 100644 --- a/app/dashboard/templates/dashboard/custom_domain.html +++ b/app/dashboard/templates/dashboard/custom_domain.html @@ -13,6 +13,12 @@

Custom Domains

+ {% if not current_user.is_premium() %} + + {% endif %} + {% for custom_domain in custom_domains %}
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() %} + + {% endif %} +