Skip to content

Commit

Permalink
Fix permission calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
swarley committed Nov 15, 2020
1 parent 32ba2b8 commit 9cc3a26
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/discordrb/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,16 @@ def defined_role_permission?(action, channel)
roles_to_check.sort_by(&:position).reduce(false) do |can_act, role|
# Get the override defined for the role on the channel
channel_allow = permission_overwrite(action, channel, role.id)
can_act = if channel_allow
# If the channel has an override, check whether it is an allow - if yes,
# the user can act, if not, it can't
channel_allow == :allow
else
# Otherwise defer to the role
role.permissions.instance_variable_get("@#{action}") || can_act
end
can_act
if channel_allow
# If the channel has an override, check whether it is an allow - if yes,
# the user can act, if not, it can't
break true if channel_allow == :allow

false
else
# Otherwise defer to the role
role.permissions.instance_variable_get("@#{action}") || can_act
end
end
end

Expand Down
26 changes: 26 additions & 0 deletions spec/permissions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,31 @@ class ExampleCalculator
permission = subject.__send__(:defined_role_permission?, :manage_messages, channel)
expect(permission).to eq true
end

it 'takes overwrites into account' do
everyone_role = double('everyone role', id: 0, position: 0, permissions: Discordrb::Permissions.new)
role_a = double('role a', id: 1, position: 1, permissions: Discordrb::Permissions.new([:manage_messages]))
role_b = double('role b', id: 2, position: 2, permissions: Discordrb::Permissions.new)
channel = double('channel')

subject.server = double('server', everyone_role: everyone_role)
subject.roles = [role_a, role_b]

allow(subject).to receive(:permission_overwrite).and_return(nil)

allow(subject).to receive(:permission_overwrite)
.with(:manage_messages, channel, role_a.id)
.and_return(:deny)

allow(subject).to receive(:permission_overwrite)
.with(:manage_messages, channel, role_b.id)
.and_return(:allow)

subject.roles = [role_a]
expect(subject.__send__(:defined_role_permission?, :manage_messages, channel)).to be false

subject.roles = [role_a, role_b]
expect(subject.__send__(:defined_role_permission?, :manage_messages, channel)).to be true
end
end
end

0 comments on commit 9cc3a26

Please sign in to comment.