FIX: even admin does not see/like encrypted posts (#20)

FIX: even admin does not see/like encrypted posts (#20)

Even admin should not see private and encrypted messaged.

Of course, admin can not decrypt messages so they are safe but right now they can like without seeing the content.

diff --git a/lib/post_actions_controller_extensions.rb b/lib/post_actions_controller_extensions.rb
new file mode 100644
index 0000000..531f41a
--- /dev/null
+++ b/lib/post_actions_controller_extensions.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+module PostActionsControllerExtensions
+  def create
+    if SiteSetting.encrypt_enabled?
+      raise Discourse::NotFound if @post.blank?
+      raise Discourse::InvalidAccess if !guardian.is_user_a_member_of_encrypted_conversation?(@post.topic)
+    end
+    super
+  end
+end
diff --git a/lib/topics_controller_extensions.rb b/lib/topics_controller_extensions.rb
index a5e282d..b4d56bc 100644
--- a/lib/topics_controller_extensions.rb
+++ b/lib/topics_controller_extensions.rb
@@ -1,6 +1,14 @@
 # frozen_string_literal: true
 
 module TopicsControllerExtensions
+  def show
+    if SiteSetting.encrypt_enabled?
+      topic = Topic.find_by(id: params[:topic_id])
+      raise Discourse::InvalidAccess if !guardian.is_user_a_member_of_encrypted_conversation?(topic)
+    end
+    super
+  end
+
   def update
     @topic ||= Topic.find_by(id: params[:topic_id])
 
diff --git a/plugin.rb b/plugin.rb
index 3a73a0b..56e92af 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -29,6 +29,7 @@ after_initialize do
   load File.expand_path('../lib/post_extensions.rb', __FILE__)
   load File.expand_path('../lib/topic_extensions.rb', __FILE__)
   load File.expand_path('../lib/topics_controller_extensions.rb', __FILE__)
+  load File.expand_path('../lib/post_actions_controller_extensions.rb', __FILE__)
   load File.expand_path('../lib/user_extensions.rb', __FILE__)
   load File.expand_path('../lib/email_sender_extensions.rb', __FILE__)
   load File.expand_path('../app/mailers/user_notifications_extensions.rb', __FILE__)
@@ -51,12 +52,13 @@ after_initialize do
   end
 
   reloadable_patch do |plugin|
-    Post.class_eval              { prepend PostExtensions }
-    Topic.class_eval             { prepend TopicExtensions }
-    TopicsController.class_eval  { prepend TopicsControllerExtensions }
-    User.class_eval              { prepend UserExtensions }
-    Email::Sender.class_eval     { prepend EmailSenderExtensions }
-    UserNotifications.class_eval { prepend UserNotificationsExtensions }
+    Post.class_eval                  { prepend PostExtensions }
+    Topic.class_eval                 { prepend TopicExtensions }
+    TopicsController.class_eval      { prepend TopicsControllerExtensions }
+    PostActionsController.class_eval { prepend PostActionsControllerExtensions }
+    User.class_eval                  { prepend UserExtensions }
+    Email::Sender.class_eval         { prepend EmailSenderExtensions }
+    UserNotifications.class_eval     { prepend UserNotificationsExtensions }
   end
 
   # Send plugin-specific topic data to client via serializers.
@@ -162,6 +164,15 @@ after_initialize do
     object.user_encryption_key&.encrypt_private
   end
 
+  add_to_class(:guardian, :is_user_a_member_of_encrypted_conversation?) do |topic|
+    return false unless topic
+    if SiteSetting.encrypt_enabled? && topic.is_encrypted?
+      authenticated? && topic.all_allowed_users.where(id: @user.id).exists?
+    else
+      true
+    end
+  end
+
   #
   # Hide cooked content.
   #
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index c30350a..dd9633d 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -6,9 +6,12 @@ describe TopicsController do
   let(:topic) { Fabricate(:encrypt_topic) }
   let(:user) { Fabricate(:user) }
   let(:group) { Fabricate(:group) }
+  let(:admin) { Fabricate(:admin) }
+  let(:admin2) { Fabricate(:admin) }
 
   before do
-    sign_in(Fabricate(:admin))
+    TopicAllowedUser.create!(user_id: admin.id, topic_id: topic.id)
+    sign_in(admin)
   end
 
   context '#update' do
@@ -20,6 +23,16 @@ describe TopicsController do
     end
   end
 
+  it 'not invited admin does not have access' do
+    sign_in(admin2)
+    get "/t/#{topic.slug}/#{topic.id}.json"
+    expect(response.status).to eq(403)
+
+    TopicAllowedUser.create!(user_id: admin2.id, topic_id: topic.id)
+    get "/t/#{topic.slug}/#{topic.id}.json"
+    expect(response.status).to eq(200)
+  end
+
   context '#invite' do
     it 'saves user key' do
       post "/t/#{topic.id}/invite.json", params: { user: user.username, key: 'key of user' }

GitHub sha: e414c993

This commit appears in #20 which was approved by udan11. It was merged by lis2.