From dc1ffffc0e3c95015f0a551daf5d8e81e12b36f9 Mon Sep 17 00:00:00 2001 From: Jokius Date: Thu, 20 May 2021 18:19:45 +0300 Subject: [PATCH 1/2] Fix remove_filtered_policy, filtered_rule and join_slice methods --- lib/casbin-ruby/model/policy.rb | 9 +++------ lib/casbin-ruby/util.rb | 2 +- spec/casbin/enforcer_spec.rb | 10 ++++++++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/casbin-ruby/model/policy.rb b/lib/casbin-ruby/model/policy.rb index cea050d..4b7d256 100644 --- a/lib/casbin-ruby/model/policy.rb +++ b/lib/casbin-ruby/model/policy.rb @@ -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| @@ -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 diff --git a/lib/casbin-ruby/util.rb b/lib/casbin-ruby/util.rb index c246fb9..6f6c030 100644 --- a/lib/casbin-ruby/util.rb +++ b/lib/casbin-ruby/util.rb @@ -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`. diff --git a/spec/casbin/enforcer_spec.rb b/spec/casbin/enforcer_spec.rb index ba9c839..e876a1b 100644 --- a/spec/casbin/enforcer_spec.rb +++ b/spec/casbin/enforcer_spec.rb @@ -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 From 5cf410e0fa574eddcdee910c15061c9637924acc Mon Sep 17 00:00:00 2001 From: Jokius Date: Fri, 21 May 2021 08:58:51 +0300 Subject: [PATCH 2/2] Bump version to v1.0.5 --- lib/casbin-ruby/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/casbin-ruby/version.rb b/lib/casbin-ruby/version.rb index 78eaafc..7adf9d4 100644 --- a/lib/casbin-ruby/version.rb +++ b/lib/casbin-ruby/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Casbin - VERSION = '1.0.4' + VERSION = '1.0.5' end