Skip to content

Commit

Permalink
feat: improve the synced enforcer like Go casbin (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiujiarui authored May 1, 2024
1 parent eb321d7 commit abf9092
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
77 changes: 76 additions & 1 deletion casbin/enforcer_synced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,81 @@ bool SyncedEnforcer ::RemoveFilteredNamedGroupingPolicy(const std::string& ptype
return Enforcer::RemoveFilteredNamedGroupingPolicy(ptype, fieldIndex, fieldValues);
}

// GetAllActions gets the list of actions that show up in the current policy.
std::vector<std::string> SyncedEnforcer::GetAllActions() {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::GetAllActions();
}

// GetFilteredPolicy gets all the authorization rules in the policy, field filters can be specified.
PoliciesValues SyncedEnforcer::GetFilteredPolicy(int fieldIndex, std::vector<std::string> fieldValues) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::GetFilteredPolicy(fieldIndex, fieldValues);
}

// EnforceExWithMatcher use a custom matcher and explain enforcement by informing matched rules.
bool SyncedEnforcer::SyncedEnforceExWithMatcher(const std::string& matcher, std::shared_ptr<IEvaluator> evalator, std::vector<std::string>& explain) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceExWithMatcher(matcher, evalator, explain);
}

bool SyncedEnforcer::SyncedEnforceExWithMatcher(const std::string& matcher, const DataList& params, std::vector<std::string>& explain) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceExWithMatcher(matcher, params, explain);
}

bool SyncedEnforcer::SyncedEnforceExWithMatcher(const std::string& matcher, const DataVector& params, std::vector<std::string>& explain) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceExWithMatcher(matcher, params, explain);
}

bool SyncedEnforcer::SyncedEnforceExWithMatcher(const std::string& matcher, const DataMap& params, std::vector<std::string>& explain) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceExWithMatcher(matcher, params, explain);
}

// EnforceEx explain enforcement by informing matched rules.
bool SyncedEnforcer::SyncedEnforceEx(std::shared_ptr<IEvaluator> evalator, std::vector<std::string>& explain) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceEx(evalator, explain);
}

bool SyncedEnforcer::SyncedEnforceEx(const DataList& params, std::vector<std::string>& explain) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceEx(params, explain);
}

bool SyncedEnforcer::SyncedEnforceEx(const DataVector& params, std::vector<std::string>& explain) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceEx(params, explain);
}

bool SyncedEnforcer::SyncedEnforceEx(const DataMap& params, std::vector<std::string>& explain) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceEx(params, explain);
}

// 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 SyncedEnforcer::SyncedEnforceWithMatcher(const std::string& matcher, std::shared_ptr<IEvaluator> evalator) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceWithMatcher(matcher, evalator);
}

bool SyncedEnforcer::SyncedEnforceWithMatcher(const std::string& matcher, const DataList& params) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceWithMatcher(matcher, params);
}

bool SyncedEnforcer::SyncedEnforceWithMatcher(const std::string& matcher, const DataVector& params) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceWithMatcher(matcher, params);
}

bool SyncedEnforcer::SyncedEnforceWithMatcher(const std::string& matcher, const DataMap& params) {
std::unique_lock<std::shared_mutex> lock(policyMutex);
return Enforcer::EnforceWithMatcher(matcher, params);
}

} // namespace casbin

#endif // ENFORCER_SYNCED_CPP
#endif // ENFORCER_SYNCED_CPP
36 changes: 35 additions & 1 deletion include/casbin/enforcer_synced.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,42 @@ class SyncedEnforcer : public Enforcer {

// RemoveFilteredNamedGroupingPolicy removes a role inheritance rule from the current named policy, field filters can be specified.
bool RemoveFilteredNamedGroupingPolicy(const std::string& ptype, int fieldIndex, const std::vector<std::string>& fieldValues) override;

// GetAllActions gets the list of actions that show up in the current policy.

std::vector<std::string> GetAllActions() override;

// GetFilteredPolicy gets all the authorization rules in the policy, field filters can be specified.
PoliciesValues GetFilteredPolicy(int fieldIndex, std::vector<std::string> fieldValues);

// EnforceExWithMatcher use a custom matcher and explain enforcement by informing matched rules.
bool SyncedEnforceExWithMatcher(const std::string& matcher, std::shared_ptr<IEvaluator> evalator, std::vector<std::string>& explain);

bool SyncedEnforceExWithMatcher(const std::string& matcher, const DataList& params, std::vector<std::string>& explain);

bool SyncedEnforceExWithMatcher(const std::string& matcher, const DataVector& params, std::vector<std::string>& explain);

bool SyncedEnforceExWithMatcher(const std::string& matcher, const DataMap& params, std::vector<std::string>& explain);

// EnforceEx explain enforcement by informing matched rules.
bool SyncedEnforceEx(std::shared_ptr<IEvaluator> evalator, std::vector<std::string>& explain);

bool SyncedEnforceEx(const DataList& params, std::vector<std::string>& explain);

bool SyncedEnforceEx(const DataVector& params, std::vector<std::string>& explain);

bool SyncedEnforceEx(const DataMap& params, std::vector<std::string>& explain);

// 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
bool SyncedEnforceWithMatcher(const std::string& matcher, std::shared_ptr<IEvaluator> evalator);

bool SyncedEnforceWithMatcher(const std::string& matcher, const DataList& params);

bool SyncedEnforceWithMatcher(const std::string& matcher, const DataVector& params);

bool SyncedEnforceWithMatcher(const std::string& matcher, const DataMap& params);
};

} // namespace casbin

#endif
#endif
24 changes: 24 additions & 0 deletions tests/enforcer_synced_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,28 @@ TEST(TestEnforcerSynced, TestMultiThreadBatchEnforce) {
EXPECT_EQ(e.IsAutoLoadingRunning(), false);
}

void testSyncedEnforcerGetPolicy(casbin::SyncedEnforcer& e, PoliciesVector expected) {
auto myRes = e.GetPolicy();
PoliciesVector actual;
for (auto it = myRes.begin(); it != myRes.end(); ++it) {
actual.push_back(*it);
}
std::sort(actual.begin(), actual.end());
std::sort(expected.begin(), expected.end());
ASSERT_EQ(expected, actual);
}

TEST(TestSyncedEnforcer, GetPolicy) {
casbin::SyncedEnforcer e(basic_model_path, basic_policy_path);

PoliciesVector expected_policy = {
{"alice", "data1", "read"},
{"bob", "data2", "write"},
};

testSyncedEnforcerGetPolicy(e, expected_policy);
}



} // namespace

0 comments on commit abf9092

Please sign in to comment.