1818
1919#include " pch.h"
2020
21+ #include < algorithm>
22+
2123#include " ./enforcer.h"
2224#include " ./persist/watcher_ex.h"
2325#include " ./persist/file_adapter/file_adapter.h"
@@ -37,117 +39,123 @@ bool Enforcer :: enforce(string matcher, Scope scope) {
3739 // }()
3840
3941 this ->func_map .scope = scope;
42+ this ->func_map .LoadFunctionMap ();
4043
41- if (this ->enabled )
44+ if (! this ->enabled )
4245 return true ;
4346
4447 // for(unordered_map <string, Function> :: iterator it = this->fm.fmap.begin() ; it != this->fm.fmap.end() ; it++)
4548 // this->fm.AddFunction(it->first, it->second);
4649
47- string expString ;
50+ string exp_string ;
4851 if (matcher == " " )
49- expString = this ->model ->m [" m" ].assertion_map [" m" ]->value ;
52+ exp_string = this ->model ->m [" m" ].assertion_map [" m" ]->value ;
5053 else
51- expString = matcher;
54+ exp_string = matcher;
55+
5256
5357 unordered_map <string, RoleManager*> rm_map;
5458 bool ok = this ->model ->m .find (" g" ) != this ->model ->m .end ();
59+
5560 if (ok) {
5661 for (unordered_map <string, Assertion*> :: iterator it = this ->model ->m [" g" ].assertion_map .begin () ; it != this ->model ->m [" g" ].assertion_map .end () ; it++){
5762 RoleManager* rm = it->second ->rm ;
58- int index = int (expString.find ((it->first )+" (" ));
63+ int char_count = int (count (it->second ->value .begin (), it->second ->value .end (), ' _' ));
64+ int index = int (exp_string.find ((it->first )+" (" ));
5965 if (index != string::npos)
60- expString .insert (index+(it->first +" (" ).length ()- 1 , (it-> first )+ " _rm " );
61- PushPointer (this ->func_map .scope , (void *)rm, (it-> first )+ " _rm " );
62- this ->func_map .AddFunction (it->first , GFunction);
66+ exp_string .insert (index+(it->first +" (" ).length (), " rm, " );
67+ PushPointer (this ->func_map .scope , (void *)rm, " rm " );
68+ this ->func_map .AddFunction (it->first , GFunction, char_count + 1 );
6369 }
6470 }
6571
66- unordered_map <string, int > pIntTokens ;
72+ unordered_map <string, int > p_int_tokens ;
6773 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;
74+ p_int_tokens [this ->model ->m [" p" ].assertion_map [" p" ]->tokens [i]] = i;
6975
70- vector <string> pTokens = this ->model ->m [" p" ].assertion_map [" p" ]->tokens ;
76+ vector <string> p_tokens = this ->model ->m [" p" ].assertion_map [" p" ]->tokens ;
7177
72- vector <Effect> policyEffects;
73- vector <float > matcherResults;
78+ int policy_len = int (this ->model ->m [" p" ].assertion_map [" p" ]->policy .size ());
7479
75- int policyLen = int (this ->model ->m [" p" ].assertion_map [" p" ]->policy .size ());
80+ vector <Effect> policy_effects (policy_len, Effect :: Indeterminate);
81+ vector <float > matcher_results;
7682
77- if (policyLen != 0 ) {
83+ if (policy_len != 0 ) {
7884 if (this ->model ->m [" r" ].assertion_map [" r" ]->tokens .size () != this ->func_map .GetRLen ())
7985 return false ;
8086
8187 // TODO
82- for ( int i = 0 ; i < this -> model -> m [ " p " ]. assertion_map [ " p " ]-> policy . size () ; i++){
88+ for ( int i = 0 ; i < policy_len ; i++){
8389 // 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 ())
90+ vector<string> p_vals = this ->model ->m [" p" ].assertion_map [" p" ]->policy [i];
91+ if (this ->model ->m [" p" ].assertion_map [" p" ]->tokens .size () != p_vals .size ())
8692 return false ;
8793
8894 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);
95+ for (int j = 0 ; j < p_tokens .size () ; j++){
96+ int index = int (p_tokens [j].find (" _" ));
97+ string token = p_tokens [j].substr (index+1 );
98+ PushStringPropToObject (this ->func_map .scope , " p" , p_vals [j], token);
9399 }
94100
95- this ->func_map .Eval (expString);
101+ this ->func_map .Evaluate (exp_string);
102+
96103 // TODO
97104 // log.LogPrint("Result: ", result)
98-
99105 if (CheckType (this ->func_map .scope ) == Type :: Bool){
100106 bool result = GetBoolean (this ->func_map .scope );
101107 if (!result) {
102- policyEffects [i] = Effect :: Indeterminate;
108+ policy_effects [i] = Effect :: Indeterminate;
103109 continue ;
104110 }
105111 }
106112 else if (CheckType (this ->func_map .scope ) == Type :: Float){
107113 bool result = GetFloat (this ->func_map .scope );
108114 if (result == 0 ) {
109- policyEffects [i] = Effect :: Indeterminate;
115+ policy_effects [i] = Effect :: Indeterminate;
110116 continue ;
111117 } else
112- matcherResults [i] = result;
118+ matcher_results [i] = result;
113119 }
114120 else
115121 return false ;
116122
117- bool ok = pIntTokens .find (" p_eft" ) != pIntTokens .end ();
118- if (ok ) {
119- int j = pIntTokens [" p_eft" ];
120- string eft = pVals [j];
123+ bool is_p_eft = p_int_tokens .find (" p_eft" ) != p_int_tokens .end ();
124+ if (is_p_eft ) {
125+ int j = p_int_tokens [" p_eft" ];
126+ string eft = p_vals [j];
121127 if (eft == " allow" )
122- policyEffects [i] = Effect :: Allow;
128+ policy_effects [i] = Effect :: Allow;
123129 else if (eft == " deny" )
124- policyEffects [i] = Effect :: Deny;
130+ policy_effects [i] = Effect :: Deny;
125131 else
126- policyEffects [i] = Effect :: Indeterminate;
132+ policy_effects [i] = Effect :: Indeterminate;
127133 }
128134 else
129- policyEffects [i] = Effect :: Allow;
135+ policy_effects [i] = Effect :: Allow;
130136
131137 if (this ->model ->m [" e" ].assertion_map [" e" ]->value == " priority(p_eft) || deny" )
132138 break ;
133139 }
134140 } else {
135- this ->func_map .Eval (expString);
141+ bool isValid = this ->func_map .Evaluate (exp_string);
142+ if (!isValid)
143+ return false ;
136144 bool result = this ->func_map .GetBooleanResult ();
145+
137146 // TODO
138147 // log.LogPrint("Result: ", result)
139-
140148 if (result)
141- policyEffects[ 0 ] = Effect::Allow;
149+ policy_effects. push_back ( Effect::Allow) ;
142150 else
143- policyEffects[ 0 ] = Effect::Indeterminate;
151+ policy_effects. push_back ( Effect::Indeterminate) ;
144152 }
145153
146154 // TODO
147155 // log.LogPrint("Rule Results: ", policyEffects)
148156
149- bool result = this ->eft ->MergeEffects (this ->model ->m [" e" ].assertion_map [" e" ]->value , policyEffects, matcherResults );
150-
157+ bool result = this ->eft ->MergeEffects (this ->model ->m [" e" ].assertion_map [" e" ]->value , policy_effects, matcher_results );
158+
151159 return result;
152160}
153161
@@ -198,7 +206,7 @@ Enforcer* Enforcer :: NewEnforcer(Model* m, Adapter* adapter) {
198206
199207 e->Initialize ();
200208
201- if (e->adapter != NULL ) {
209+ if (e->adapter -> file_path != " " ) {
202210 e->LoadPolicy ();
203211 }
204212 return e;
@@ -344,7 +352,6 @@ void Enforcer :: ClearPolicy() {
344352void Enforcer :: LoadPolicy() {
345353 this ->model ->ClearPolicy ();
346354 this ->adapter ->LoadPolicy (this ->model );
347-
348355 this ->model ->PrintPolicy ();
349356
350357 if (this ->auto_build_role_links ) {
0 commit comments