Module: Casbin::Util

Defined in:
lib/casbin-ruby/util.rb,
lib/casbin-ruby/util/evaluator.rb,
lib/casbin-ruby/util/builtin_operators.rb

Defined Under Namespace

Modules: BuiltinOperators Classes: Evaluator

Constant Summary collapse

EVAL_REG =
/\beval\(([^),]*)\)/.freeze

Class Method Summary collapse

Class Method Details

.array_remove_duplicates(arr) ⇒ Object

removes any duplicated elements in a string array.



31
32
33
# File 'lib/casbin-ruby/util.rb', line 31

def array_remove_duplicates(arr)
  arr.uniq
end

.array_to_string(arr) ⇒ Object

gets a printable string for a string array.



36
37
38
# File 'lib/casbin-ruby/util.rb', line 36

def array_to_string(arr)
  arr.join(', ')
end

.escape_assertion(string) ⇒ Object

Escapes the dots in the assertion, because the expression evaluation doesn’t support such variable names. Also it replaces attributes with hash syntax (‘r.obj.Owner` -> `r_obj`, `r.obj.Owner.Position` -> `r_obj[’Position’]‘), because Keisan functions work in both regular `f(x)` and postfix `x.f()` notation, where for example `a.f(b,c)` is translated internally to `f(a,b,c)` - github.com/project-eutopia/keisan#specifying-functions For now we replace attributes for the request elements like `r.sub`, `r.obj`, `r.act` casbin.org/docs/en/abac#how-to-use-abac We support Unicode in attributes for the compatibility with Golang - golang.org/ref/spec#Identifiers



21
22
23
24
25
26
27
28
# File 'lib/casbin-ruby/util.rb', line 21

def escape_assertion(string)
  string.gsub(/\br\.(\w+)((?:\.[[:alpha:]_][[:alnum:]_]*)+)/) do |_|
    param = Regexp.last_match(1)
    attrs = Regexp.last_match(2)[1..-1]&.split('.')&.map { |attr| "['#{attr}']" }
    attrs = attrs&.join || ''
    "r_#{param}#{attrs}"
  end.gsub('r.', 'r_').gsub('p.', 'p_')
end

.get_eval_value(string) ⇒ Object

For now, it does not used. Returns the parameters of function eval.



65
66
67
# File 'lib/casbin-ruby/util.rb', line 65

def get_eval_value(string)
  string.scan(EVAL_REG).flatten
end

.has_eval(string) ⇒ Object

determine whether matcher contains function eval



46
47
48
# File 'lib/casbin-ruby/util.rb', line 46

def has_eval(string)
  EVAL_REG.match?(string)
end

.join_slice(a, *b) ⇒ Object

joins a string and a slice into a new slice.



70
71
72
# File 'lib/casbin-ruby/util.rb', line 70

def join_slice(a, *b)
  Array(a).concat b
end

.params_to_string(*params) ⇒ Object

gets a printable string for variable number of parameters.



41
42
43
# File 'lib/casbin-ruby/util.rb', line 41

def params_to_string(*params)
  params.join(', ')
end

.remove_comments(string) ⇒ Object

removes the comments starting with # in the text.



9
10
11
# File 'lib/casbin-ruby/util.rb', line 9

def remove_comments(string)
  string.split('#').first.strip
end

.replace_eval(expr, rules) ⇒ Object

replace all occurrences of function eval with rules



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/casbin-ruby/util.rb', line 51

def replace_eval(expr, rules)
  i = -1
  expr.gsub EVAL_REG do |_|
    i += 1
    if rules.is_a? Hash
      "(#{escape_assertion rules[Regexp.last_match 1]})"
    else
      "(#{escape_assertion rules[i]})"
    end
  end
end

.set_subtract(a, b) ⇒ Object

returns the elements in ‘a` that aren’t in ‘b`.



75
76
77
# File 'lib/casbin-ruby/util.rb', line 75

def set_subtract(a, b)
  a - b
end