Skip to content

Commit

Permalink
ensure sort works consistently across platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
khash committed Jan 10, 2024
1 parent fdf1524 commit e4fe78c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
12 changes: 6 additions & 6 deletions lib/roleback/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def conflicts_with?(rule)
# scope_weight = 100
# resource_weight = 10
# outcome_weight = 1
# scope_value = 0 if scope == ANY, 1 otherwise
# resource_value = 0 if resource == ANY, 1 otherwise
# outcome_value = 0 if outcome == ALLOW, 1 otherwise
# scope_value = 1 if scope == ANY, 2 otherwise
# resource_value = 1 if resource == ANY, 2 otherwise
# outcome_value = 1 if outcome == ALLOW, 2 otherwise
def numerical_value
scope_value = @scope == ::Roleback::ANY ? 0 : 1
resource_value = @resource == ::Roleback::ANY ? 0 : 1
outcome_value = @outcome == ::Roleback::ALLOW ? 0 : 1
scope_value = @scope == ::Roleback::ANY ? 1 : 2
resource_value = @resource == ::Roleback::ANY ? 1 : 2
outcome_value = @outcome == ::Roleback::ALLOW ? 1 : 2

(scope_value * 100) + (resource_value * 10) + (outcome_value * 1)
end
Expand Down
10 changes: 9 additions & 1 deletion lib/roleback/rule_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ def self.sort(rules)

# sort the rules
rules.sort! do |a, b|
b.numerical_value <=> a.numerical_value
an = a.numerical_value
bn = b.numerical_value

# if the numerical values are the same, sort by key, otherwise sort by numerical value
if an == bn
a.key <=> b.key
else
bn <=> an
end
end
end

Expand Down
32 changes: 16 additions & 16 deletions spec/rule_book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
rule1 = ::Roleback::Rule.new(role: role1, resource: resource, scope: scope, action: :show, outcome: ::Roleback::DENY)

# any allow
rule2 = ::Roleback::Rule.new(role: role1, resource: ::Roleback::ANY, scope: ::Roleback::ANY, action: :show, outcome: ::Roleback::ALLOW)
rule2 = ::Roleback::Rule.new(role: role1, resource: ::Roleback.any, scope: ::Roleback::any, action: :show, outcome: ::Roleback::ALLOW)

rule_book1.add(rule1)
rule_book1.add(rule2)
Expand Down Expand Up @@ -201,23 +201,23 @@

expect(rules.length).to eq(18)

# make sure the rules are sorted correctly based on the following list
# make sure the rules are sorted correctly based on the following values:
expect(sorted_rules[0].to_s).to eq('api:/users/work->deny')
expect(sorted_rules[1].to_s).to eq('api:/users/rest->allow')
expect(sorted_rules[2].to_s).to eq('api:/users/create->allow')
expect(sorted_rules[3].to_s).to eq('api:/users/show->allow')
expect(sorted_rules[4].to_s).to eq('api:/users/update->allow')
expect(sorted_rules[5].to_s).to eq('api:/users/delete->allow')
expect(sorted_rules[6].to_s).to eq('api:/users/index->allow')
expect(sorted_rules[7].to_s).to eq('api:/users/new->allow')
expect(sorted_rules[8].to_s).to eq('api:/users/edit->allow')
expect(sorted_rules[1].to_s).to eq('api:/users/create->allow')
expect(sorted_rules[2].to_s).to eq('api:/users/delete->allow')
expect(sorted_rules[3].to_s).to eq('api:/users/edit->allow')
expect(sorted_rules[4].to_s).to eq('api:/users/index->allow')
expect(sorted_rules[5].to_s).to eq('api:/users/new->allow')
expect(sorted_rules[6].to_s).to eq('api:/users/rest->allow')
expect(sorted_rules[7].to_s).to eq('api:/users/show->allow')
expect(sorted_rules[8].to_s).to eq('api:/users/update->allow')
expect(sorted_rules[9].to_s).to eq('*:/users/create->allow')
expect(sorted_rules[10].to_s).to eq('*:/users/show->allow')
expect(sorted_rules[11].to_s).to eq('*:/users/update->allow')
expect(sorted_rules[12].to_s).to eq('*:/users/delete->allow')
expect(sorted_rules[13].to_s).to eq('*:/users/index->allow')
expect(sorted_rules[14].to_s).to eq('*:/users/new->allow')
expect(sorted_rules[15].to_s).to eq('*:/users/edit->allow')
expect(sorted_rules[10].to_s).to eq('*:/users/delete->allow')
expect(sorted_rules[11].to_s).to eq('*:/users/edit->allow')
expect(sorted_rules[12].to_s).to eq('*:/users/index->allow')
expect(sorted_rules[13].to_s).to eq('*:/users/new->allow')
expect(sorted_rules[14].to_s).to eq('*:/users/show->allow')
expect(sorted_rules[15].to_s).to eq('*:/users/update->allow')
expect(sorted_rules[16].to_s).to eq('*:/users/work->allow')
expect(sorted_rules[17].to_s).to eq('*:/*/see->allow')
end
Expand Down

0 comments on commit e4fe78c

Please sign in to comment.