From ec57ca54b596ba1a745c0a987847a1462f0b5a95 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 19 Mar 2018 14:12:01 +0800 Subject: [PATCH] FEATURE: Admins should be able to view PMs of any group. --- lib/topic_query.rb | 20 ++++++++++---- spec/components/topic_query_spec.rb | 41 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 35311ba0a85..5557fd6d6de 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -434,11 +434,21 @@ class TopicQuery if type == :group result = result.includes(:allowed_users) - result = result.where("topics.id IN (SELECT topic_id FROM topic_allowed_groups - WHERE group_id IN ( - SELECT group_id FROM group_users WHERE user_id = #{user.id.to_i}) AND - group_id IN (SELECT id FROM groups WHERE name ilike ?) - )", @options[:group_name]) + result = result.where(" + topics.id IN ( + SELECT topic_id FROM topic_allowed_groups + WHERE ( + group_id IN ( + SELECT group_id + FROM group_users + WHERE user_id = #{user.id.to_i} + OR #{user.staff?} + ) + ) + AND group_id IN (SELECT id FROM groups WHERE name ilike ?) + )", + @options[:group_name] + ) elsif type == :user result = result.includes(:allowed_users) result = result.where("topics.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = #{user.id.to_i})") diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb index eee0192f899..989348f4c54 100644 --- a/spec/components/topic_query_spec.rb +++ b/spec/components/topic_query_spec.rb @@ -940,4 +940,45 @@ describe TopicQuery do expect(topics).to contain_exactly(topic1, topic2, topic6) end end + + describe '#list_private_messages_group' do + let(:group) { Fabricate(:group) } + + let!(:group_message) do + Fabricate(:private_message_topic, + allowed_groups: [group], + topic_allowed_users: [ + Fabricate.build(:topic_allowed_user, user: Fabricate(:user)), + ] + ) + end + + before do + group.add(creator) + end + + it 'should return the right list for a group user' do + topics = TopicQuery.new(nil, group_name: group.name) + .list_private_messages_group(creator) + .topics + + expect(topics).to contain_exactly(group_message) + end + + it 'should return the right list for an admin not part of the group' do + topics = TopicQuery.new(nil, group_name: group.name) + .list_private_messages_group(Fabricate(:admin)) + .topics + + expect(topics).to contain_exactly(group_message) + end + + it 'should return the right list for a user not part of the group' do + topics = TopicQuery.new(nil, group_name: group.name) + .list_private_messages_group(Fabricate(:user)) + .topics + + expect(topics).to eq([]) + end + end end