Skip to content

Commit

Permalink
fix: crash while initialize Enforcer without adapter bug (#78)
Browse files Browse the repository at this point in the history
* fix: crash while initialize Enforcer without adapter bug

Signed-off-by: chris <bigcat26@gmail.com>

* fix: AddFunction is not working as expect

Signed-off-by: chris <bigcat26@gmail.com>

* fix: remove ApplyFunctionMap

Signed-off-by: chris <bigcat26@gmail.com>
  • Loading branch information
bigcat26 authored Feb 25, 2021
1 parent 1f2aa3d commit b0f7c79
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
10 changes: 7 additions & 3 deletions casbin/enforcer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <string, int> 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;
Expand All @@ -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 <Effect> policy_effects(policy_len, Effect :: Indeterminate);
vector <float> matcher_results(policy_len);
vector <float> matcher_results(policy_len, 0.0);

if(policy_len != 0) {
if(this->model->m["r"].assertion_map["r"]->tokens.size() != this->func_map.GetRLen())
Expand Down Expand Up @@ -200,7 +204,7 @@ Enforcer :: Enforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {

this->Initialize();

if (this->adapter->file_path != "") {
if (this->adapter && this->adapter->file_path != "") {
this->LoadPolicy();
}
}
Expand Down
4 changes: 3 additions & 1 deletion casbin/enforcer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#ifndef CASBIN_CPP_ENFORCER
#define CASBIN_CPP_ENFORCER

#include<memory>
#include <tuple>
#include <vector>
#include "./rbac/role_manager.h"
#include "./model/function.h"
#include "./enforcer_interface.h"
Expand All @@ -30,6 +31,7 @@ class Enforcer : public IEnforcer{
string model_path;
shared_ptr<Model> model;
FunctionMap func_map;
vector <tuple<string, Function, Index>> user_func_list;
shared_ptr<Effector> eft;

shared_ptr<Adapter> adapter;
Expand Down
2 changes: 1 addition & 1 deletion casbin/management_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
14 changes: 7 additions & 7 deletions casbin/model/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ FunctionMap :: FunctionMap(){
}

void FunctionMap :: ProcessFunctions(string expression){
for(unordered_map<string, Function> :: 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<string> params = Split(function_params, ",");

for(int i=0;i<params.size();i++){
Expand Down Expand Up @@ -70,8 +70,8 @@ bool FunctionMap :: GetBooleanResult(){

// AddFunction adds an expression function.
void FunctionMap :: AddFunction(string func_name, Function f, Index nargs) {
func_map[func_name] = f;
PushFunction(this->scope, 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){
Expand Down Expand Up @@ -121,4 +121,4 @@ void FunctionMap :: LoadFunctionMap() {
AddFunction("keyMatch3", KeyMatch3, 2);
AddFunction("regexMatch", RegexMatch, 2);
AddFunction("ipMatch", IPMatch, 2);
}
}
4 changes: 2 additions & 2 deletions casbin/model/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef CASBIN_CPP_MODEL_FUNCTION
#define CASBIN_CPP_MODEL_FUNCTION

#include <unordered_map>
#include <list>

#include "../util/built_in_functions.h"

Expand All @@ -26,7 +26,7 @@ using namespace std;
class FunctionMap {
public:
Scope scope;
unordered_map <string, Function> func_map;
list <string> func_list;

FunctionMap();

Expand Down

0 comments on commit b0f7c79

Please sign in to comment.