Skip to content

Commit 012f357

Browse files
committed
fix: change the enforcer interface.
Signed-off-by: ZipoChan <[email protected]>
1 parent bf6096a commit 012f357

File tree

5 files changed

+57
-85
lines changed

5 files changed

+57
-85
lines changed

casbin/enforcer.cpp

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ bool Enforcer :: enforce(string matcher, Scope scope) {
162162
/**
163163
* Enforcer is the default constructor.
164164
*/
165-
shared_ptr<Enforcer> Enforcer :: NewEnforcer() {
166-
shared_ptr<Enforcer> e = shared_ptr<Enforcer>(new Enforcer());
167-
return e;
165+
unique_ptr<Enforcer> Enforcer ::NewEnforcer() {
166+
unique_ptr<Enforcer> e = unique_ptr<Enforcer>(new Enforcer());
167+
return move(e);
168168
}
169169

170170
/**
@@ -173,8 +173,8 @@ shared_ptr<Enforcer> Enforcer :: NewEnforcer() {
173173
* @param model_path the path of the model file.
174174
* @param policyFile the path of the policy file.
175175
*/
176-
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFile) {
177-
return NewEnforcer(model_path, shared_ptr<FileAdapter>(FileAdapter :: NewAdapter(policyFile)));
176+
unique_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFile) {
177+
return move(NewEnforcer(model_path, shared_ptr<FileAdapter>(FileAdapter :: NewAdapter(policyFile))));
178178
}
179179

180180
/**
@@ -183,10 +183,10 @@ shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFil
183183
* @param model_path the path of the model file.
184184
* @param adapter the adapter.
185185
*/
186-
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, shared_ptr<Adapter> adapter) {
187-
shared_ptr<Enforcer> e = NewEnforcer(shared_ptr<Model>(Model :: NewModelFromFile(model_path)), adapter);
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);
188188
e->model_path = model_path;
189-
return e;
189+
return move(e);
190190
}
191191

192192
/**
@@ -195,8 +195,8 @@ shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, shared_ptr<Adapt
195195
* @param m the model.
196196
* @param adapter the adapter.
197197
*/
198-
shared_ptr<Enforcer> Enforcer :: NewEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {
199-
shared_ptr<Enforcer> e = shared_ptr<Enforcer>(new Enforcer());
198+
unique_ptr<Enforcer> Enforcer :: NewEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {
199+
unique_ptr<Enforcer> e = unique_ptr<Enforcer>(new Enforcer());
200200
e->adapter = adapter;
201201
e->watcher = NULL;
202202

@@ -209,25 +209,25 @@ shared_ptr<Enforcer> Enforcer :: NewEnforcer(shared_ptr<Model> m, shared_ptr<Ada
209209
if (e->adapter->file_path != "") {
210210
e->LoadPolicy();
211211
}
212-
return e;
212+
return move(e);
213213
}
214214

215215
/**
216216
* Enforcer initializes an enforcer with a model.
217217
*
218218
* @param m the model.
219219
*/
220-
shared_ptr<Enforcer> Enforcer :: NewEnforcer(shared_ptr<Model> m) {
221-
return NewEnforcer(m, NULL);
220+
unique_ptr<Enforcer> Enforcer ::NewEnforcer(shared_ptr<Model> m) {
221+
return move(NewEnforcer(m, NULL));
222222
}
223223

224224
/**
225225
* Enforcer initializes an enforcer with a model file.
226226
*
227227
* @param model_path the path of the model file.
228228
*/
229-
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path) {
230-
return NewEnforcer(model_path, "");
229+
unique_ptr<Enforcer> Enforcer ::NewEnforcer(string model_path) {
230+
return move(NewEnforcer(model_path, ""));
231231
}
232232

233233
/**
@@ -237,10 +237,10 @@ shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path) {
237237
* @param policyFile the path of the policy file.
238238
* @param enableLog whether to enable Casbin's log.
239239
*/
240-
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFile, bool enableLog) {
241-
shared_ptr<Enforcer> e = NewEnforcer(model_path, shared_ptr<FileAdapter>(FileAdapter :: NewAdapter(policyFile)));
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)));
242242
// e.EnableLog(enableLog);
243-
return e;
243+
return move(e);
244244
}
245245

246246

