Skip to content

Commit 0f34f15

Browse files
committed
Allow subclasses to match on superclass subject
if Bicycle < Vehicle, and you have a policy `can :read, Vehicle`, then already `can?(:read, Vehicle.new)` and `can?(:read, Bicycle.new)` are both true. `can?(:read, Vehicle)` is also true. I believe `can?(:read, Bicycle)` should also be true, it should respect the subclass. Bicycle is a kind of Vehicle, so if they have been granted permission to read all Vehicles, that applies to all Bicycles too. Closes chaps-io#55, see more there.
1 parent ce45ebb commit 0f34f15

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/access-granted/permission.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def matches_action?(action)
1717
end
1818

1919
def matches_subject?(subject)
20-
subject == @subject || subject.class <= @subject
20+
subject == @subject ||
21+
(subject.is_a?(Class) && @subject.is_a?(Class) && subject <= @subject) ||
22+
subject.class <= @subject
2123
end
2224

2325
def matches_conditions?(subject)

spec/permission_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
expect(perm.matches_subject? String).to eq(true)
5050
end
5151

52+
it "matches if superclass of class object is equal to subject" do
53+
perm = subject.new(true, :read, Exception)
54+
expect(perm.matches_subject? StandardError).to eq(true)
55+
end
56+
5257
it "matches if class is equal to subject" do
5358
perm = subject.new(true, :read, String)
5459
expect(perm.matches_subject? "test").to eq(true)

0 commit comments

Comments
 (0)