Don't allow category definition topics to be converted to PMs (#5216)

This commit is contained in:
OsamaSayegh 2017-10-02 11:04:58 +03:00 committed by Régis Hanol
parent b5bbb8ae8a
commit 79f3d299a1
4 changed files with 10 additions and 1 deletions

View File

@ -177,7 +177,7 @@ export default createWidget('topic-admin-menu', {
icon: visible ? 'eye-slash' : 'eye',
label: visible ? 'actions.invisible' : 'actions.visible' });
if (this.currentUser.get('staff')) {
if (details.get('can_convert_topic')) {
buttons.push({ className: 'topic-admin-convert',
action: isPrivateMessage ? 'convertToPublicTopic' : 'convertToPrivateMessage',
icon: isPrivateMessage ? 'comment' : 'envelope',

View File

@ -118,6 +118,7 @@ class TopicViewSerializer < ApplicationSerializer
result[:can_create_post] = true if scope.can_create?(Post, object.topic)
result[:can_reply_as_new_topic] = true if scope.can_reply_as_new_topic?(object.topic)
result[:can_flag_topic] = actions_summary.any? { |a| a[:can_act] }
result[:can_convert_topic] = true if scope.can_convert_topic?(object.topic)
result
end

View File

@ -63,7 +63,9 @@ module TopicGuardian
end
def can_convert_topic?(topic)
return false if topic.blank?
return false if topic && topic.trashed?
return false if Category.where("topic_id = ?", topic.id).exists?
return true if is_admin?
is_moderator? && can_create_post?(topic)
end

View File

@ -996,6 +996,12 @@ describe Guardian do
expect(Guardian.new(trust_level_4).can_convert_topic?(topic)).to be_falsey
end
it 'returns false for category definition topics' do
c = Fabricate(:category)
topic = Topic.find_by(id: c.topic_id)
expect(Guardian.new(admin).can_convert_topic?(topic)).to be_falsey
end
it 'returns true when a moderator' do
expect(Guardian.new(moderator).can_convert_topic?(topic)).to be_truthy
end