DEV: Various code improvements (#125)

DEV: Various code improvements (#125)

  • Isolate extensions in DiscourseEncrypt namespace
  • Move permission check to server side
  • Other minor coding style improvements
diff --git a/app/controllers/encrypt_controller.rb b/app/controllers/encrypt_controller.rb
index 2643b3f..bd3f7f4 100644
--- a/app/controllers/encrypt_controller.rb
+++ b/app/controllers/encrypt_controller.rb
@@ -4,8 +4,7 @@ class DiscourseEncrypt::EncryptController < ApplicationController
   requires_plugin DiscourseEncrypt::PLUGIN_NAME
 
   before_action :ensure_logged_in
-  before_action :ensure_encrypt_enabled
-  skip_before_action :check_xhr
+  before_action :ensure_can_encrypt
 
   # Saves a user's identity in their custom fields.
   #
@@ -236,12 +235,7 @@ class DiscourseEncrypt::EncryptController < ApplicationController
 
   private
 
-  def ensure_encrypt_enabled
-    groups = current_user.groups.pluck(:name).map(&:downcase)
-    encrypt_groups = SiteSetting.encrypt_groups.split('|').map(&:downcase)
-
-    if !SiteSetting.encrypt_groups.empty? && (groups & encrypt_groups).empty?
-      raise Discourse::InvalidAccess
-    end
+  def ensure_can_encrypt
+    current_user.guardian.ensure_can_encrypt!
   end
 end
diff --git a/app/controllers/encrypted_post_timers_controller.rb b/app/controllers/encrypted_post_timers_controller.rb
index b2fc6b7..65a2743 100644
--- a/app/controllers/encrypted_post_timers_controller.rb
+++ b/app/controllers/encrypted_post_timers_controller.rb
@@ -1,6 +1,11 @@
 # frozen_string_literal: true
 
 class DiscourseEncrypt::EncryptedPostTimersController < ApplicationController
+  requires_plugin DiscourseEncrypt::PLUGIN_NAME
+
+  before_action :ensure_logged_in
+  before_action :ensure_can_encrypt
+
   def create
     delete_at = 1.minutes.from_now
     Array.wrap(params[:post_id]).each do |post_id|
@@ -31,4 +36,8 @@ class DiscourseEncrypt::EncryptedPostTimersController < ApplicationController
 
     EncryptedPostTimer.create!(post: post, delete_at: delete_at)
   end
+
+  def ensure_can_encrypt
+    current_user.guardian.ensure_can_encrypt!
+  end
 end
diff --git a/app/mailers/user_notifications_extensions.rb b/app/mailers/user_notifications_extensions.rb
index 2e450a4..2bcbd43 100644
--- a/app/mailers/user_notifications_extensions.rb
+++ b/app/mailers/user_notifications_extensions.rb
@@ -1,6 +1,6 @@
 # frozen_string_literal: true
 
-module UserNotificationsExtensions
+module DiscourseEncrypt::UserNotificationsExtensions
   def notification_email(user, opts)
     if opts[:post] && opts[:post].is_encrypted?
       opts[:allow_reply_by_email] = false
diff --git a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js
index 76f80c5..4b533e2 100644
--- a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js
+++ b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js
@@ -8,8 +8,8 @@ import I18n from "I18n";
 
 export default {
   setupComponent(args, component) {
-    const { currentUser, siteSettings } = component;
-    const status = getEncryptionStatus(currentUser, siteSettings);
+    const { currentUser } = component;
+    const status = getEncryptionStatus(currentUser);
 
     component.setProperties({
       isEncryptEnabled: status !== ENCRYPT_DISABLED,
@@ -17,7 +17,7 @@ export default {
 
       /** Listens for encryption status updates. */
       listener() {
-        const newStatus = getEncryptionStatus(currentUser, siteSettings);
+        const newStatus = getEncryptionStatus(currentUser);
         component.setProperties({
           isEncryptEnabled: newStatus !== ENCRYPT_DISABLED,
           isEncryptActive: newStatus === ENCRYPT_ACTIVE,
diff --git a/assets/javascripts/discourse/connectors/user-preferences-security/encrypt.js b/assets/javascripts/discourse/connectors/user-preferences-security/encrypt.js
index 217b5b3..7eb96fd 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-security/encrypt.js
+++ b/assets/javascripts/discourse/connectors/user-preferences-security/encrypt.js
@@ -7,7 +7,6 @@ import {
   ENCRYPT_ACTIVE,
   ENCRYPT_DISABLED,
   activateEncrypt,
-  canEnableEncrypt,
   enableEncrypt,
   getEncryptionStatus,
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
@@ -20,7 +19,7 @@ import I18n from "I18n";
 
 export default {
   setupComponent(args, component) {
-    const { currentUser, siteSettings } = component;
+    const { currentUser } = component;
     const isCurrentUser = args.model.id === currentUser.id;
 
     component.setProperties({
@@ -31,13 +30,13 @@ export default {
       /** Whether current user is the same as model user. */
       isCurrentUser,
       /** Whether plugin is enabled for current user. */
-      canEnableEncrypt: canEnableEncrypt(args.model, siteSettings),
+      canEnableEncrypt: args.model.can_encrypt,
       /** Whether the encryption is enabled or not. */
       isEncryptEnabled: !!args.model.encrypt_public,
     });
 
     if (isCurrentUser) {
-      const status = getEncryptionStatus(args.model, siteSettings);
+      const status = getEncryptionStatus(args.model);
       component.setProperties({
         /** Value of passphrase input.
          *  It should stay in memory for as little time as possible.
@@ -58,7 +57,7 @@ export default {
         isEncryptActive: status === ENCRYPT_ACTIVE,
         /** Listens for encryption status updates. */
         listener() {
-          const newStatus = getEncryptionStatus(args.model, siteSettings);
+          const newStatus = getEncryptionStatus(args.model);
           component.setProperties({
             isEncryptEnabled: newStatus !== ENCRYPT_DISABLED,
             isEncryptActive: newStatus === ENCRYPT_ACTIVE,
diff --git a/assets/javascripts/discourse/initializers/add-search-results.js b/assets/javascripts/discourse/initializers/add-search-results.js
index 3550901..8c7f6df 100644
--- a/assets/javascripts/discourse/initializers/add-search-results.js
+++ b/assets/javascripts/discourse/initializers/add-search-results.js
@@ -69,8 +69,7 @@ export default {
 
   initialize(container) {
     const currentUser = container.lookup("current-user:main");
-    const siteSettings = container.lookup("site-settings:main");
-    if (getEncryptionStatus(currentUser, siteSettings) !== ENCRYPT_ACTIVE) {
+    if (getEncryptionStatus(currentUser) !== ENCRYPT_ACTIVE) {
       return;
     }
 
diff --git a/assets/javascripts/discourse/initializers/auto-enable-encrypt.js b/assets/javascripts/discourse/initializers/auto-enable-encrypt.js
index dc00682..46772cc 100644
--- a/assets/javascripts/discourse/initializers/auto-enable-encrypt.js
+++ b/assets/javascripts/discourse/initializers/auto-enable-encrypt.js
@@ -19,7 +19,7 @@ export default {
     if (currentUser) {
       if (
         !window.localStorage.getItem(AUTO_ENABLE_KEY) &&
-        getEncryptionStatus(currentUser, siteSettings) === ENCRYPT_DISABLED
+        getEncryptionStatus(currentUser) === ENCRYPT_DISABLED
       ) {
         window.localStorage.setItem(AUTO_ENABLE_KEY, true);
         enableEncrypt(currentUser).then(() => {
diff --git a/assets/javascripts/discourse/initializers/decrypt-post-revision.js b/assets/javascripts/discourse/initializers/decrypt-post-revision.js
index 09b6fa0..6971336 100644
--- a/assets/javascripts/discourse/initializers/decrypt-post-revision.js
+++ b/assets/javascripts/discourse/initializers/decrypt-post-revision.js
@@ -14,8 +14,7 @@ export default {
 
   initialize(container) {
     const currentUser = container.lookup("current-user:main");
-    const siteSettings = container.lookup("site-settings:main");
-    if (getEncryptionStatus(currentUser, siteSettings) !== ENCRYPT_ACTIVE) {
+    if (getEncryptionStatus(currentUser) !== ENCRYPT_ACTIVE) {
       return;
     }
 
diff --git a/assets/javascripts/discourse/initializers/decrypt-posts.js b/assets/javascripts/discourse/initializers/decrypt-posts.js
index 72447e3..78e2051 100644
--- a/assets/javascripts/discourse/initializers/decrypt-posts.js
+++ b/assets/javascripts/discourse/initializers/decrypt-posts.js
@@ -260,7 +260,7 @@ export default {

[... diff too long, it was truncated ...]

GitHub sha: 0dec48077f00d1b1368d60d4a8eca153258c1ade

This commit appears in #125 which was approved by eviltrout. It was merged by udan11.