From b0f7c79031fd3baf5cd2a726e2fe3aa7ba57de34 Mon Sep 17 00:00:00 2001 From: coding is so funny Date: Thu, 25 Feb 2021 14:49:25 +0800 Subject: [PATCH] fix: crash while initialize Enforcer without adapter bug (#78) * fix: crash while initialize Enforcer without adapter bug Signed-off-by: chris * fix: AddFunction is not working as expect Signed-off-by: chris * fix: remove ApplyFunctionMap Signed-off-by: chris --- casbin/enforcer.cpp | 10 +++++++--- casbin/enforcer.h | 4 +++- casbin/management_api.cpp | 2 +- casbin/model/function.cpp | 14 +++++++------- casbin/model/function.h | 4 ++-- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/casbin/enforcer.cpp b/casbin/enforcer.cpp index 18ab7efb..749d9aa1 100644 --- a/casbin/enforcer.cpp +++ b/casbin/enforcer.cpp @@ -39,7 +39,6 @@ bool Enforcer :: enforce(string matcher, Scope scope) { // }() this->func_map.scope = scope; - this->func_map.LoadFunctionMap(); if(!this->enabled) return true; @@ -69,6 +68,11 @@ bool Enforcer :: enforce(string matcher, Scope scope) { } } + // apply function map to current scope. + for(auto func: user_func_list){ + this->func_map.AddFunction(get<0>(func), get<1>(func), get<2>(func)); + } + unordered_map p_int_tokens; for(int i = 0 ; i < this->model->m["p"].assertion_map["p"]->tokens.size() ; i++) p_int_tokens[this->model->m["p"].assertion_map["p"]->tokens[i]] = i; @@ -78,7 +82,7 @@ bool Enforcer :: enforce(string matcher, Scope scope) { int policy_len = int(this->model->m["p"].assertion_map["p"]->policy.size()); vector policy_effects(policy_len, Effect :: Indeterminate); - vector matcher_results(policy_len); + vector matcher_results(policy_len, 0.0); if(policy_len != 0) { if(this->model->m["r"].assertion_map["r"]->tokens.size() != this->func_map.GetRLen()) @@ -200,7 +204,7 @@ Enforcer :: Enforcer(shared_ptr m, shared_ptr adapter) { this->Initialize(); - if (this->adapter->file_path != "") { + if (this->adapter && this->adapter->file_path != "") { this->LoadPolicy(); } } diff --git a/casbin/enforcer.h b/casbin/enforcer.h index 77c5b56f..0d0334e3 100644 --- a/casbin/enforcer.h +++ b/casbin/enforcer.h @@ -17,7 +17,8 @@ #ifndef CASBIN_CPP_ENFORCER #define CASBIN_CPP_ENFORCER -#include +#include +#include #include "./rbac/role_manager.h" #include "./model/function.h" #include "./enforcer_interface.h" @@ -30,6 +31,7 @@ class Enforcer : public IEnforcer{ string model_path; shared_ptr model; FunctionMap func_map; + vector > user_func_list; shared_ptr eft; shared_ptr adapter; diff --git a/casbin/management_api.cpp b/casbin/management_api.cpp index 00833475..14670a55 100644 --- a/casbin/management_api.cpp +++ b/casbin/management_api.cpp @@ -305,5 +305,5 @@ bool Enforcer :: RemoveFilteredNamedGroupingPolicy(string p_type, int field_inde // AddFunction adds a customized function. void Enforcer :: AddFunction(string name, Function function, Index nargs) { - this->func_map.AddFunction(name, function, nargs); + user_func_list.push_back(make_tuple(name, function, nargs)); } \ No newline at end of file diff --git a/casbin/model/function.cpp b/casbin/model/function.cpp index 55856fb5..426242ea 100644 --- a/casbin/model/function.cpp +++ b/casbin/model/function.cpp @@ -26,15 +26,15 @@ FunctionMap :: FunctionMap(){ } void FunctionMap :: ProcessFunctions(string expression){ - for(unordered_map :: iterator it = this->func_map.begin() ; it != this->func_map.end() ; it++){ - int index = int(expression.find((it->first)+"(")); + for(auto func: func_list){ + int index = int(expression.find(func+"(")); if(index != string::npos){ int close_index = int(expression.find(")", index)); - int start = index + int(((it->first)+"(").length()); + int start = index + int((func+"(").length()); string function_params = expression.substr(start, close_index-start); - FetchIdentifier(this->scope, it->first); + FetchIdentifier(this->scope, func); vector params = Split(function_params, ","); for(int i=0;iscope, f, func_name, nargs); + func_list.push_back(func_name); + PushFunction(scope, f, func_name, nargs); } void FunctionMap :: AddFunctionPropToR(string identifier, Function func, Index nargs){ @@ -121,4 +121,4 @@ void FunctionMap :: LoadFunctionMap() { AddFunction("keyMatch3", KeyMatch3, 2); AddFunction("regexMatch", RegexMatch, 2); AddFunction("ipMatch", IPMatch, 2); -} \ No newline at end of file +} diff --git a/casbin/model/function.h b/casbin/model/function.h index 1903a646..09ff254b 100644 --- a/casbin/model/function.h +++ b/casbin/model/function.h @@ -17,7 +17,7 @@ #ifndef CASBIN_CPP_MODEL_FUNCTION #define CASBIN_CPP_MODEL_FUNCTION -#include +#include #include "../util/built_in_functions.h" @@ -26,7 +26,7 @@ using namespace std; class FunctionMap { public: Scope scope; - unordered_map func_map; + list func_list; FunctionMap();