module Casbin::Util::BuiltinOperators

Constants

KEY_MATCH2_PATTERN
KEY_MATCH3_PATTERN

Public Class Methods

generate_g_function(rm) click to toggle source

the factory method of the g(_, _) function.

# File lib/casbin-ruby/util/builtin_operators.rb, line 87
def generate_g_function(rm)
  return ->(*args) { args[0] == args[1] } unless rm

  lambda do |*args|
    name1 = args[0]
    name2 = args[1]

    if args.length == 2
      rm.has_link(name1, name2)
    else
      domain = args[2].to_s
      rm.has_link(name1, name2, domain)
    end
  end
end
glob_match(string, pattern) click to toggle source

determines whether string matches the pattern in glob expression.

# File lib/casbin-ruby/util/builtin_operators.rb, line 71
def glob_match(string, pattern)
  File.fnmatch(pattern, string, File::FNM_PATHNAME)
end
glob_match_func(*args) click to toggle source

the wrapper for globMatch.

# File lib/casbin-ruby/util/builtin_operators.rb, line 29
def glob_match_func(*args)
  glob_match(args[0], args[1])
end
ip_match(ip1, ip2) click to toggle source

IPMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR pattern. For example, “192.168.2.123” matches “192.168.2.0/24

# File lib/casbin-ruby/util/builtin_operators.rb, line 78
def ip_match(ip1, ip2)
  ip = IPAddr.new(ip1)
  network = IPAddr.new(ip2)
  network.include?(ip)
rescue IPAddr::InvalidAddressError
  ip1 == ip2
end
ip_match_func(*args) click to toggle source

the wrapper for IPMatch.

# File lib/casbin-ruby/util/builtin_operators.rb, line 34
def ip_match_func(*args)
  ip_match(args[0], args[1])
end
key_match(key1, key2) click to toggle source

determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *. For example, “/foo/bar” matches “/foo/

# File lib/casbin-ruby/util/builtin_operators.rb, line 40
def key_match(key1, key2)
  i = key2.index('*')
  return key1 == key2 if i.nil?
  return key1[0...i] == key2[0...i] if key1.size > i

  key1 == key2[0...i]
end
key_match2(key1, key2) click to toggle source

determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *. For example, “/foo/bar” matches “/foo/*”, “/resource1” matches “/:resource

# File lib/casbin-ruby/util/builtin_operators.rb, line 50
def key_match2(key1, key2)
  key2 = key2.gsub('/*', '/.*')
  key2 = key2.gsub(KEY_MATCH2_PATTERN, '\1[^/]+\2')
  regex_match(key1, "^#{key2}$")
end
key_match2_func(*args) click to toggle source
# File lib/casbin-ruby/util/builtin_operators.rb, line 15
def key_match2_func(*args)
  key_match2(args[0], args[1])
end
key_match3(key1, key2) click to toggle source

determines determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *. For example, “/foo/bar” matches “/foo/*”, “/resource1” matches “/{resource}”

# File lib/casbin-ruby/util/builtin_operators.rb, line 59
def key_match3(key1, key2)
  key2 = key2.gsub('/*', '/.*')
  key2 = key2.gsub(KEY_MATCH3_PATTERN, '\1[^\/]+\2')
  regex_match(key1, "^#{key2}$")
end
key_match3_func(*args) click to toggle source
# File lib/casbin-ruby/util/builtin_operators.rb, line 19
def key_match3_func(*args)
  key_match3(args[0], args[1])
end
key_match_func(*args) click to toggle source

The wrapper for key_match.

# File lib/casbin-ruby/util/builtin_operators.rb, line 11
def key_match_func(*args)
  key_match(args[0], args[1])
end
regex_match(key1, key2) click to toggle source

determines whether key1 matches the pattern of key2 in regular expression.

# File lib/casbin-ruby/util/builtin_operators.rb, line 66
def regex_match(key1, key2)
  (key1 =~ /#{key2}/)&.zero? || false
end
regex_match_func(*args) click to toggle source

the wrapper for RegexMatch.

# File lib/casbin-ruby/util/builtin_operators.rb, line 24
def regex_match_func(*args)
  regex_match(args[0], args[1])
end