Merge pull request #2507 from techAPJ/invite-patch-1

REFACTOR: move all conditions to guardian
This commit is contained in:
Robin Ward 2014-07-04 13:52:57 -04:00
commit a64b954fca
3 changed files with 16 additions and 5 deletions

View File

@ -22,7 +22,7 @@ export default Discourse.ContainerView.extend({
if (Discourse.User.current()) { if (Discourse.User.current()) {
if (!topic.get('isPrivateMessage')) { if (!topic.get('isPrivateMessage')) {
// We hide some controls from private messages // We hide some controls from private messages
if (this.get('topic.details.can_invite_to') && (!this.get('topic.category.read_restricted') || Discourse.User.currentProp('admin'))) { if (this.get('topic.details.can_invite_to')) {
this.attachViewClass(InviteReplyButton); this.attachViewClass(InviteReplyButton);
} }
this.attachViewClass(StarButton); this.attachViewClass(StarButton);

View File

@ -200,9 +200,9 @@ class Guardian
end end
def can_invite_to?(object, group_ids=nil) def can_invite_to?(object, group_ids=nil)
can_see?(object) && can_invite = can_see?(object) && can_invite_to_forum? && ( group_ids.blank? || is_admin? )
can_invite_to_forum? && can_invite = can_invite && ( !object.category.read_restricted || is_admin? ) if object.is_a?(Topic)
( group_ids.blank? || is_admin? ) can_invite
end end
def can_bulk_invite_to_forum?(user) def can_bulk_invite_to_forum?(user)

View File

@ -241,9 +241,13 @@ describe Guardian do
end end
describe 'can_invite_to?' do describe 'can_invite_to?' do
let(:group) { Fabricate(:group) }
let(:category) { Fabricate(:category, read_restricted: true) }
let(:topic) { Fabricate(:topic) } let(:topic) { Fabricate(:topic) }
let(:private_topic) { Fabricate(:topic, category: category) }
let(:user) { topic.user } let(:user) { topic.user }
let(:moderator) { Fabricate(:moderator) } let(:moderator) { Fabricate(:moderator) }
let(:admin) { Fabricate(:admin) }
it 'handles invitation correctly' do it 'handles invitation correctly' do
Guardian.new(nil).can_invite_to?(topic).should be_false Guardian.new(nil).can_invite_to?(topic).should be_false
@ -268,6 +272,14 @@ describe Guardian do
Guardian.new(user).can_invite_to?(topic).should be_false Guardian.new(user).can_invite_to?(topic).should be_false
end end
it 'returns false for normal user on private topic' do
Guardian.new(user).can_invite_to?(private_topic).should be_false
end
it 'returns true for admin on private topic' do
Guardian.new(admin).can_invite_to?(private_topic).should be_true
end
end end
describe 'can_see?' do describe 'can_see?' do
@ -1757,4 +1769,3 @@ describe Guardian do
end end
end end
end end