Skip to content

Commit

Permalink
Compress duplicates rules (#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrChoclate authored May 23, 2024
1 parent 178d2c5 commit 2f4c3cf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
18 changes: 18 additions & 0 deletions lib/cancan/rules_compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(rules)
end

def compress(array)
array = simplify(array)
idx = array.rindex(&:catch_all?)
return array unless idx

Expand All @@ -19,5 +20,22 @@ def compress(array)
.drop_while { |n| n.base_behavior == value.base_behavior }
.tap { |a| a.unshift(value) unless value.cannot_catch_all? }
end

# If we have A OR (!A AND anything ), then we can simplify to A OR anything
# If we have A OR (A OR anything ), then we can simplify to A OR anything
# If we have !A AND (A OR something), then we can simplify it to !A AND something
# If we have !A AND (!A AND something), then we can simplify it to !A AND something
#
# So as soon as we see a condition that is the same as the previous one,
# we can skip it, no matter of the base_behavior
def simplify(rules)
seen = Set.new
rules.reverse_each.filter_map do |rule|
next if seen.include?(rule.conditions)

seen.add(rule.conditions)
rule
end.reverse
end
end
end
40 changes: 37 additions & 3 deletions spec/cancan/rule_compressor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ def cannot(action, subject, args = nil)
end
end

# TODO: not supported yet
xcontext 'duplicate rules' do
context 'duplicate rules' do
let(:rules) do
[can(:read, Blog, id: 4),
can(:read, Blog, id: 1),
Expand All @@ -99,7 +98,42 @@ def cannot(action, subject, args = nil)
end

it 'minimizes the rules, by removing duplicates' do
expect(described_class.new(rules).rules_collapsed).to eq [rules[0], rules[1], rules[2], rules[4]]
expect(described_class.new(rules).rules_collapsed).to eq [rules[0], rules[1], rules[4], rules[5]]
end
end

context 'duplicates rules with cannot' do
let(:rules) do
[can(:read, Blog, id: 1),
cannot(:read, Blog, id: 1)]
end

it 'minimizes the rules, by removing useless previous rules' do
expect(described_class.new(rules).rules_collapsed).to eq [rules[1]]
end
end

context 'duplicates rules with cannot and can again' do
let(:rules) do
[can(:read, Blog, id: [1, 2]),
cannot(:read, Blog, id: 1),
can(:read, Blog, id: 1)]
end

it 'minimizes the rules, by removing useless previous rules' do
expect(described_class.new(rules).rules_collapsed).to eq [rules[0], rules[2]]
end
end

context 'duplicates rules with 2 cannot' do
let(:rules) do
[can(:read, Blog),
cannot(:read, Blog, id: 1),
cannot(:read, Blog, id: 1)]
end

it 'minimizes the rules, by removing useless previous rules' do
expect(described_class.new(rules).rules_collapsed).to eq [rules[0], rules[2]]
end
end

Expand Down

0 comments on commit 2f4c3cf

Please sign in to comment.