Skip to content

Commit 405b251

Browse files
committed
fix: change expression evaluation logic.
1 parent e5288c2 commit 405b251

File tree

5 files changed

+87
-39
lines changed

5 files changed

+87
-39
lines changed

casbin/model/Function.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ class FunctionMap {
3030

3131
FunctionMap();
3232

33+
void ProcessFunctions(string expression);
34+
3335
int GetRLen();
3436

35-
void Eval(string expression);
37+
bool Evaluate(string expression);
3638

3739
bool GetBooleanResult();
3840

3941
// AddFunction adds an expression function.
40-
void AddFunction(string func_name, Function f, Index nargs = VARARGS);
42+
void AddFunction(string func_name, Function f, Index nargs);
4143

42-
void AddFunctionPropToR(string identifier, Function func, unsigned int nargs = VARARGS);
44+
void AddFunctionPropToR(string identifier, Function func, Index nargs);
4345

4446
void AddBooleanPropToR(string identifier, bool val);
4547

@@ -60,7 +62,7 @@ class FunctionMap {
6062
void AddObjectPropToR(string identifier);
6163

6264
// LoadFunctionMap loads an initial function map.
63-
static FunctionMap LoadFunctionMap();
65+
void LoadFunctionMap();
6466

6567
};
6668

casbin/model/function.cpp

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,49 @@
1919
#include "pch.h"
2020

2121
#include "./function.h"
22+
#include "../util/util.h"
2223

2324
FunctionMap :: FunctionMap(){
2425
scope = InitializeScope();
2526
}
2627

28+
void FunctionMap :: ProcessFunctions(string expression){
29+
for(unordered_map<string, Function> :: iterator it = this->func_map.begin() ; it != this->func_map.end() ; it++){
30+
int index = int(expression.find((it->first)+"("));
31+
32+
if(index != string::npos){
33+
int close_index = expression.find(")", index);
34+
int start = index + ((it->first)+"(").length();
35+
36+
string function_params = expression.substr(start, close_index-start);
37+
FetchIdentifier(this->scope, it->first);
38+
vector<string> params = Split(function_params, ",");
39+
40+
for(int i=0;i<params.size();i++){
41+
int quote_index = params[i].find("\"");
42+
if(quote_index == string::npos)
43+
Get(this->scope, Trim(params[i]));
44+
else{
45+
params[i] = params[i].replace(quote_index, 1, "'");
46+
int second_quote_index = params[i].find("\"", quote_index+1);
47+
params[i] = params[i].replace(second_quote_index, 1, "'");
48+
Get(this->scope, Trim(params[i]));
49+
}
50+
}
51+
}
52+
}
53+
}
54+
2755
int FunctionMap :: GetRLen(){
2856
bool found = FetchIdentifier(scope, "rlen");
2957
if(found)
3058
return GetInt(scope);
3159
return -1;
3260
}
3361

34-
void FunctionMap :: Eval(string expression){
35-
duk_eval_string(scope, expression.c_str());
62+
bool FunctionMap :: Evaluate(string expression){
63+
ProcessFunctions(expression);
64+
return Eval(scope, expression);
3665
}
3766

3867
bool FunctionMap :: GetBooleanResult(){
@@ -42,11 +71,11 @@ bool FunctionMap :: GetBooleanResult(){
4271
// AddFunction adds an expression function.
4372
void FunctionMap :: AddFunction(string func_name, Function f, Index nargs) {
4473
func_map[func_name] = f;
45-
PushFunction(this->scope, f, nargs, func_name);
74+
PushFunction(this->scope, f, func_name, nargs);
4675
}
4776

48-
void FunctionMap :: AddFunctionPropToR(string identifier, Function func, unsigned int nargs){
49-
PushFunctionPropToObject(scope, "r", func, nargs, identifier);
77+
void FunctionMap :: AddFunctionPropToR(string identifier, Function func, Index nargs){
78+
PushFunctionPropToObject(scope, "r", func, identifier, nargs);
5079
}
5180

5281
void FunctionMap :: AddBooleanPropToR(string identifier, bool val){
@@ -86,14 +115,10 @@ void FunctionMap :: AddObjectPropToR(string identifier){
86115
}
87116

88117
// LoadFunctionMap loads an initial function map.
89-
FunctionMap FunctionMap :: LoadFunctionMap() {
90-
FunctionMap func_map;
91-
92-
func_map.AddFunction("keyMatch", KeyMatch);
93-
func_map.AddFunction("keyMatch2", KeyMatch2);
94-
func_map.AddFunction("keyMatch3", KeyMatch3);
95-
func_map.AddFunction("regexMatch", RegexMatch);
96-
func_map.AddFunction("ipMatch", IPMatch);
97-
98-
return func_map;
118+
void FunctionMap :: LoadFunctionMap() {
119+
AddFunction("keyMatch", KeyMatch, 2);
120+
AddFunction("keyMatch2", KeyMatch2, 2);
121+
AddFunction("keyMatch3", KeyMatch3, 2);
122+
AddFunction("regexMatch", RegexMatch, 2);
123+
AddFunction("ipMatch", IPMatch, 2);
99124
}

casbin/model/model.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ bool Model :: HasSection(string sec) {
5757
void Model :: LoadSection(Model* model, ConfigInterface* cfg, string sec) {
5858
int i = 1;
5959
while(true) {
60-
if (!LoadAssertion(model, cfg, sec, sec+GetKeySuffix(i)))
60+
if (!LoadAssertion(model, cfg, sec, sec+GetKeySuffix(i))){
6161
break;
62+
}
6263
else
6364
i++;
6465
}
@@ -91,14 +92,14 @@ bool Model :: AddDef(string sec, string key, string value) {
9192
ast->tokens = Split(ast->value, ",");
9293
for (int i = 0; i < ast->tokens.size() ; i++)
9394
ast->tokens[i] = key + "_" + Trim(ast->tokens[i]);
94-
} else if(sec == "m")
95-
ast->value = RemoveComments(ast->value);
95+
}
9696
else
97-
ast->value = RemoveComments(EscapeAssertion(ast->value));
97+
ast->value = RemoveComments(ast->value);
9898

99-
if (m.find(sec) != m.end())
99+
if (m.find(sec) == m.end())
100100
m[sec] = AssertionMap();
101101
ast->policy = vector<vector<string>>{};
102+
102103
m[sec].assertion_map[key] = ast;
103104

104105
return true;
@@ -161,9 +162,8 @@ void Model :: BuildIncrementalRoleLinks(RoleManager* rm, policy_op op, string se
161162

162163
// BuildRoleLinks initializes the roles in RBAC.
163164
void Model :: BuildRoleLinks(RoleManager* rm) {
164-
for (unordered_map<string, Assertion*> :: iterator it = this->m["g"].assertion_map.begin() ; it != this->m["g"].assertion_map.end() ; it++) {
165+
for (unordered_map<string, Assertion*> :: iterator it = this->m["g"].assertion_map.begin() ; it != this->m["g"].assertion_map.end() ; it++)
165166
(it->second)->BuildRoleLinks(rm);
166-
}
167167
}
168168

169169
// PrintPolicy prints the policy to log.
@@ -187,13 +187,15 @@ void Model :: PrintPolicy() {
187187

188188
// ClearPolicy clears all current policy.
189189
void Model :: ClearPolicy() {
190-
for (unordered_map<string, Assertion*> :: iterator it = this->m["p"].assertion_map.begin() ; it != this->m["p"].assertion_map.end() ; it++)
190+
for (unordered_map<string, Assertion*> :: iterator it = this->m["p"].assertion_map.begin() ; it != this->m["p"].assertion_map.end() ; it++){
191191
if((it->second)->policy.size() > 0)
192192
(it->second)->policy.clear();
193+
}
193194

194-
for (unordered_map<string, Assertion*> :: iterator it = this->m["g"].assertion_map.begin() ; it != this->m["g"].assertion_map.end() ; it++)
195+
for (unordered_map<string, Assertion*> :: iterator it = this->m["g"].assertion_map.begin() ; it != this->m["g"].assertion_map.end() ; it++){
195196
if((it->second)->policy.size() > 0)
196197
(it->second)->policy.clear();
198+
}
197199
}
198200

199201
// GetPolicy gets all rules in a policy.
@@ -222,7 +224,7 @@ vector<vector<string>> Model :: GetFilteredPolicy(string sec, string p_type, int
222224

223225
// HasPolicy determines whether a model has the specified policy rule.
224226
bool Model :: HasPolicy(string sec, string p_type, vector<string> rule) {
225-
vector<vector<string>> policy(m[sec].assertion_map[p_type]->policy);
227+
vector<vector<string>> policy = m[sec].assertion_map[p_type]->policy;
226228
for(int i=0 ; i < policy.size() ; i++)
227229
if (ArrayEquals(rule, policy[i]))
228230
return true;
@@ -236,6 +238,7 @@ bool Model :: AddPolicy(string sec, string p_type, vector<string> rule) {
236238
m[sec].assertion_map[p_type]->policy.push_back(rule);
237239
return true;
238240
}
241+
239242
return false;
240243
}
241244

@@ -266,17 +269,19 @@ bool Model :: RemovePolicy(string sec, string p_type, vector<string> rule) {
266269
// RemovePolicies removes policy rules from the model.
267270
bool Model :: RemovePolicies(string sec, string p_type, vector<vector<string>> rules) {
268271
OUTER: for (int j = 0; j < rules.size(); j++) {
269-
for (int i = 0; i < this->m[sec].assertion_map[p_type]->policy.size(); i++)
270-
if (ArrayEquals(rules[j], this->m[sec].assertion_map[p_type]->policy[i])) {
272+
for (int i = 0; i < this->m[sec].assertion_map[p_type]->policy.size(); i++){
273+
if (ArrayEquals(rules[j], this->m[sec].assertion_map[p_type]->policy[i]))
271274
goto OUTER;
272275
}
273276
return false;
274277
}
275278

276-
for (int j = 0; j < rules.size(); j++)
277-
for (int i = 0; i < this->m[sec].assertion_map[p_type]->policy.size(); i++)
279+
for (int j = 0; j < rules.size(); j++){
280+
for (int i = 0; i < this->m[sec].assertion_map[p_type]->policy.size(); i++){
278281
if (ArrayEquals(rules[j], this->m[sec].assertion_map[p_type]->policy[i]))
279282
this->m[sec].assertion_map[p_type]->policy.erase(this->m[sec].assertion_map[p_type]->policy.begin() + i);
283+
}
284+
}
280285

281286
return true;
282287
}
@@ -289,8 +294,8 @@ pair<bool, vector<vector<string>>> Model :: RemoveFilteredPolicy(string sec, str
289294
bool res = false;
290295
for(int i = 0 ; i < policy.size() ; i++){
291296
bool matched = true;
292-
for (int i = 0 ; i < field_values.size() ; i++) {
293-
if (field_values[i] != "" && (policy[i])[field_index+i] != field_values[i]) {
297+
for (int j = 0 ; j < field_values.size() ; j++) {
298+
if (field_values[j] != "" && (policy[i])[field_index+j] != field_values[j]) {
294299
matched = false;
295300
break;
296301
}

casbin/model/scope_config.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void PushObjectValue(Scope scope){
6464
duk_push_global_object(scope);
6565
}
6666

67-
void PushFunction(Scope scope, Function f, int nargs, string fname) {
67+
void PushFunction(Scope scope, Function f, string fname, int nargs) {
6868
duk_push_c_function(scope, f, (Index)nargs);
6969
duk_put_global_string(scope, fname.c_str());
7070
}
@@ -116,7 +116,7 @@ void PushObject(Scope scope, string identifier){
116116
duk_put_global_string(scope, (identifier+"len").c_str());
117117
}
118118

119-
void PushFunctionPropToObject(Scope scope, string obj, Function f, int nargs, string fname) {
119+
void PushFunctionPropToObject(Scope scope, string obj, Function f, string fname, int nargs) {
120120
duk_get_global_string(scope, obj.c_str());
121121
duk_push_c_function(scope, f, nargs);
122122
duk_put_prop_string(scope, -2, fname.c_str());
@@ -223,4 +223,17 @@ string GetString(Scope scope, int id){
223223

224224
void* GetPointer(Scope scope, int id){
225225
return (void *)duk_to_pointer(scope, (Index)id);
226+
}
227+
228+
void Get(Scope scope, string identifier){
229+
Eval(scope, identifier);
230+
}
231+
232+
bool Eval(Scope scope, string expression){
233+
PushStringValue(scope, expression);
234+
return duk_peval(scope)==0;
235+
}
236+
237+
void EvalNoResult(Scope scope, string expression){
238+
duk_eval_string_noresult(scope, expression.c_str());
226239
}

casbin/model/scope_config.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void PushDoubleValue(Scope scope, double d);
5050
void PushStringValue(Scope scope, string s);
5151
void PushPointerValue(Scope scope, void * ptr);
5252
void PushObjectValue(Scope scope);
53-
void PushFunction(Scope scope, Function f, int nargs, string fname);
53+
void PushFunction(Scope scope, Function f, string fname, int nargs);
5454
void PushBoolean(Scope scope, bool expression, string identifier);
5555
void PushTrue(Scope scope, string identifier);
5656
void PushFalse(Scope scope, string identifier);
@@ -60,7 +60,7 @@ void PushDouble(Scope scope, double d, string identifier);
6060
void PushString(Scope scope, string s, string identifier);
6161
void PushPointer(Scope scope, void * ptr, string identifier);
6262
void PushObject(Scope scope, string identifier = "r");
63-
void PushFunctionPropToObject(Scope scope, string obj, Function f, int nargs, string fname);
63+
void PushFunctionPropToObject(Scope scope, string obj, Function f, string fname, int nargs);
6464
void PushBooleanPropToObject(Scope scope, string obj, bool expression, string identifier);
6565
void PushTruePropToObject(Scope scope, string obj, string identifier);
6666
void PushFalsePropToObject(Scope scope, string obj, string identifier);
@@ -79,5 +79,8 @@ float GetFloat(Scope scope, int id = -1);
7979
double GetDouble(Scope scope, int id = -1);
8080
string GetString(Scope scope, int id = -1);
8181
void* GetPointer(Scope scope, int id = -1);
82+
void Get(Scope scope, string identifier);
83+
bool Eval(Scope scope, string expression);
84+
void EvalNoResult(Scope scope, string expression);
8285

8386
#endif

0 commit comments

Comments
 (0)