فهرست منبع

Make prefix generation configurable per domain

Sylvia van Os 4 سال پیش
والد
کامیت
26d0437009

+ 28 - 2
app/dashboard/templates/dashboard/domain_detail/info.html

@@ -57,7 +57,7 @@
   </div>
 
   <hr>
-  <div>Default Alias name</div>
+  <div>Default Alias Name</div>
   <div class="small-text">
     This name will be used as the default alias name when you send
     or reply from an alias, unless overwritten by the alias specific name.
@@ -76,6 +76,32 @@
     </form>
   </div>
 
+  <hr>
+  <div>Random Prefix Generation</div>
+  <div class="small-text">
+    A random prefix can be generated for this domain for usage in the New Alias
+    feature.
+  </div>
+
+  <div>
+    <form method="post">
+      <input type="hidden" name="form-name" value="switch-random-prefix-generation">
+      <label class="custom-switch cursor mt-2 pl-0"
+             data-toggle="tooltip"
+          {% if custom_domain.random_prefix_generation %}
+             title="Disable random prefix generation"
+          {% else %}
+             title="Enable random prefix generation"
+          {% endif %}
+      >
+        <input type="checkbox" class="custom-switch-input"
+            {{ "checked" if custom_domain.random_prefix_generation else "" }}>
+
+        <span class="custom-switch-indicator"></span>
+      </label>
+    </form>
+  </div>
+
   <hr>
   <h3 class="mb-0">Delete Domain</h3>
   <div class="small-text mb-3">Please note that this operation is irreversible.
@@ -119,4 +145,4 @@
       })
     });
   </script>
-{% endblock %}
+{% endblock %}

+ 5 - 3
app/dashboard/views/custom_alias.py

@@ -26,7 +26,7 @@ signer = TimestampSigner(CUSTOM_ALIAS_SECRET)
 
 def available_suffixes(user: User) -> [bool, str, str]:
     """Return (is_custom_domain, alias-suffix, time-signed alias-suffix)"""
-    user_custom_domains = [cd.domain for cd in user.verified_custom_domains()]
+    user_custom_domains = user.verified_custom_domains()
 
     # List of (is_custom_domain, alias-suffix, time-signed alias-suffix)
     suffixes = []
@@ -34,8 +34,10 @@ def available_suffixes(user: User) -> [bool, str, str]:
     # put custom domain first
     # for each user domain, generate both the domain and a random suffix version
     for alias_domain in user_custom_domains:
-        domain_suffixes = ["@" + alias_domain, "." + random_word() + "@" + alias_domain]
-        for suffix in domain_suffixes:
+        suffix = "@" + alias_domain.domain
+        suffixes.append((True, suffix, signer.sign(suffix).decode()))
+        if alias_domain.random_prefix_generation:
+            suffix = "." + random_word() + "@" + alias_domain.domain
             suffixes.append((True, suffix, signer.sign(suffix).decode()))
 
     # then default domain

+ 17 - 0
app/dashboard/views/domain_detail.py

@@ -160,6 +160,23 @@ 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") == "switch-random-prefix-generation":
+            custom_domain.random_prefix_generation = not custom_domain.random_prefix_generation
+            db.session.commit()
+
+            if custom_domain.random_prefix_generation:
+                flash(
+                    f"Random prefix generation has been enabled for {custom_domain.domain}",
+                    "success",
+                )
+            else:
+                flash(
+                    f"Random prefix generation has been disabled for {custom_domain.domain}",
+                    "warning",
+                )
+            return redirect(
+                url_for("dashboard.domain_detail", custom_domain_id=custom_domain.id)
+            )
         elif request.form.get("form-name") == "delete":
             name = custom_domain.domain
             CustomDomain.delete(custom_domain_id)

+ 3 - 0
app/models.py

@@ -1449,6 +1449,9 @@ 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")
 
+    # option to generate random prefix version automatically
+    random_prefix_generation = db.Column(db.Boolean, nullable=False, default=False, server_default="0")
+
     user = db.relationship(User, foreign_keys=[user_id])
 
     @property

+ 29 - 0
migrations/versions/2020_100922_a90e423c6763_.py

@@ -0,0 +1,29 @@
+"""empty message
+
+Revision ID: a90e423c6763
+Revises: 1abfc9e14d7e
+Create Date: 2020-10-09 22:35:11.359186
+
+"""
+import sqlalchemy_utils
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'a90e423c6763'
+down_revision = '1abfc9e14d7e'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('custom_domain', sa.Column('random_prefix_generation', sa.Boolean(), server_default='0', nullable=False))
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.drop_column('custom_domain', 'random_prefix_generation')
+    # ### end Alembic commands ###