diff --git a/casbin/enforcer.cpp b/casbin/enforcer.cpp index a93ce95d..6205bc72 100644 --- a/casbin/enforcer.cpp +++ b/casbin/enforcer.cpp @@ -441,23 +441,17 @@ void Enforcer :: BuildIncrementalRoleLinks(policy_op op, string p_type, vectorenforce("", 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 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). @@ -474,6 +468,17 @@ bool Enforcer::Enforce(unordered_map 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 params) { vector r_tokens = this->model->m["r"].assertion_map["r"]->tokens; @@ -493,6 +498,7 @@ bool Enforcer::EnforceWithMatcher(string matcher, vector 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 params) { Scope scope = InitializeScope(); diff --git a/casbin/enforcer.h b/casbin/enforcer.h index c8a73b7f..789c7e3f 100644 --- a/casbin/enforcer.h +++ b/casbin/enforcer.h @@ -147,8 +147,6 @@ class Enforcer : public IEnforcer{ void BuildIncrementalRoleLinks(policy_op op, string p_type, vector> 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). @@ -159,6 +157,10 @@ class Enforcer : public IEnforcer{ bool Enforce(unordered_map 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 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 "". diff --git a/test/test_enforcer.cpp b/test/test_enforcer.cpp index ab7909d6..6cdddff8 100644 --- a/test/test_enforcer.cpp +++ b/test/test_enforcer.cpp @@ -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 params, bool res) { Assert::AreEqual(res, e->Enforce(params)); } @@ -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");