Skip to content

Commit bd4e66c

Browse files
committed
fix: enforcer duktape configuration.
1 parent 376c972 commit bd4e66c

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

casbin/enforcer.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "pch.h"
2020

21+
// #include <iostream>
22+
2123
#include "./enforcer.h"
2224
#include "./persist/watcher_ex.h"
2325
#include "./persist/file_adapter/file_adapter.h"
@@ -38,116 +40,116 @@ bool Enforcer :: enforce(string matcher, Scope scope) {
3840

3941
this->func_map.scope = scope;
4042

41-
if(this->enabled)
43+
if(!this->enabled)
4244
return true;
4345

4446
// for(unordered_map <string, Function> :: iterator it = this->fm.fmap.begin() ; it != this->fm.fmap.end() ; it++)
4547
// this->fm.AddFunction(it->first, it->second);
4648

47-
string expString;
49+
string exp_string;
4850
if(matcher == "")
49-
expString = this->model->m["m"].assertion_map["m"]->value;
51+
exp_string = this->model->m["m"].assertion_map["m"]->value;
5052
else
51-
expString = matcher;
53+
exp_string = matcher;
5254

5355
unordered_map <string, RoleManager*> rm_map;
5456
bool ok = this->model->m.find("g") != this->model->m.end();
5557
if(ok) {
5658
for(unordered_map <string, Assertion*> :: iterator it = this->model->m["g"].assertion_map.begin() ; it != this->model->m["g"].assertion_map.end() ; it++){
5759
RoleManager* rm = it->second->rm;
58-
int index = int(expString.find((it->first)+"("));
60+
int index = int(exp_string.find((it->first)+"("));
5961
if(index != string::npos)
60-
expString.insert(index+(it->first+"(").length()-1, (it->first)+"_rm");
62+
exp_string.insert(index+(it->first+"(").length()-1, (it->first)+"_rm");
6163
PushPointer(this->func_map.scope, (void *)rm, (it->first)+"_rm");
6264
this->func_map.AddFunction(it->first, GFunction);
6365
}
6466
}
6567

66-
unordered_map <string, int> pIntTokens;
68+
unordered_map <string, int> p_int_tokens;
6769
for(int i = 0 ; i < this->model->m["p"].assertion_map["p"]->tokens.size() ; i++)
68-
pIntTokens[this->model->m["p"].assertion_map["p"]->tokens[i]] = i;
70+
p_int_tokens[this->model->m["p"].assertion_map["p"]->tokens[i]] = i;
6971

70-
vector <string> pTokens = this->model->m["p"].assertion_map["p"]->tokens;
72+
vector <string> p_tokens = this->model->m["p"].assertion_map["p"]->tokens;
7173

72-
vector <Effect> policyEffects;
73-
vector <float> matcherResults;
74+
int policy_len = int(this->model->m["p"].assertion_map["p"]->policy.size());
7475

75-
int policyLen = int(this->model->m["p"].assertion_map["p"]->policy.size());
76+
vector <Effect> policy_effects(policy_len, Effect :: Indeterminate);
77+
vector <float> matcher_results;
7678

77-
if(policyLen != 0) {
79+
if(policy_len != 0) {
7880
if(this->model->m["r"].assertion_map["r"]->tokens.size() != this->func_map.GetRLen())
7981
return false;
8082

8183
//TODO
82-
for( int i = 0 ; i < this->model->m["p"].assertion_map["p"]->policy.size() ; i++){
84+
for( int i = 0 ; i < policy_len ; i++){
8385
// log.LogPrint("Policy Rule: ", pvals)
84-
vector<string> pVals = this->model->m["p"].assertion_map["p"]->policy[i];
85-
if(this->model->m["p"].assertion_map["p"]->tokens.size() != pVals.size())
86+
vector<string> p_vals = this->model->m["p"].assertion_map["p"]->policy[i];
87+
if(this->model->m["p"].assertion_map["p"]->tokens.size() != p_vals.size())
8688
return false;
8789

8890
PushObject(this->func_map.scope, "p");
89-
for(int j = 0 ; j < pTokens.size() ; j++){
90-
int index = int(pTokens[j].find("_"));
91-
string token = pTokens[j].substr(index+1);
92-
PushStringPropToObject(this->func_map.scope, "p", pVals[j], token);
91+
for(int j = 0 ; j < p_tokens.size() ; j++){
92+
int index = int(p_tokens[j].find("_"));
93+
string token = p_tokens[j].substr(index+1);
94+
PushStringPropToObject(this->func_map.scope, "p", p_vals[j], token);
9395
}
9496

95-
this->func_map.Eval(expString);
97+
this->func_map.Eval(exp_string);
98+
9699
//TODO
97100
// log.LogPrint("Result: ", result)
98-
99101
if(CheckType(this->func_map.scope) == Type :: Bool){
100102
bool result = GetBoolean(this->func_map.scope);
101103
if(!result) {
102-
policyEffects[i] = Effect :: Indeterminate;
104+
policy_effects[i] = Effect :: Indeterminate;
103105
continue;
104106
}
105107
}
106108
else if(CheckType(this->func_map.scope) == Type :: Float){
107109
bool result = GetFloat(this->func_map.scope);
108110
if(result == 0) {
109-
policyEffects[i] = Effect :: Indeterminate;
111+
policy_effects[i] = Effect :: Indeterminate;
110112
continue;
111113
} else
112-
matcherResults[i] = result;
114+
matcher_results[i] = result;
113115
}
114116
else
115117
return false;
116118

117-
bool ok = pIntTokens.find("p_eft") != pIntTokens.end();
118-
if(ok) {
119-
int j = pIntTokens["p_eft"];
120-
string eft = pVals[j];
119+
bool is_p_eft = p_int_tokens.find("p_eft") != p_int_tokens.end();
120+
if(is_p_eft) {
121+
int j = p_int_tokens["p_eft"];
122+
string eft = p_vals[j];
121123
if(eft == "allow")
122-
policyEffects[i] = Effect :: Allow;
124+
policy_effects[i] = Effect :: Allow;
123125
else if(eft == "deny")
124-
policyEffects[i] = Effect :: Deny;
126+
policy_effects[i] = Effect :: Deny;
125127
else
126-
policyEffects[i] = Effect :: Indeterminate;
128+
policy_effects[i] = Effect :: Indeterminate;
127129
}
128130
else
129-
policyEffects[i] = Effect :: Allow;
131+
policy_effects[i] = Effect :: Allow;
130132

131133
if(this->model->m["e"].assertion_map["e"]->value == "priority(p_eft) || deny")
132134
break;
133135
}
134136
} else {
135-
this->func_map.Eval(expString);
137+
this->func_map.Eval(exp_string);
136138
bool result = this->func_map.GetBooleanResult();
137139
//TODO
138140
// log.LogPrint("Result: ", result)
139141

140142
if(result)
141-
policyEffects[0] = Effect::Allow;
143+
policy_effects[0] = Effect::Allow;
142144
else
143-
policyEffects[0] = Effect::Indeterminate;
145+
policy_effects[0] = Effect::Indeterminate;
144146
}
145147

146148
//TODO
147149
// log.LogPrint("Rule Results: ", policyEffects)
148150

149-
bool result = this->eft->MergeEffects(this->model->m["e"].assertion_map["e"]->value, policyEffects, matcherResults);
150-
151+
bool result = this->eft->MergeEffects(this->model->m["e"].assertion_map["e"]->value, policy_effects, matcher_results);
152+
151153
return result;
152154
}
153155

casbin/model/function.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "./function.h"
2222

2323
FunctionMap :: FunctionMap(){
24-
scope = duk_create_heap_default();
24+
scope = InitializeScope();
2525
}
2626

2727
int FunctionMap :: GetRLen(){

casbin/model/model.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ bool Model :: AddDef(string sec, string key, string value) {
9191
ast->tokens = Split(ast->value, ",");
9292
for (int i = 0; i < ast->tokens.size() ; i++)
9393
ast->tokens[i] = key + "_" + Trim(ast->tokens[i]);
94-
}
95-
else
94+
} else if(sec == "e")
9695
ast->value = RemoveComments(EscapeAssertion(ast->value));
96+
else
97+
ast->value = RemoveComments(ast->value);
9798

9899
if (m.find(sec) != m.end())
99100
m[sec] = AssertionMap();

casbin/model/scope_config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void PushPointer(Scope scope, void * ptr, string identifier){
110110
}
111111

112112
void PushObject(Scope scope, string identifier){
113-
duk_push_global_object(scope);
113+
duk_push_object(scope);
114114
duk_put_global_string(scope, identifier.c_str());
115115
duk_push_int(scope, 0);
116116
duk_put_global_string(scope, (identifier+"len").c_str());

0 commit comments

Comments
 (0)