module Casbin::Util

Constants

EVAL_REG

Public Class Methods

array_remove_duplicates(arr) click to toggle source

removes any duplicated elements in a string array.

# File lib/casbin-ruby/util.rb, line 31
def array_remove_duplicates(arr)
  arr.uniq
end
array_to_string(arr) click to toggle source

gets a printable string for a string array.

# File lib/casbin-ruby/util.rb, line 36
def array_to_string(arr)
  arr.join(', ')
end
escape_assertion(string) click to toggle source

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

# 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) click to toggle source

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

# File lib/casbin-ruby/util.rb, line 65
def get_eval_value(string)
  string.scan(EVAL_REG).flatten
end
has_eval(string) click to toggle source

determine whether matcher contains function eval

# File lib/casbin-ruby/util.rb, line 46
def has_eval(string)
  EVAL_REG.match?(string)
end
join_slice(a, *b) click to toggle source

joins a string and a slice into a new slice.

# File lib/casbin-ruby/util.rb, line 70
def join_slice(a, *b)
  Array(a).concat b
end
params_to_string(*params) click to toggle source

gets a printable string for variable number of parameters.

# File lib/casbin-ruby/util.rb, line 41
def params_to_string(*params)
  params.join(', ')
end
remove_comments(string) click to toggle source

removes the comments starting with # in the text.

# File lib/casbin-ruby/util.rb, line 9
def remove_comments(string)
  string.split('#').first.strip
end
replace_eval(expr, rules) click to toggle source

replace all occurrences of function eval with rules

# 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) click to toggle source

returns the elements in `a` that aren't in `b`.

# File lib/casbin-ruby/util.rb, line 75
def set_subtract(a, b)
  a - b
end