Skip to content

Commit

Permalink
feat: add more functions with convenient interface.
Browse files Browse the repository at this point in the history
Revert "fix:change enforce interface."

This reverts commit b732bf7.

fix:change the interface of enforcers.
  • Loading branch information
ZipoChan committed Jul 6, 2020
1 parent 59d5db1 commit 96572fa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
24 changes: 15 additions & 9 deletions casbin/enforcer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,23 +441,17 @@ void Enforcer :: BuildIncrementalRoleLinks(policy_op op, string p_type, vector<v

// Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
bool Enforcer :: Enforce(Scope scope) {
return this->enforce("", scope);
}

// Enforce with two params, decides whether a "subject" can do the operation "action", input parameters are usually: (sub, act).
bool Enforcer::Enforce(string sub, string act) {
vector<string> v = { sub, act };
return Enforce(v);
return this->EnforceWithMatcher("", scope);
}

// Enforce with three params, decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
bool Enforcer::Enforce(string sub, string obj, string act) {
return Enforce({sub,obj,act});
return EnforceWithMatcher("", sub, obj, act);
}

// 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).
bool Enforcer::Enforce(string sub, string dom, string obj, string act) {
return Enforce({ sub,dom,obj,act });
return EnforceWithMatcher("", sub, dom, obj, act);
}

// Enforce with a vector param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
Expand All @@ -474,6 +468,17 @@ bool Enforcer::Enforce(unordered_map<string, string> params) {
bool Enforcer :: EnforceWithMatcher(string matcher, Scope scope) {
return this->enforce(matcher, scope);
}

// Enforce with three params, decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
bool Enforcer::EnforceWithMatcher(string matcher, string sub, string obj, string act) {
return this->EnforceWithMatcher(matcher, { sub,obj,act });
}

// 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).
bool Enforcer::EnforceWithMatcher(string matcher, string sub, string dom, string obj, string act) {
return this->EnforceWithMatcher(matcher, { sub,dom,obj,act });
}

// 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 "".
bool Enforcer::EnforceWithMatcher(string matcher, vector<string> params) {
vector <string> r_tokens = this->model->m["r"].assertion_map["r"]->tokens;
Expand All @@ -493,6 +498,7 @@ bool Enforcer::EnforceWithMatcher(string matcher, vector<string> params) {

return this->enforce(matcher, scope);
}

// 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 "".
bool Enforcer::EnforceWithMatcher(string matcher, unordered_map<string, string> params) {
Scope scope = InitializeScope();
Expand Down
6 changes: 4 additions & 2 deletions casbin/enforcer.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ class Enforcer : public IEnforcer{
void BuildIncrementalRoleLinks(policy_op op, string p_type, vector<vector<string>> rules);
// Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
bool Enforce(Scope scope);
// Enforce with two params, decides whether a "subject" can do the operation "action", input parameters are usually: (sub, act).
bool Enforce(string sub, string act);
// Enforce with three params, decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
bool Enforce(string sub, string obj, string act);
// 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).
Expand All @@ -159,6 +157,10 @@ class Enforcer : public IEnforcer{
bool Enforce(unordered_map<string,string> params);
// 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 "".
bool EnforceWithMatcher(string matcher, Scope scope);
// Enforce with three params, decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
bool EnforceWithMatcher(string matcher, string sub, string obj, string act);
// 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).
bool EnforceWithMatcher(string matcher, string sub, string dom, string obj, string act);
// 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 "".
bool EnforceWithMatcher(string matcher, vector<string> params);
// 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 "".
Expand Down
15 changes: 0 additions & 15 deletions test/test_enforcer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ namespace test_enforcer
Assert::AreEqual(res, e->Enforce(sub, obj, act));
}

void TestEnforce(Enforcer* e, string sub, string act, bool res) {
Assert::AreEqual(res, e->Enforce(sub, act));
}

void TestEnforce(Enforcer* e, vector<string> params, bool res) {
Assert::AreEqual(res, e->Enforce(params));
}
Expand Down Expand Up @@ -90,17 +86,6 @@ namespace test_enforcer
TestEnforce(e, { "bob", "data2", "read" }, false);
TestEnforce(e, { "bob", "data2", "write" }, true);
}

TEST_METHOD(TestTwoParams) {
string model = filePath("../examples/basic_without_users_model.conf");
string policy = filePath("../examples/basic_without_users_policy.csv");
Enforcer* e = Enforcer::NewEnforcer(model, policy);

TestEnforce(e, "data1", "read", true);
TestEnforce(e, "data1", "write", false);
TestEnforce(e, "data2", "read", false);
TestEnforce(e, "data2", "write", true);
}

TEST_METHOD(TestVectorParams) {
string model = filePath("../examples/basic_model_without_spaces.conf");
Expand Down

0 comments on commit 96572fa

Please sign in to comment.