Skip to content

Commit ae0f347

Browse files
committed
feat: Missing implementations in internal API
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent 1f7d811 commit ae0f347

File tree

4 files changed

+229
-93
lines changed

4 files changed

+229
-93
lines changed

casbin/internal_api.cpp

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
#include "./util/util.h"
2626
#include "./persist/watcher_ex.h"
2727
#include "./exception/unsupported_operation_exception.h"
28+
#include "./persist/watcher_update.h"
2829

2930
namespace casbin {
3031

3132
// addPolicy adds a rule to the current policy.
32-
bool Enforcer :: addPolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule) {
33+
bool Enforcer::addPolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule) {
3334
bool rule_added = m_model->AddPolicy(sec, p_type, rule);
3435
if(!rule_added)
3536
return rule_added;
@@ -59,7 +60,7 @@ bool Enforcer :: addPolicy(const std::string& sec, const std::string& p_type, co
5960
}
6061

6162
// addPolicies adds rules to the current policy.
62-
bool Enforcer :: addPolicies(const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules) {
63+
bool Enforcer::addPolicies(const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules) {
6364
bool rules_added = m_model->AddPolicies(sec, p_type, rules);
6465
if (!rules_added)
6566
return rules_added;
@@ -83,7 +84,7 @@ bool Enforcer :: addPolicies(const std::string& sec, const std::string& p_type,
8384
}
8485

8586
// removePolicy removes a rule from the current policy.
86-
bool Enforcer :: removePolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule) {
87+
bool Enforcer::removePolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule) {
8788
bool rule_removed = m_model->RemovePolicy(sec, p_type, rule);
8889
if(!rule_removed)
8990
return rule_removed;
@@ -113,7 +114,7 @@ bool Enforcer :: removePolicy(const std::string& sec, const std::string& p_type,
113114
}
114115

115116
// removePolicies removes rules from the current policy.
116-
bool Enforcer :: removePolicies(const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules) {
117+
bool Enforcer::removePolicies(const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules) {
117118
bool rules_removed = m_model->AddPolicies(sec, p_type, rules);
118119
if (!rules_removed)
119120
return rules_removed;
@@ -136,7 +137,7 @@ bool Enforcer :: removePolicies(const std::string& sec, const std::string& p_typ
136137
}
137138

138139
// removeFilteredPolicy removes rules based on field filters from the current policy.
139-
bool Enforcer :: removeFilteredPolicy(const std::string& sec, const std::string& p_type, int field_index, const std::vector<std::string>& field_values){
140+
bool Enforcer::removeFilteredPolicy(const std::string& sec, const std::string& p_type, int field_index, const std::vector<std::string>& field_values){
140141
std::pair<int, std::vector<std::vector<std::string>>> p = m_model->RemoveFilteredPolicy(sec, p_type, field_index, field_values);
141142
bool rule_removed = p.first;
142143
std::vector<std::vector<std::string>> effects = p.second;
@@ -166,13 +167,46 @@ bool Enforcer :: removeFilteredPolicy(const std::string& sec, const std::string&
166167
return rule_removed;
167168
}
168169

169-
bool Enforcer :: updatePolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& oldRule, const std::vector<std::string>& newRule) {
170-
bool is_model_policy_updated = m_model->UpdatePolicy(sec, p_type, oldRule, newRule);
171-
return true;
170+
bool Enforcer::updatePolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& oldRule, const std::vector<std::string>& newRule) {
171+
bool is_rule_updated = m_model->UpdatePolicy(sec, p_type, oldRule, newRule);
172+
if(!is_rule_updated)
173+
return false;
174+
175+
if(sec == "g") {
176+
this->BuildIncrementalRoleLinks(policy_remove, p_type, { oldRule });
177+
this->BuildIncrementalRoleLinks(policy_add, p_type, { newRule });
178+
}
179+
if (m_watcher && m_auto_notify_watcher) {
180+
if(IsInstanceOf<WatcherUpdatable>(m_watcher.get())) {
181+
std::dynamic_pointer_cast<WatcherUpdatable>(m_watcher)->UpdateForUpdatePolicy(oldRule, newRule);
182+
}
183+
else {
184+
m_watcher->Update();
185+
}
186+
}
187+
return is_rule_updated;
172188
}
173189

174-
bool Enforcer :: updatePolicies(const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& p1, const std::vector<std::vector<std::string>>& p2) {
175-
return true;
190+
bool Enforcer::updatePolicies(const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& oldRules, const std::vector<std::vector<std::string>>& newRules) {
191+
bool is_rules_updated = m_model->UpdatePolicies(sec, p_type, oldRules, newRules);
192+
if(!is_rules_updated)
193+
return false;
194+
195+
if(sec == "g") {
196+
this->BuildIncrementalRoleLinks(policy_remove, p_type, oldRules);
197+
this->BuildIncrementalRoleLinks(policy_add, p_type, newRules);
198+
}
199+
200+
if (m_watcher && m_auto_notify_watcher) {
201+
if(IsInstanceOf<WatcherUpdatable>(m_watcher.get())) {
202+
std::dynamic_pointer_cast<WatcherUpdatable>(m_watcher)->UpdateForUpdatePolicies(oldRules, newRules);
203+
}
204+
else {
205+
m_watcher->Update();
206+
}
207+
}
208+
209+
return is_rules_updated;
176210
}
177211

178212
} // namespace casbin

0 commit comments

Comments
 (0)