@@ -444,16 +444,6 @@ bool Enforcer :: Enforce(Scope scope) {
444444
return this->EnforceWithMatcher("", scope);
445445
}
446446

447-
// Enforce with three params, decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
448-
bool Enforcer::Enforce(string sub, string obj, string act) {
449-
return EnforceWithMatcher("", sub, obj, act);
450-
}
451-
452-
// Enforce with four params, decides whether a "subject" can access a "object" with the operation "action" in the domain "dom", input parameters are usually: (sub, dom, obj,act).
453-
bool Enforcer::Enforce(string sub, string dom, string obj, string act) {
454-
return EnforceWithMatcher("", sub, dom, obj, act);
455-
}
456-
457447
// Enforce with a vector param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
458448
bool Enforcer::Enforce(vector<string> params) {
459449
return this->EnforceWithMatcher("", params);
@@ -469,16 +459,6 @@ bool Enforcer :: EnforceWithMatcher(string matcher, Scope scope) {
469459
return this->enforce(matcher, scope);
470460
}
471461

472-
// Enforce with three params, decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
473-
bool Enforcer::EnforceWithMatcher(string matcher, string sub, string obj, string act) {
474-
return this->EnforceWithMatcher(matcher, { sub,obj,act });
475-
}
476-
477-
// Enforce with four params, decides whether a "subject" can access a "object" with the operation "action" in the domain "dom", input parameters are usually: (sub, dom, obj,act).
478-
bool Enforcer::EnforceWithMatcher(string matcher, string sub, string dom, string obj, string act) {
479-
return this->EnforceWithMatcher(matcher, { sub,dom,obj,act });
480-
}
481-
482462
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
483463
bool Enforcer::EnforceWithMatcher(string matcher, vector<string> params) {
484464
vector <string> r_tokens = this->model->m["r"].assertion_map["r"]->tokens;

casbin/enforcer.h

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,48 +50,48 @@ class Enforcer : public IEnforcer{
5050
/**
5151
* Enforcer is the default constructor.
5252
*/
53-
static shared_ptr<Enforcer> NewEnforcer();
53+
static unique_ptr<Enforcer> NewEnforcer();
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.
5858
* @param policyFile the path of the policy file.
5959
*/
60-
static shared_ptr<Enforcer> NewEnforcer(string model_path, string policyFile);
60+
static unique_ptr<Enforcer> NewEnforcer(string model_path, string policyFile);
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 shared_ptr<Enforcer> NewEnforcer(string model_path, shared_ptr<Adapter> adapter);
67+
static unique_ptr<Enforcer> NewEnforcer(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 shared_ptr<Enforcer> NewEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter);
74+
static unique_ptr<Enforcer> NewEnforcer(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 shared_ptr<Enforcer> NewEnforcer(shared_ptr<Model> m);
80+
static unique_ptr<Enforcer> NewEnforcer(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 shared_ptr<Enforcer> NewEnforcer(string model_path);
86+
static unique_ptr<Enforcer> NewEnforcer(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.
9191
* @param policyFile the path of the policy file.
9292
* @param enableLog whether to enable Casbin's log.
9393
*/
94-
static shared_ptr<Enforcer> NewEnforcer(string model_path, string policyFile, bool enableLog);
94+
static unique_ptr<Enforcer> NewEnforcer(string model_path, string policyFile, bool enableLog);
9595
// InitWithFile initializes an enforcer with a model file and a policy file.
9696
void InitWithFile(string model_path, string policyPath);
9797
// InitWithAdapter initializes an enforcer with a database adapter.
@@ -148,20 +148,12 @@ class Enforcer : public IEnforcer{
148148
void BuildIncrementalRoleLinks(policy_op op, string p_type, vector<vector<string>> rules);
149149
// Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
150150
bool Enforce(Scope scope);
151-
// Enforce with three params, decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
152-
bool Enforce(string sub, string obj, string act);
153-
// Enforce with four params, decides whether a "subject" can access a "object" with the operation "action" in the domain "dom", input parameters are usually: (sub, dom, obj,act).
154-
bool Enforce(string sub, string dom, string obj, string act);
155151
// Enforce with a vector param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
156152
bool Enforce(vector<string> params);
157153
// Enforce with a map param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
158154
bool Enforce(unordered_map<string,string> params);
159155
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
160156
bool EnforceWithMatcher(string matcher, Scope scope);
161-
// Enforce with three params, decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
162-
bool EnforceWithMatcher(string matcher, string sub, string obj, string act);
163-
// Enforce with four params, decides whether a "subject" can access a "object" with the operation "action" in the domain "dom", input parameters are usually: (sub, dom, obj,act).
164-
bool EnforceWithMatcher(string matcher, string sub, string dom, string obj, string act);
165157
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
166158
bool EnforceWithMatcher(string matcher, vector<string> params);
167159
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".

test/test_enforcer.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ namespace test_enforcer
1515
{
1616
public:
1717

18-
void TestEnforce(shared_ptr<Enforcer> e, string sub, string dom, string obj, string act, bool res) {
19-
Assert::AreEqual(res, e->Enforce(sub, dom, obj, act));
18+
void TestEnforce(unique_ptr<Enforcer>& e, string sub, string dom, string obj, string act, bool res) {
19+
Assert::AreEqual(res, e->Enforce({sub, dom, obj, act}));
2020
}
2121

22-
void TestEnforce(shared_ptr<Enforcer> e, string sub, string obj, string act, bool res) {
23-
Assert::AreEqual(res, e->Enforce(sub, obj, act));
22+
void TestEnforce(unique_ptr<Enforcer>& e, string sub, string obj, string act, bool res) {
23+
Assert::AreEqual(res, e->Enforce({sub, obj, act}));
2424
}
2525

26-
void TestEnforce(shared_ptr<Enforcer> e, vector<string> params, bool res) {
26+
void TestEnforce(unique_ptr<Enforcer>& e, vector<string> params, bool res) {
2727
Assert::AreEqual(res, e->Enforce(params));
2828
}
2929

30-
void TestEnforce(shared_ptr<Enforcer> e, unordered_map<string,string> params, bool res) {
30+
void TestEnforce(unique_ptr<Enforcer>& e, unordered_map<string,string> params, bool res) {
3131
Assert::AreEqual(res, e->Enforce(params));
3232
}
3333

@@ -36,7 +36,7 @@ namespace test_enforcer
3636

3737
string model = "../../examples/rbac_with_domains_model.conf";
3838
string policy = "../../examples/rbac_with_domains_policy.csv";
39-
shared_ptr<Enforcer> e = Enforcer::NewEnforcer(model, policy);
39+
unique_ptr<Enforcer> e = Enforcer::NewEnforcer(model, policy);
4040

4141
TestEnforce(e, "alice", "domain1", "data1", "read", true);
4242
TestEnforce(e, "alice", "domain1", "data1", "write", true);
@@ -51,7 +51,7 @@ namespace test_enforcer
5151
TEST_METHOD(TestThreeParams) {
5252
string model = "../../examples/basic_model_without_spaces.conf";
5353
string policy = "../../examples/basic_policy.csv";
54-
shared_ptr<Enforcer> e = Enforcer::NewEnforcer(model, policy);
54+
unique_ptr<Enforcer> e = Enforcer::NewEnforcer(model, policy);
5555

5656
TestEnforce(e, { "alice", "data1", "read" }, true);
5757
TestEnforce(e, { "alice", "data1", "write" }, false);
@@ -66,7 +66,7 @@ namespace test_enforcer
6666
TEST_METHOD(TestVectorParams) {
6767
string model = "../../examples/basic_model_without_spaces.conf";
6868
string policy = "../../examples/basic_policy.csv";
69-
shared_ptr<Enforcer> e = Enforcer::NewEnforcer(model, policy);
69+
unique_ptr<Enforcer> e = Enforcer::NewEnforcer(model, policy);
7070

7171
TestEnforce(e, { "alice", "data1", "read" }, true);
7272
TestEnforce(e, { "alice", "data1", "write" }, false);
@@ -81,7 +81,7 @@ namespace test_enforcer
8181
TEST_METHOD(TestMapParams) {
8282
string model = "../../examples/basic_model_without_spaces.conf";
8383
string policy = "../../examples/basic_policy.csv";
84-
shared_ptr<Enforcer> e = Enforcer::NewEnforcer(model, policy);
84+
unique_ptr<Enforcer> e = Enforcer::NewEnforcer(model, policy);
8585

8686
unordered_map<string, string> params = { {"sub","alice"},{"obj","data1"},{"act","read"} };
8787
TestEnforce(e, params, true);

0 commit comments

Comments
 (0)