Module: Casbin::Util::BuiltinOperators
- Defined in:
- lib/casbin-ruby/util/builtin_operators.rb
Constant Summary collapse
- KEY_MATCH2_PATTERN =
%r{:[^/]+}.freeze
- KEY_MATCH3_PATTERN =
%r{\{[^/]+\}}.freeze
Class Method Summary collapse
-
.generate_g_function(rm) ⇒ Object
the factory method of the g(_, _) function.
-
.glob_match(string, pattern) ⇒ Object
determines whether string matches the pattern in glob expression.
-
.glob_match_func(*args) ⇒ Object
the wrapper for globMatch.
-
.ip_match(ip1, ip2) ⇒ Object
IPMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR pattern.
-
.ip_match_func(*args) ⇒ Object
the wrapper for IPMatch.
-
.key_match(key1, key2) ⇒ Object
determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
-
.key_match2(key1, key2) ⇒ Object
determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
- .key_match2_func(*args) ⇒ Object
-
.key_match3(key1, key2) ⇒ Object
determines determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
- .key_match3_func(*args) ⇒ Object
-
.key_match_func(*args) ⇒ Object
The wrapper for key_match.
-
.regex_match(key1, key2) ⇒ Object
determines whether key1 matches the pattern of key2 in regular expression.
-
.regex_match_func(*args) ⇒ Object
the wrapper for RegexMatch.
Class Method Details
.generate_g_function(rm) ⇒ Object
the factory method of the g(_, _) function.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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) ⇒ Object
determines whether string matches the pattern in glob expression.
71 72 73 |
# 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) ⇒ Object
the wrapper for globMatch.
29 30 31 |
# 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) ⇒ Object
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
78 79 80 81 82 83 84 |
# 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) ⇒ Object
the wrapper for IPMatch.
34 35 36 |
# 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) ⇒ Object
determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *. For example, “/foo/bar” matches “/foo/
40 41 42 43 44 45 46 |
# 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) ⇒ Object
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
50 51 52 53 54 |
# 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) ⇒ Object
15 16 17 |
# 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) ⇒ Object
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”
59 60 61 62 63 |
# 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) ⇒ Object
19 20 21 |
# 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) ⇒ Object
The wrapper for key_match.
11 12 13 |
# 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) ⇒ Object
determines whether key1 matches the pattern of key2 in regular expression.
66 67 68 |
# File 'lib/casbin-ruby/util/builtin_operators.rb', line 66 def regex_match(key1, key2) (key1 =~ /#{key2}/)&.zero? || false end |
.regex_match_func(*args) ⇒ Object
the wrapper for RegexMatch.
24 25 26 |
# File 'lib/casbin-ruby/util/builtin_operators.rb', line 24 def regex_match_func(*args) regex_match(args[0], args[1]) end |