Skip to content

Commit 10febc6

Browse files
authored
Merge pull request #62 from divy9881/constructor_interface
fix: Add constructor interface to Enforcer, Adapters, RoleManager and Effector.
2 parents 2752d63 + 37baac3 commit 10febc6

30 files changed

+472
-496
lines changed

casbin/config/config.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ void Config :: ParseBuffer(istream* buf){
9797
* @param confName the path of the model file.
9898
* @return the constructor of Config.
9999
*/
100-
Config* Config :: NewConfig(string conf_name) {
101-
Config* c = new Config;
100+
shared_ptr<Config> Config :: NewConfig(string conf_name) {
101+
shared_ptr<Config> c(new Config);
102102
c->Parse(conf_name);
103103
return c;
104104
}
@@ -109,8 +109,8 @@ Config* Config :: NewConfig(string conf_name) {
109109
* @param text the model text.
110110
* @return the constructor of Config.
111111
*/
112-
Config* Config :: NewConfigFromText(string text) {
113-
Config *c = new Config;
112+
shared_ptr<Config> Config :: NewConfigFromText(string text) {
113+
shared_ptr<Config> c(new Config);
114114
stringstream stream(text);
115115
c->ParseBuffer(&stream);
116116
return c;

casbin/config/config.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef CASBIN_CPP_CONFIG_CONFIG
1818
#define CASBIN_CPP_CONFIG_CONFIG
1919

20+
#include <memory>
2021
#include <unordered_map>
2122
#include <mutex>
2223

@@ -51,15 +52,15 @@ class Config : public ConfigInterface {
5152
* @param confName the path of the model file.
5253
* @return the constructor of Config.
5354
*/
54-
static Config* NewConfig(string conf_name);
55+
static shared_ptr<Config> NewConfig(string conf_name);
5556

5657
/**
5758
* newConfigFromText create an empty configuration representation from text.
5859
*
5960
* @param text the model text.
6061
* @return the constructor of Config.
6162
*/
62-
static Config* NewConfigFromText(string text);
63+
static shared_ptr<Config> NewConfigFromText(string text);
6364

6465
bool GetBool(string key);
6566

casbin/effect/default_effector.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121
#include "./default_effector.h"
2222
#include "../exception/unsupported_operation_exception.h"
2323

24-
// NewDefaultEffector is the constructor for DefaultEffector.
25-
DefaultEffector* DefaultEffector :: NewDefaultEffector(){
26-
DefaultEffector* e = new DefaultEffector;
27-
return e;
28-
}
29-
3024
/**
3125
* MergeEffects merges all matching results collected by the enforcer into a single decision.
3226
*/

casbin/effect/default_effector.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
class DefaultEffector : public Effector{
2626
public:
2727

28-
// NewDefaultEffector is the constructor for DefaultEffector.
29-
static DefaultEffector* NewDefaultEffector();
30-
3128
/**
3229
* MergeEffects merges all matching results collected by the enforcer into a single decision.
3330
*/

casbin/enforcer.cpp

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ bool Enforcer :: enforce(string matcher, Scope scope) {
5454
exp_string = matcher;
5555

5656

57-
unordered_map <string, RoleManager*> rm_map;
57+
unordered_map <string, shared_ptr<RoleManager>> rm_map;
5858
bool ok = this->model->m.find("g") != this->model->m.end();
5959

6060
if(ok) {
61-
for(unordered_map <string, Assertion*> :: iterator it = this->model->m["g"].assertion_map.begin() ; it != this->model->m["g"].assertion_map.end() ; it++){
62-
RoleManager* rm = it->second->rm;
61+
for(unordered_map <string, shared_ptr<Assertion>> :: iterator it = this->model->m["g"].assertion_map.begin() ; it != this->model->m["g"].assertion_map.end() ; it++){
62+
shared_ptr<RoleManager> rm = it->second->rm;
6363
int char_count = int(count(it->second->value.begin(), it->second->value.end(), '_'));
6464
int index = int(exp_string.find((it->first)+"("));
6565
if(index != string::npos)
6666
exp_string.insert(index+(it->first+"(").length(), "rm, ");
67-
PushPointer(this->func_map.scope, (void *)rm, "rm");
67+
PushPointer(this->func_map.scope, (void *)rm.get(), "rm");
6868
this->func_map.AddFunction(it->first, GFunction, char_count + 1);
6969
}
7070
}
@@ -162,9 +162,7 @@ bool Enforcer :: enforce(string matcher, Scope scope) {
162162
/**
163163
* Enforcer is the default constructor.
164164
*/
165-
unique_ptr<Enforcer> Enforcer ::NewEnforcer() {
166-
unique_ptr<Enforcer> e = unique_ptr<Enforcer>(new Enforcer());
167-
return move(e);
165+
Enforcer ::Enforcer() {
168166
}
169167

170168
/**
@@ -173,8 +171,7 @@ unique_ptr<Enforcer> Enforcer ::NewEnforcer() {
173171
* @param model_path the path of the model file.
174172
* @param policyFile the path of the policy file.
175173
*/
176-
unique_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFile) {
177-
return move(NewEnforcer(model_path, shared_ptr<FileAdapter>(FileAdapter :: NewAdapter(policyFile))));
174+
Enforcer :: Enforcer(string model_path, string policy_file): Enforcer(model_path, shared_ptr<FileAdapter>(new FileAdapter(policy_file))) {
178175
}
179176

180177
/**
@@ -183,10 +180,8 @@ unique_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFil
183180
* @param model_path the path of the model file.
184181
* @param adapter the adapter.
185182
*/
186-
unique_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, shared_ptr<Adapter> adapter) {
187-
unique_ptr<Enforcer> e = NewEnforcer(shared_ptr<Model>(Model :: NewModelFromFile(model_path)), adapter);
188-
e->model_path = model_path;
189-
return move(e);
183+
Enforcer :: Enforcer(string model_path, shared_ptr<Adapter> adapter): Enforcer(shared_ptr<Model>(new Model(model_path)), adapter) {
184+
this->model_path = model_path;
190185
}
191186

192187
/**
@@ -195,39 +190,35 @@ unique_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, shared_ptr<Adapt
195190
* @param m the model.
196191
* @param adapter the adapter.
197192
*/
198-
unique_ptr<Enforcer> Enforcer :: NewEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {
199-
unique_ptr<Enforcer> e = unique_ptr<Enforcer>(new Enforcer());
200-
e->adapter = adapter;
201-
e->watcher = NULL;
193+
Enforcer :: Enforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {
194+
this->adapter = adapter;
195+
this->watcher = NULL;
202196

203-
e->model = m;
204-
e->model->PrintModel();
205-
e->func_map.LoadFunctionMap();
197+
this->model = m;
198+
this->model->PrintModel();
199+
this->func_map.LoadFunctionMap();
206200

207-
e->Initialize();
201+
this->Initialize();
208202

209-
if (e->adapter->file_path != "") {
210-
e->LoadPolicy();
203+
if (this->adapter->file_path != "") {
204+
this->LoadPolicy();
211205
}
212-
return move(e);
213206
}
214207

215208
/**
216209
* Enforcer initializes an enforcer with a model.
217210
*
218211
* @param m the model.
219212
*/
220-
unique_ptr<Enforcer> Enforcer ::NewEnforcer(shared_ptr<Model> m) {
221-
return move(NewEnforcer(m, NULL));
213+
Enforcer ::Enforcer(shared_ptr<Model> m): Enforcer(m, NULL) {
222214
}
223215

224216
/**
225217
* Enforcer initializes an enforcer with a model file.
226218
*
227219
* @param model_path the path of the model file.
228220
*/
229-
unique_ptr<Enforcer> Enforcer ::NewEnforcer(string model_path) {
230-
return move(NewEnforcer(model_path, ""));
221+
Enforcer ::Enforcer(string model_path): Enforcer(model_path, "") {
231222
}
232223

233224
/**
@@ -237,16 +228,14 @@ unique_ptr<Enforcer> Enforcer ::NewEnforcer(string model_path) {
237228
* @param policyFile the path of the policy file.
238229
* @param enableLog whether to enable Casbin's log.
239230
*/
240-
unique_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFile, bool enableLog) {
241-
unique_ptr<Enforcer> e = NewEnforcer(model_path, shared_ptr<FileAdapter>(FileAdapter :: NewAdapter(policyFile)));
242-
// e.EnableLog(enableLog);
243-
return move(e);
231+
Enforcer :: Enforcer(string model_path, string policy_file, bool enable_log): Enforcer(model_path, shared_ptr<FileAdapter>(new FileAdapter(policy_file))) {
232+
// e.EnableLog(enable_log);
244233
}
245234

246235

247236
// InitWithFile initializes an enforcer with a model file and a policy file.
248-
void Enforcer :: InitWithFile(string model_path, string policyPath) {
249-
shared_ptr<Adapter> a = shared_ptr<FileAdapter>(FileAdapter::NewAdapter(policyPath));
237+
void Enforcer :: InitWithFile(string model_path, string policy_path) {
238+
shared_ptr<Adapter> a = shared_ptr<FileAdapter>(new FileAdapter(policy_path));
250239
this->InitWithAdapter(model_path, a);
251240
}
252241

@@ -275,8 +264,8 @@ void Enforcer :: InitWithModelAndAdapter(shared_ptr<Model> m, shared_ptr<Adapter
275264
}
276265

277266
void Enforcer :: Initialize() {
278-
this->rm = shared_ptr<DefaultRoleManager>(DefaultRoleManager :: NewRoleManager(10));
279-
this->eft = shared_ptr<DefaultEffector>(DefaultEffector :: NewDefaultEffector());
267+
this->rm = shared_ptr<DefaultRoleManager>(new DefaultRoleManager(10));
268+
this->eft = shared_ptr<DefaultEffector>(new DefaultEffector());
280269
this->watcher = NULL;
281270

282271
this->enabled = true;
@@ -350,7 +339,7 @@ void Enforcer :: ClearPolicy() {
350339

351340
// LoadPolicy reloads the policy from file/database.
352341
void Enforcer :: LoadPolicy() {
353-
this->model->ClearPolicy();
342+
this->ClearPolicy();
354343
this->adapter->LoadPolicy(this->model.get());
355344
this->model->PrintPolicy();
356345

@@ -362,18 +351,17 @@ void Enforcer :: LoadPolicy() {
362351
//LoadFilteredPolicy reloads a filtered policy from file/database.
363352
template<typename Filter>
364353
void Enforcer :: LoadFilteredPolicy(Filter filter) {
365-
this->model->ClearPolicy();
354+
this->ClearPolicy();
366355

367-
FilteredAdapter* filteredAdapter;
356+
shared_ptr<FilteredAdapter> filtered_adapter;
368357

369358
if (this->adapter->IsFiltered()) {
370-
void* adapter = this->adapter.get();
371-
filteredAdapter = (FilteredAdapter*)adapter;
359+
filtered_adapter = dynamic_pointer_cast<FilteredAdapter>(this->adapter);
372360
}
373361
else
374362
throw CasbinAdapterException("filtered policies are not supported by this adapter");
375363

376-
filteredAdapter->LoadFilteredPolicy(this->model, filter);
364+
filtered_adapter->LoadFilteredPolicy(this->model, filter);
377365

378366
this->model->PrintPolicy();
379367
if(this->auto_build_role_links)
@@ -431,12 +419,12 @@ void Enforcer :: EnableAutoBuildRoleLinks(bool auto_build_role_links) {
431419
void Enforcer :: BuildRoleLinks() {
432420
this->rm->Clear();
433421

434-
this->model->BuildRoleLinks(this->rm.get());
422+
this->model->BuildRoleLinks(this->rm);
435423
}
436424

437425
// BuildIncrementalRoleLinks provides incremental build the role inheritance relations.
438426
void Enforcer :: BuildIncrementalRoleLinks(policy_op op, string p_type, vector<vector<string>> rules) {
439-
return this->model->BuildIncrementalRoleLinks(this->rm.get(), op, "g", p_type, rules);
427+
return this->model->BuildIncrementalRoleLinks(this->rm, op, "g", p_type, rules);
440428
}
441429

442430
// Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).

casbin/enforcer.h

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,50 +50,50 @@ class Enforcer : public IEnforcer{
5050
/**
5151
* Enforcer is the default constructor.
5252
*/
53-
static unique_ptr<Enforcer> NewEnforcer();
53+
Enforcer();
5454
/**
5555
* Enforcer initializes an enforcer with a model file and a policy file.
5656
*
5757
* @param model_path the path of the model file.
58-
* @param policyFile the path of the policy file.
58+
* @param policy_file the path of the policy file.
5959
*/
60-
static unique_ptr<Enforcer> NewEnforcer(string model_path, string policyFile);
60+
Enforcer(string model_path, string policy_file);
6161
/**
6262
* Enforcer initializes an enforcer with a database adapter.
6363
*
6464
* @param model_path the path of the model file.
6565
* @param adapter the adapter.
6666
*/
67-
static unique_ptr<Enforcer> NewEnforcer(string model_path, shared_ptr<Adapter> adapter);
67+
Enforcer(string model_path, shared_ptr<Adapter> adapter);
6868
/**
6969
* Enforcer initializes an enforcer with a model and a database adapter.
7070
*
7171
* @param m the model.
7272
* @param adapter the adapter.
7373
*/
74-
static unique_ptr<Enforcer> NewEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter);
74+
Enforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter);
7575
/**
7676
* Enforcer initializes an enforcer with a model.
7777
*
7878
* @param m the model.
7979
*/
80-
static unique_ptr<Enforcer> NewEnforcer(shared_ptr<Model> m);
80+
Enforcer(shared_ptr<Model> m);
8181
/**
8282
* Enforcer initializes an enforcer with a model file.
8383
*
8484
* @param model_path the path of the model file.
8585
*/
86-
static unique_ptr<Enforcer> NewEnforcer(string model_path);
86+
Enforcer(string model_path);
8787
/**
8888
* Enforcer initializes an enforcer with a model file, a policy file and an enable log flag.
8989
*
9090
* @param model_path the path of the model file.
91-
* @param policyFile the path of the policy file.
92-
* @param enableLog whether to enable Casbin's log.
91+
* @param policy_file the path of the policy file.
92+
* @param enable_log whether to enable Casbin's log.
9393
*/
94-
static unique_ptr<Enforcer> NewEnforcer(string model_path, string policyFile, bool enableLog);
94+
Enforcer(string model_path, string policy_file, bool enable_log);
9595
// InitWithFile initializes an enforcer with a model file and a policy file.
96-
void InitWithFile(string model_path, string policyPath);
96+
void InitWithFile(string model_path, string policy_path);
9797
// InitWithAdapter initializes an enforcer with a database adapter.
9898
void InitWithAdapter(string model_path, shared_ptr<Adapter> adapter);
9999
// InitWithModelAndAdapter initializes an enforcer with a model and a database adapter.
@@ -161,45 +161,45 @@ class Enforcer : public IEnforcer{
161161

162162
/*Management API member functions.*/
163163
vector<string> GetAllSubjects();
164-
vector<string> GetAllNamedSubjects(string ptype);
164+
vector<string> GetAllNamedSubjects(string p_type);
165165
vector<string> GetAllObjects();
166-
vector<string> GetAllNamedObjects(string ptype);
166+
vector<string> GetAllNamedObjects(string p_type);
167167
vector<string> GetAllActions();
168-
vector<string> GetAllNamedActions(string ptype);
168+
vector<string> GetAllNamedActions(string p_type);
169169
vector<string> GetAllRoles();
170-
vector<string> GetAllNamedRoles(string ptype);
170+
vector<string> GetAllNamedRoles(string p_type);
171171
vector<vector<string>> GetPolicy();
172172
vector<vector<string>> GetFilteredPolicy(int field_index, vector<string> field_values);
173-
vector<vector<string>> GetNamedPolicy(string ptype);
174-
vector<vector<string>> GetFilteredNamedPolicy(string ptype, int field_index, vector<string> field_values);
173+
vector<vector<string>> GetNamedPolicy(string p_type);
174+
vector<vector<string>> GetFilteredNamedPolicy(string p_type, int field_index, vector<string> field_values);
175175
vector<vector<string>> GetGroupingPolicy();
176176
vector<vector<string>> GetFilteredGroupingPolicy(int field_index, vector<string> field_values);
177-
vector<vector<string>> GetNamedGroupingPolicy(string ptype);
178-
vector<vector<string>> GetFilteredNamedGroupingPolicy(string ptype, int field_index, vector<string> field_values);
177+
vector<vector<string>> GetNamedGroupingPolicy(string p_type);
178+
vector<vector<string>> GetFilteredNamedGroupingPolicy(string p_type, int field_index, vector<string> field_values);
179179
bool HasPolicy(vector<string> params);
180-
bool HasNamedPolicy(string ptype, vector<string> params);
180+
bool HasNamedPolicy(string p_type, vector<string> params);
181181
bool AddPolicy(vector<string> params);
182182
bool AddPolicies(vector<vector<string>> rules);
183-
bool AddNamedPolicy(string ptype, vector<string> params);
183+
bool AddNamedPolicy(string p_type, vector<string> params);
184184
bool AddNamedPolicies(string p_type, vector<vector<string>> rules);
185185
bool RemovePolicy(vector<string> params);
186186
bool RemovePolicies(vector<vector<string>> rules);
187187
bool RemoveFilteredPolicy(int field_index, vector<string> field_values);
188-
bool RemoveNamedPolicy(string ptype, vector<string> params);
188+
bool RemoveNamedPolicy(string p_type, vector<string> params);
189189
bool RemoveNamedPolicies(string p_type, vector<vector<string>> rules);
190-
bool RemoveFilteredNamedPolicy(string ptype, int field_index, vector<string> field_values);
190+
bool RemoveFilteredNamedPolicy(string p_type, int field_index, vector<string> field_values);
191191
bool HasGroupingPolicy(vector<string> params);
192-
bool HasNamedGroupingPolicy(string ptype, vector<string> params);
192+
bool HasNamedGroupingPolicy(string p_type, vector<string> params);
193193
bool AddGroupingPolicy(vector<string> params);
194194
bool AddGroupingPolicies(vector<vector<string>> rules);
195-
bool AddNamedGroupingPolicy(string ptype, vector<string> params);
195+
bool AddNamedGroupingPolicy(string p_type, vector<string> params);
196196
bool AddNamedGroupingPolicies(string p_type, vector<vector<string>> rules);
197197
bool RemoveGroupingPolicy(vector<string> params);
198198
bool RemoveGroupingPolicies(vector<vector<string>> rules);
199199
bool RemoveFilteredGroupingPolicy(int field_index, vector<string> field_values);
200-
bool RemoveNamedGroupingPolicy(string ptype, vector<string> params);
200+
bool RemoveNamedGroupingPolicy(string p_type, vector<string> params);
201201
bool RemoveNamedGroupingPolicies(string p_type, vector<vector<string>> rules);
202-
bool RemoveFilteredNamedGroupingPolicy(string ptype, int field_index, vector<string> field_values);
202+
bool RemoveFilteredNamedGroupingPolicy(string p_type, int field_index, vector<string> field_values);
203203
void AddFunction(string name, Function function, Index nargs);
204204

205205
/*RBAC API member functions.*/
@@ -223,11 +223,11 @@ class Enforcer : public IEnforcer{
223223
bool DeletePermission(vector<string> permission);
224224

225225
/* Internal API member functions */
226-
bool addPolicy(string sec, string ptype, vector<string> rule);
226+
bool addPolicy(string sec, string p_type, vector<string> rule);
227227
bool addPolicies(string sec, string p_type, vector<vector<string>> rules);
228-
bool removePolicy(string sec , string ptype , vector<string> rule);
228+
bool removePolicy(string sec , string p_type , vector<string> rule);
229229
bool removePolicies(string sec, string p_type, vector<vector<string>> rules);
230-
bool removeFilteredPolicy(string sec , string ptype , int fieldIndex , vector<string> fieldValues);
230+
bool removeFilteredPolicy(string sec , string p_type , int field_index , vector<string> field_values);
231231

232232
/* RBAC API with domains.*/
233233
vector<string> GetUsersForRoleInDomain(string name, string domain = {});

0 commit comments

Comments
 (0)