Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress duplicates rules #843

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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