@@ -59,6 +59,20 @@ casbin::Scope InitializeParamsWithDomains(const std::string& sub, const std::str
5959 return scope;
6060}
6161
62+ casbin::Scope InitializeParamsWithJson (std::shared_ptr<nlohmann::json> sub, std::string obj, std::string act) {
63+ casbin::Scope scope = casbin::InitializeScope ();
64+ casbin::PushObject (scope, " r" );
65+
66+ casbin::PushStringPropToObject (scope, " r" , obj, " obj" );
67+ casbin::PushStringPropToObject (scope, " r" , act, " act" );
68+
69+ casbin::PushObject (scope, " sub" );
70+ casbin::PushObjectPropFromJson (scope, *sub, " sub" );
71+ casbin::PushObjectPropToObject (scope, " r" , " sub" );
72+
73+ return scope;
74+ }
75+
6276void TestEnforce (casbin::Enforcer& e, casbin::Scope& scope, bool res) {
6377 ASSERT_EQ (res, e.Enforce (scope));
6478}
@@ -523,6 +537,44 @@ TEST(TestModelEnforcer, TestABACModelWithJson) {
523537 ASSERT_TRUE (e.Enforce (listParams));
524538 ASSERT_TRUE (e.Enforce (vectorParams));
525539}
540+
541+ std::shared_ptr<nlohmann::json> newTestSubject (std::string name, int age) {
542+ nlohmann::json sub = {{" Name" , name}, {" Age" , age}};
543+ return std::make_shared<nlohmann::json>(sub);
544+ }
545+
546+ TEST (TestModelEnforcer, TestABACPolicyWithJson) {
547+ casbin::Enforcer e (abac_rule_model_path, abac_rule_policy_path);
548+
549+ auto sub1 = newTestSubject (" alice" , 16 );
550+ auto sub2 = newTestSubject (" alice" , 20 );
551+ auto sub3 = newTestSubject (" alice" , 65 );
552+
553+ auto scope = InitializeParamsWithJson (sub1, " /data1" , " read" );
554+ TestEnforce (e, scope, false );
555+ scope = InitializeParamsWithJson (sub1, " /data2" , " read" );
556+ TestEnforce (e, scope, false );
557+ scope = InitializeParamsWithJson (sub1, " /data1" , " write" );
558+ TestEnforce (e, scope, false );
559+ scope = InitializeParamsWithJson (sub1, " /data2" , " write" );
560+ TestEnforce (e, scope, true );
561+ scope = InitializeParamsWithJson (sub2, " /data1" , " read" );
562+ TestEnforce (e, scope, true );
563+ scope = InitializeParamsWithJson (sub2, " /data2" , " read" );
564+ TestEnforce (e, scope, false );
565+ scope = InitializeParamsWithJson (sub2, " /data1" , " write" );
566+ TestEnforce (e, scope, false );
567+ scope = InitializeParamsWithJson (sub2, " /data2" , " write" );
568+ TestEnforce (e, scope, true );
569+ scope = InitializeParamsWithJson (sub3, " /data1" , " read" );
570+ TestEnforce (e, scope, true );
571+ scope = InitializeParamsWithJson (sub3, " /data2" , " read" );
572+ TestEnforce (e, scope, false );
573+ scope = InitializeParamsWithJson (sub3, " /data1" , " write" );
574+ TestEnforce (e, scope, false );
575+ scope = InitializeParamsWithJson (sub3, " /data2" , " write" );
576+ TestEnforce (e, scope, false );
577+ }
526578/*
527579type testCustomRoleManager struct {}
528580
0 commit comments