Skip to content

Commit 79b94b4

Browse files
committed
feat: add MatchingFunctions.
1 parent a1612c2 commit 79b94b4

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

casbin/util/built_in_functions.cpp

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <regex>
2222

23+
#include "../model/function.h"
2324
#include "./built_in_functions.h"
2425
#include "../rbac/role_manager.h"
2526
#include "./util.h"
@@ -37,20 +38,20 @@ ReturnType KeyMatch(Scope scope) {
3738
string key1 = GetString(scope, 0);
3839
string key2 = GetString(scope, 1);
3940

41+
PushBooleanValue(scope, KeyMatch(key1, key2));
42+
return RETURN_RESULT;
43+
}
44+
45+
bool KeyMatch(string key1, string key2) {
4046
size_t pos = key2.find("*");
4147

42-
if (pos == string :: npos) {
43-
PushBooleanValue(scope, key1 == key2);
44-
return RETURN_RESULT;
45-
}
48+
if (pos == string :: npos)
49+
return key1 == key2;
4650

47-
if (key1.length() > pos) {
48-
PushBooleanValue(scope, key1.substr(0, pos) == key2.substr(0, pos));
49-
return RETURN_RESULT;
50-
}
51+
if (key1.length() > pos)
52+
return key1.substr(0, pos) == key2.substr(0, pos);
5153

52-
PushBooleanValue(scope, key1 == key2.substr(0, pos));
53-
return RETURN_RESULT;
54+
return key1 == key2.substr(0, pos);
5455
}
5556

5657
// KeyMatch2 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
@@ -59,6 +60,11 @@ ReturnType KeyMatch2(Scope scope) {
5960
string key1 = GetString(scope, 0);
6061
string key2 = GetString(scope, 1);
6162

63+
PushBooleanValue(scope, KeyMatch2(key1, key2));
64+
return RETURN_RESULT;
65+
}
66+
67+
bool KeyMatch2(string key1, string key2) {
6268
vector<string> key1_arr = Split(key1, "/");
6369
vector<string> key2_arr = Split(key2, "/");
6470

@@ -99,8 +105,7 @@ ReturnType KeyMatch2(Scope scope) {
99105
if(key2_arr[key2_arr.size()-1] != "*")
100106
res = false;
101107

102-
PushBooleanValue(scope, res);
103-
return RETURN_RESULT;
108+
return res;
104109
}
105110

106111
// KeyMatch3 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
@@ -109,6 +114,11 @@ ReturnType KeyMatch3(Scope scope) {
109114
string key1 = GetString(scope, 0);
110115
string key2 = GetString(scope, 1);
111116

117+
PushBooleanValue(scope, KeyMatch3(key1, key2));
118+
return RETURN_RESULT;
119+
}
120+
121+
bool KeyMatch3(string key1, string key2) {
112122
vector<string> key1_arr = Split(key1, "/");
113123
vector<string> key2_arr = Split(key2, "/");
114124

@@ -150,26 +160,34 @@ ReturnType KeyMatch3(Scope scope) {
150160
if(key2_arr[key2_arr.size()-1] != "*")
151161
res = false;
152162

153-
PushBooleanValue(scope, res);
154-
return RETURN_RESULT;
163+
return res;
155164
}
156165

157166
// RegexMatch determines whether key1 matches the pattern of key2 in regular expression.
158167
ReturnType RegexMatch(Scope scope) {
159168
string key1 = GetString(scope, 0);
160169
string key2 = GetString(scope, 1);
161170

162-
regex regex_s(key2);
163-
PushBooleanValue(scope, regex_match(key1, regex_s));
171+
PushBooleanValue(scope, RegexMatch(key1, key2));
164172
return RETURN_RESULT;
165173
}
166174

175+
bool RegexMatch(string key1, string key2) {
176+
regex regex_s(key2);
177+
return regex_match(key1, regex_s);
178+
}
179+
167180
// IPMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR pattern.
168181
// For example, "192.168.2.123" matches "192.168.2.0/24"
169182
ReturnType IPMatch(Scope scope) {
170183
string ip1 = GetString(scope, 0);
171184
string ip2 = GetString(scope, 1);
172185

186+
PushBooleanValue(scope, IPMatch(ip1, ip2));
187+
return RETURN_RESULT;
188+
}
189+
190+
bool IPMatch(string ip1, string ip2) {
173191
IP objIP1 = parseIP(ip1);
174192
if (objIP1.isLegal == false)
175193
throw IllegalArgumentException("invalid argument: ip1 in IPMatch() function is not an IP address.");
@@ -180,17 +198,15 @@ ReturnType IPMatch(Scope scope) {
180198
if (objIP2.isLegal == false)
181199
throw IllegalArgumentException("invalid argument: ip1 in IPMatch() function is not an IP address.");
182200

183-
PushBooleanValue(scope, objIP1.Equal(objIP2));
184-
return RETURN_RESULT;
201+
return objIP1.Equal(objIP2);
185202
}
186203

187-
PushBooleanValue(scope, objCIDR.net.contains(objIP1));
188-
return RETURN_RESULT;
204+
return objCIDR.net.contains(objIP1);
189205
}
190206

191207
// GFunction is the method of the g(_, _) function.
192208
ReturnType GFunction(Scope scope) {
193-
RoleManager *rm;
209+
RoleManager* rm;
194210
rm = (RoleManager*)GetPointer(scope, 0);
195211
string name1 = GetString(scope, 1);
196212
string name2 = GetString(scope, 2);
@@ -199,12 +215,12 @@ ReturnType GFunction(Scope scope) {
199215

200216
if(rm == NULL)
201217
PushBooleanValue(scope, name1 == name2);
202-
else if (len == 2) {
218+
else if (len == 3) {
203219
vector<string> domain;
204220
bool res = rm->HasLink(name1, name2, domain);
205221
PushBooleanValue(scope, res);
206222
} else {
207-
vector<string> domain{GetString(scope, 2)};
223+
vector<string> domain{GetString(scope, 3)};
208224
bool res = rm->HasLink(name1, name2, domain);
209225
PushBooleanValue(scope, res);
210226
}

casbin/util/built_in_functions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,26 @@
2222
// KeyMatch determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
2323
// For example, "/foo/bar" matches "/foo/*"
2424
ReturnType KeyMatch(Scope scope);
25+
bool KeyMatch(string key1, string key2);
2526

2627
// KeyMatch2 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
2728
// For example, "/foo/bar" matches "/foo/*", "/resource1" matches "/:resource"
2829
ReturnType KeyMatch2(Scope scope);
30+
bool KeyMatch2(string key1, string key2);
2931

3032
// KeyMatch3 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
3133
// For example, "/foo/bar" matches "/foo/*", "/resource1" matches "/{resource}"
3234
ReturnType KeyMatch3(Scope scope);
35+
bool KeyMatch3(string key1, string key2);
3336

3437
// RegexMatch determines whether key1 matches the pattern of key2 in regular expression.
3538
ReturnType RegexMatch(Scope scope);
39+
bool RegexMatch(string key1, string key2);
3640

3741
// IPMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR pattern.
3842
// For example, "192.168.2.123" matches "192.168.2.0/24"
3943
ReturnType IPMatch(Scope scope);
44+
bool IPMatch(string ip1, string ip2);
4045

4146
// GFunction is the method of the g(_, _) function.
4247
ReturnType GFunction(Scope scope);

0 commit comments

Comments
 (0)