-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
BuiltinOperations.php
320 lines (280 loc) · 7.27 KB
/
BuiltinOperations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
declare(strict_types=1);
namespace Casbin\Util;
use Casbin\Rbac\RoleManager;
use Closure;
use Exception;
use IPTools\IP;
use IPTools\Range;
/**
* Class BuiltinOperations.
*
* @author techlee@qq.com
*/
class BuiltinOperations
{
/**
* Determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
* For example, "/foo/bar" matches "/foo/*".
*
* @param string $key1
* @param string $key2
*
* @return bool
*/
public static function keyMatch(string $key1, string $key2): bool
{
if (false === strpos($key2, '*')) {
return $key1 == $key2;
}
$needle = rtrim($key2, '*');
return substr($key1, 0, \strlen($needle)) === (string)$needle;
}
/**
* The wrapper for KeyMatch.
*
* @param mixed ...$args
*
* @return bool
*/
public static function keyMatchFunc(...$args): bool
{
$name1 = $args[0];
$name2 = $args[1];
return self::keyMatch($name1, $name2);
}
/**
* 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".
*
* @param string $key1
* @param string $key2
*
* @return bool
*/
public static function keyMatch2(string $key1, string $key2): bool
{
if ('*' === $key2) {
$key2 = '.*';
}
$key2 = str_replace(['/*'], ['/.*'], $key2);
$pattern = '/:[^\/]+/';
$key2 = preg_replace_callback(
$pattern,
function ($m) {
return '[^\/]+';
},
$key2
);
return self::regexMatch($key1, '^' . $key2 . '$');
}
/**
* The wrapper for KeyMatch2.
*
* @param mixed ...$args
*
* @return bool
*/
public static function keyMatch2Func(...$args): bool
{
$name1 = $args[0];
$name2 = $args[1];
return self::keyMatch2($name1, $name2);
}
/**
* 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}".
*
* @param string $key1
* @param string $key2
*
* @return bool
*/
public static function keyMatch3(string $key1, string $key2): bool
{
$key2 = str_replace(['/*'], ['/.*'], $key2);
$pattern = '/\{[^\/]+\}/';
$key2 = preg_replace_callback(
$pattern,
function ($m) {
return '[^\/]+';
},
$key2
);
return self::regexMatch($key1, '^' . $key2 . '$');
}
/**
* The wrapper for KeyMatch3.
*
* @param mixed ...$args
*
* @return bool
*/
public static function keyMatch3Func(...$args): bool
{
$name1 = $args[0];
$name2 = $args[1];
return self::keyMatch3($name1, $name2);
}
/**
* Determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
* Besides what KeyMatch3 does, KeyMatch4 can also match repeated patterns:
* "/parent/123/child/123" matches "/parent/{id}/child/{id}"
* "/parent/123/child/456" does not match "/parent/{id}/child/{id}"
* But KeyMatch3 will match both.
*
* @param string $key1
* @param string $key2
*
* @return bool
*/
public static function keyMatch4(string $key1, string $key2): bool
{
$key2 = str_replace(['/*'], ['/.*'], $key2);
$tokens = [];
$pattern = '/\{([^\/]+)\}/';
$key2 = preg_replace_callback(
$pattern,
function ($m) use (&$tokens) {
$tokens[] = $m[1];
return '([^\/]+)';
},
$key2
);
$matched = preg_match_all('~^' . $key2 . '$~', $key1, $matches);
if (!$matched) {
return false;
}
$values = [];
foreach ($tokens as $key => $token) {
if (!isset($values[$token])) {
$values[$token] = $matches[$key + 1];
}
if ($values[$token] != $matches[$key + 1]) {
return false;
}
}
return true;
}
/**
* The wrapper for KeyMatch4.
*
* @param mixed ...$args
*
* @return bool
*/
public static function keyMatch4Func(...$args): bool
{
$name1 = $args[0];
$name2 = $args[1];
return self::keyMatch4($name1, $name2);
}
/**
* Determines whether key1 matches the pattern of key2 in regular expression.
*
* @param string $key1
* @param string $key2
*
* @return bool
*/
public static function regexMatch(string $key1, string $key2): bool
{
return (bool)preg_match('~' . $key2 . '~', $key1);
}
/**
* The wrapper for RegexMatch.
*
* @param mixed ...$args
*
* @return bool
*/
public static function regexMatchFunc(...$args): bool
{
$name1 = $args[0];
$name2 = $args[1];
return self::regexMatch($name1, $name2);
}
/**
* Determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR
* pattern.
*
* @param string $ip1
* @param string $ip2
*
* @return bool
*
* @throws Exception
*/
public static function ipMatch(string $ip1, string $ip2): bool
{
$objIP1 = IP::parse($ip1);
$objIP2 = Range::parse($ip2);
return $objIP2->contains($objIP1);
}
/**
* The wrapper for IPMatch.
*
* @param mixed ...$args
*
* @return bool
*
* @throws Exception
*/
public static function ipMatchFunc(...$args): bool
{
$ip1 = $args[0];
$ip2 = $args[1];
return self::ipMatch($ip1, $ip2);
}
/**
* Returns true if the specified `string` matches the given glob `pattern`.
*
* @param string $str
* @param string $pattern
*
* @return bool
*
* @throws Exception
*/
public static function globMatch(string $str, string $pattern): bool
{
return fnmatch($pattern, $str, FNM_PATHNAME | FNM_PERIOD);
}
/**
* The wrapper for globMatch.
*
* @param mixed ...$args
*
* @return bool
*
* @throws Exception
*/
public static function globMatchFunc(...$args): bool
{
$str = $args[0];
$pattern = $args[1];
return self::globMatch($str, $pattern);
}
/**
* The factory method of the g(_, _) function.
*
* @param RoleManager|null $rm
*
* @return Closure
*/
public static function generateGFunction(RoleManager $rm = null): Closure
{
return function (...$args) use ($rm) {
$name1 = $args[0];
$name2 = $args[1];
if (null === $rm) {
return $name1 == $name2;
} elseif (2 == \count($args)) {
return $rm->hasLink($name1, $name2);
} else {
$domain = (string)$args[2];
return $rm->hasLink($name1, $name2, $domain);
}
};
}
}