FIX: Do not open the composer if encryption is disabled.

FIX: Do not open the composer if encryption is disabled.

diff --git a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6 b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
index 2bf1dcd..4e87966 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
@@ -16,6 +16,7 @@ import {
   deleteIndexedDb
 } from "discourse/plugins/discourse-encrypt/lib/keys_db";
 import {
+  canEnableEncrypt,
   ENCRYPT_ACTIVE,
   ENCRYPT_DISABLED,
   getEncryptionStatus,
@@ -30,38 +31,6 @@ import {
 // lead to a lot of JavaScript bloat.
 registerHelper("or", ([a, b]) => a || b);
 
-/**
- * Checks if a specific user can enable encryption.
- *
- * This check ensures that:
- *    - user already has encryption enabled OR
- *    - encryption plug-in is enabled AND
- *    - there is no group restriction or user is in one of the allowed groups.
- *
- * @param user
- *
- * @return
- */
-function canEnableEncrypt(user) {
-  if (getEncryptionStatus(user) !== ENCRYPT_DISABLED) {
-    return true;
-  }
-
-  if (Discourse.SiteSettings.encrypt_enabled) {
-    if (Discourse.SiteSettings.encrypt_groups.length === 0) {
-      return true;
-    }
-
-    const encryptGroups = Discourse.SiteSettings.encrypt_groups.split("|");
-    const groups = (user.get("groups") || []).map(group => group.get("name"));
-    if (groups.some(group => encryptGroups.includes(group))) {
-      return true;
-    }
-  }
-
-  return false;
-}
-
 export default {
   setupComponent(args, component) {
     const currentUser = Discourse.User.current();
diff --git a/assets/javascripts/discourse/initializers/hook-composer-controller.js.es6 b/assets/javascripts/discourse/initializers/hook-composer-controller.js.es6
new file mode 100644
index 0000000..cb9dedc
--- /dev/null
+++ b/assets/javascripts/discourse/initializers/hook-composer-controller.js.es6
@@ -0,0 +1,35 @@
+import Composer from "discourse/controllers/composer";
+import {
+  canEnableEncrypt,
+  ENCRYPT_ACTIVE,
+  getEncryptionStatus
+} from "discourse/plugins/discourse-encrypt/lib/discourse";
+
+export default {
+  name: "hook-composer-controller",
+
+  initialize(container) {
+    const currentUser = container.lookup("current-user:main");
+    if (!canEnableEncrypt(currentUser)) {
+      return;
+    }
+
+    Composer.reopen({
+      open(opts) {
+        // TODO: https://github.com/emberjs/ember.js/issues/15291
+        let { _super } = this;
+
+        if (
+          opts.topic &&
+          opts.topic.get("topic_key") &&
+          getEncryptionStatus(Discourse.User.current()) !== ENCRYPT_ACTIVE
+        ) {
+          bootbox.alert(I18n.t("encrypt.status_inactive"));
+          return;
+        }
+
+        return _super.call(this, ...arguments);
+      }
+    });
+  }
+};
diff --git a/assets/javascripts/lib/discourse.js.es6 b/assets/javascripts/lib/discourse.js.es6
index 7c14572..eb797d9 100644
--- a/assets/javascripts/lib/discourse.js.es6
+++ b/assets/javascripts/lib/discourse.js.es6
@@ -194,6 +194,38 @@ export function getEncryptionStatus(user) {
 }
 
 /**
+ * Checks if a specific user can enable encryption.
+ *
+ * This check ensures that:
+ *    - user already has encryption enabled OR
+ *    - encryption plug-in is enabled AND
+ *    - there is no group restriction or user is in one of the allowed groups.
+ *
+ * @param user
+ *
+ * @return
+ */
+export function canEnableEncrypt(user) {
+  if (getEncryptionStatus(user) !== ENCRYPT_DISABLED) {
+    return true;
+  }
+
+  if (Discourse.SiteSettings.encrypt_enabled) {
+    if (Discourse.SiteSettings.encrypt_groups.length === 0) {
+      return true;
+    }
+
+    const encryptGroups = Discourse.SiteSettings.encrypt_groups.split("|");
+    const groups = (user.get("groups") || []).map(group => group.get("name"));
+    if (groups.some(group => encryptGroups.includes(group))) {
+      return true;
+    }
+  }
+
+  return false;
+}
+
+/**
  * Reloads current page.
  *
  * This function is usually called when status change so all initializers
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index c346bb5..7baa70d 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -9,6 +9,7 @@ en:
       encrypted_icon_title: "This message is end-to-end encrypted."
 
       encrypted_uploads: "Uploads cannot be encrypted at this time."
+      status_inactive: "You did not activate encryption on this device."
 
       checkbox:
         checked: "This message will be end-to-end encrypted."

GitHub sha: 56e656cd

2 Likes