Skip to content

Commit

Permalink
Merge pull request #4 from CasbinRuby/fix-methods
Browse files Browse the repository at this point in the history
Fix remove_filtered_policy, filtered_rule and  join_slice methods
  • Loading branch information
akirill0v authored May 21, 2021
2 parents 839ae7f + 5cf410e commit ebe4baa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
9 changes: 3 additions & 6 deletions lib/casbin-ruby/model/policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def remove_policies(sec, ptype, rules)

# removes policy rules based on field filters from the model.
def remove_filtered_policy(sec, ptype, field_index, *field_values)
return false unless model.key?(sec) && model[sec].include?(ptype)
return false unless model.key?(sec)
return false unless model[sec].include?(ptype)

state = { tmp: [], res: false }
model[sec][ptype].policy.each do |rule|
Expand Down Expand Up @@ -142,12 +143,8 @@ def get_values_for_field_in_policy(sec, ptype, field_index)
private

def filtered_rule(state, rule, field_values, field_index)
matched = true

field_values.each_with_index do |field_value, index|
next matched = false if field_value != '' && field_value != rule[field_index + index]

if matched
if field_value == '' || rule[field_index + index] == field_value
state[:res] = true
else
state[:tmp] << rule
Expand Down
2 changes: 1 addition & 1 deletion lib/casbin-ruby/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_eval_value(string)

# joins a string and a slice into a new slice.
def join_slice(a, *b)
Array.new(a).concat b
Array(a).concat b
end

# returns the elements in `a` that aren't in `b`.
Expand Down
2 changes: 1 addition & 1 deletion lib/casbin-ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Casbin
VERSION = '1.0.4'
VERSION = '1.0.5'
end
10 changes: 10 additions & 0 deletions spec/casbin/enforcer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
enf.load_model
expect(enf.model).not_to be_nil
end

it '#remove_filtered_policy' do
expect(enf.enforce('alice', 'data1', 'read')).to be_truthy
enf.remove_filtered_policy(1, 'data1')
expect(enf.enforce('alice', 'data1', 'read')).to be_falsey

expect(enf.enforce('bob', 'data2', 'write')).to be_truthy
enf.remove_filtered_policy(2, 'write')
expect(enf.enforce('bob', 'data2', 'write')).to be_falsey
end
end

describe 'basic without spaces' do
Expand Down

0 comments on commit ebe4baa

Please sign in to comment.