FEATURE: Add setting to auto enable encryption

FEATURE: Add setting to auto enable encryption

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 d68d3ad..c718f9f 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
@@ -1,4 +1,3 @@
-import { ajax } from "discourse/lib/ajax";
 import showModal from "discourse/lib/show-modal";
 import User from "discourse/models/user";
 import {
@@ -8,17 +7,14 @@ import {
 import {
   activateEncrypt,
   canEnableEncrypt,
+  enableEncrypt,
   ENCRYPT_ACTIVE,
   ENCRYPT_DISABLED,
   getEncryptionStatus
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 import { unpackIdentity } from "discourse/plugins/discourse-encrypt/lib/pack";
-import {
-  exportIdentity,
-  generateIdentity,
-  importIdentity
-} from "discourse/plugins/discourse-encrypt/lib/protocol";
-import { Promise } from "rsvp";
+import { importIdentity } from "discourse/plugins/discourse-encrypt/lib/protocol";
+import I18n from "I18n";
 
 export default {
   setupComponent(args, component) {
@@ -81,24 +77,10 @@ export default {
     enableEncrypt() {
       this.set("inProgress", true);
 
-      const identityPromise = this.importIdentity
-        ? importIdentity(unpackIdentity(this.identity))
-        : generateIdentity();
-
-      const saveIdentityPromise = identityPromise
-        .then(identity => exportIdentity(identity))
-        .then(exported => {
-          this.set("model.encrypt_public", exported.public);
-          return ajax("/encrypt/keys", {
-            type: "PUT",
-            data: {
-              public: exported.public
-            }
-          });
-        });
-
-      const saveDbIdentityPromise = identityPromise
-        .then(identity => saveDbIdentity(identity))
+      return enableEncrypt(this.model, this.importIdentity && this.identity)
+        .then(() => {
+          this.appEvents.trigger("encrypt:status-changed");
+        })
         .finally(() => {
           this.setProperties({
             passphrase: "",
@@ -107,12 +89,6 @@ export default {
             identity: ""
           });
         });
-
-      return Promise.all([saveIdentityPromise, saveDbIdentityPromise]).then(
-        () => {
-          this.appEvents.trigger("encrypt:status-changed");
-        }
-      );
     },
 
     activateEncrypt() {
diff --git a/assets/javascripts/discourse/initializers/auto-enable-encrypt.js.es6 b/assets/javascripts/discourse/initializers/auto-enable-encrypt.js.es6
new file mode 100644
index 0000000..2a83069
--- /dev/null
+++ b/assets/javascripts/discourse/initializers/auto-enable-encrypt.js.es6
@@ -0,0 +1,34 @@
+import {
+  enableEncrypt,
+  ENCRYPT_DISABLED,
+  getEncryptionStatus
+} from "discourse/plugins/discourse-encrypt/lib/discourse";
+
+const AUTO_ENABLE_KEY = "discourse-encrypt-auto-enable";
+
+export default {
+  name: "auto-enable-encrypt",
+
+  initialize(container) {
+    const siteSettings = container.lookup("site-settings:main");
+    if (!siteSettings.auto_enable_encrypt) {
+      return;
+    }
+
+    const currentUser = container.lookup("current-user:main");
+    if (currentUser) {
+      if (
+        !window.localStorage.getItem(AUTO_ENABLE_KEY) &&
+        getEncryptionStatus(currentUser) === ENCRYPT_DISABLED
+      ) {
+        window.localStorage.setItem(AUTO_ENABLE_KEY, true);
+        enableEncrypt(currentUser).then(() => {
+          const appEvents = container.lookup("service:app-events");
+          appEvents.trigger("encrypt:status-changed");
+        });
+      }
+    } else {
+      window.localStorage.removeItem(AUTO_ENABLE_KEY);
+    }
+  }
+};
diff --git a/assets/javascripts/lib/discourse.js.es6 b/assets/javascripts/lib/discourse.js.es6
index 4e85b83..bee11f0 100644
--- a/assets/javascripts/lib/discourse.js.es6
+++ b/assets/javascripts/lib/discourse.js.es6
@@ -6,6 +6,7 @@ import {
   loadDbIdentity,
   saveDbIdentity
 } from "discourse/plugins/discourse-encrypt/lib/database";
+import { unpackIdentity } from "discourse/plugins/discourse-encrypt/lib/pack";
 import {
   decrypt,
   exportIdentity,
@@ -278,6 +279,30 @@ export function canEnableEncrypt(user) {
   return false;
 }
 
+export function enableEncrypt(model, exportedIdentity) {
+  const identityPromise = exportedIdentity
+    ? importIdentity(unpackIdentity(exportedIdentity))
+    : generateIdentity();
+
+  const saveIdentityPromise = identityPromise
+    .then(identity => exportIdentity(identity))
+    .then(exported => {
+      model.set("encrypt_public", exported.public);
+      return ajax("/encrypt/keys", {
+        type: "PUT",
+        data: {
+          public: exported.public
+        }
+      });
+    });
+
+  const saveDbIdentityPromise = identityPromise.then(identity =>
+    saveDbIdentity(identity)
+  );
+
+  return Promise.all([saveIdentityPromise, saveDbIdentityPromise]);
+}
+
 /**
  * Attempts at activating encryption on current device.
  *
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 7614bda..888ac3f 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -6,4 +6,5 @@ en:
     no_encrypt_keys: "Something went wrong. No encryption keys were included in the payload."
   site_settings:
     encrypt_enabled: "Enable encrypted private messages."
+    auto_enable_encrypt: "Automatically enable encrypt for all logged in users"
     encrypt_groups: "The name of groups that are able to use encryption (empty means everyone)."
diff --git a/config/settings.yml b/config/settings.yml
index 7b7345d..839c1ab 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -3,6 +3,10 @@ plugins:
     client: true
     default: true
     refresh: true
+  auto_enable_encrypt:
+    client: true
+    default: false
+    refresh: true
   encrypt_groups:
     default: ""
     client: true

GitHub sha: 9aa388fb