FIX: moderators can't see private topics that they aren't invited to see.

This commit is contained in:
Neil Lalonde 2014-05-12 15:26:36 -04:00
parent 447cfa8dfe
commit e68e97d986
6 changed files with 33 additions and 13 deletions

View File

@ -96,9 +96,15 @@ class PostAction < ActiveRecord::Base
return unless opts[:message] && [:notify_moderators, :notify_user].include?(post_action_type)
# this is a hack to allow a PM with no reciepients, we should think through
# a cleaner technique, a PM with myself is valid for flagging
target_usernames = post_action_type == :notify_user ? post.user.username : "x"
target_usernames = if post_action_type == :notify_user
post.user.username
elsif post_action_type == :notify_moderators
User.moderators.pluck(:username)
else
# this is a hack to allow a PM with no reciepients, we should think through
# a cleaner technique, a PM with myself is valid for flagging
'x'
end
title = I18n.t("post_action_types.#{post_action_type}.email_title",
title: post.topic.title)

View File

@ -46,7 +46,7 @@ module TopicGuardian
def can_see_topic?(topic)
return false unless topic
return true if is_staff?
return true if is_admin?
return false if topic.deleted_at
# NOTE
@ -56,7 +56,7 @@ module TopicGuardian
# not secure, or I can see it
(not(topic.read_restricted_category?) || can_see_category?(topic.category)) &&
# not private, or I am allowed (or is staff)
(not(topic.private_message?) || (authenticated? && (is_staff? || topic.all_allowed_users.where(id: @user.id).exists?)))
(not(topic.private_message?) || (authenticated? && (is_admin? || topic.all_allowed_users.where(id: @user.id).exists?)))
end
end

View File

@ -301,6 +301,15 @@ describe Guardian do
Guardian.new(user).can_see?(topic).should be_true
end
it "restricts private topics" do
user.save!
private_topic = Fabricate(:private_message_topic, user: user)
Guardian.new(private_topic.user).can_see?(private_topic).should be_true
Guardian.new(build(:user)).can_see?(private_topic).should be_false
Guardian.new(moderator).can_see?(private_topic).should be_false
Guardian.new(admin).can_see?(private_topic).should be_true
end
end
describe 'a Post' do

View File

@ -338,13 +338,13 @@ describe PostCreator do
unrelated.notifications.count.should == 0
post.topic.subtype.should == TopicSubtype.user_to_user
# if a mod replies they should be added to the allowed user list
mod = Fabricate(:moderator)
PostCreator.create(mod, raw: 'hi there welcome topic, I am a mod',
# if an admin replies they should be added to the allowed user list
admin = Fabricate(:admin)
PostCreator.create(admin, raw: 'hi there welcome topic, I am a mod',
topic_id: post.topic_id)
post.topic.reload
post.topic.topic_allowed_users.where(user_id: mod.id).count.should == 1
post.topic.topic_allowed_users.where(user_id: admin.id).count.should == 1
end
end

View File

@ -19,10 +19,10 @@ describe TopicView do
end
it "handles deleted topics" do
topic.trash!(coding_horror)
lambda { TopicView.new(topic.id, coding_horror) }.should raise_error(Discourse::NotFound)
coding_horror.stubs(:staff?).returns(true)
lambda { TopicView.new(topic.id, coding_horror) }.should_not raise_error
admin = Fabricate(:admin)
topic.trash!(admin)
lambda { TopicView.new(topic.id, Fabricate(:user)) }.should raise_error(Discourse::NotFound)
lambda { TopicView.new(topic.id, admin) }.should_not raise_error
end

View File

@ -29,6 +29,11 @@ describe PostAction do
action.related_post_id.should == posts[0].id.to_i
posts[0].subtype.should == TopicSubtype.notify_moderators
# Moderators should be invited to the private topic, otherwise they're not permitted to see it
topic_user_ids = posts[0].topic.topic_users.map {|x| x.user_id}
topic_user_ids.should include(codinghorror.id)
topic_user_ids.should include(mod.id)
# reply to PM should clear flag
p = PostCreator.new(mod, topic_id: posts[0].topic_id, raw: "This is my test reply to the user, it should clear flags")
p.create