REFACTOR: move all conditions to guardian

This commit is contained in:
Arpit Jalan 2014-07-04 23:04:19 +05:30
parent abf6ffcaa0
commit 48f86181bf
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 (!topic.get('isPrivateMessage')) {
// 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(StarButton);

View file

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

View file

@ -241,9 +241,13 @@ describe Guardian do
end
describe 'can_invite_to?' do
let(:group) { Fabricate(:group) }
let(:category) { Fabricate(:category, read_restricted: true) }
let(:topic) { Fabricate(:topic) }
let(:private_topic) { Fabricate(:topic, category: category) }
let(:user) { topic.user }
let(:moderator) { Fabricate(:moderator) }
let(:admin) { Fabricate(:admin) }
it 'handles invitation correctly' do
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
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
describe 'can_see?' do
@ -1757,4 +1769,3 @@ describe Guardian do
end
end
end