Skip to content

Commit 7143cc4

Browse files
authored
Merge pull request #106 from EmperorYP7/api-completion
feat: Added implementation and tests for UpdatePolicy API
2 parents c4a0e11 + cf260ae commit 7143cc4

File tree

18 files changed

+446
-193
lines changed

18 files changed

+446
-193
lines changed

.github/workflows/ci.yml

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,65 @@ on: [push, pull_request]
1919

2020
jobs:
2121
linux:
22-
name: "Linux Ubuntu 20.04"
22+
name: "Linux Ubuntu 20.04 (GNU 9.3.0)"
2323
runs-on: ubuntu-20.04
2424
steps:
2525
- name: Checkout
2626
id: checkout
2727
uses: actions/checkout@v2
28-
- name: Building files
28+
- name: Configuring CMake files
2929
id: building-files
3030
run: |
31-
make
31+
mkdir build && cd build && cmake ..
3232
- name: Building library
3333
id: building-lib
3434
run: |
35-
make library
35+
cd build && cmake --build . --config Debug --target all -j 10 --
3636
- name: Cleanup
37-
id: cleanup
37+
id: clean-up
3838
run: |
39-
make clean
39+
rm -r build lib
40+
41+
windows:
42+
name: "Windows 10 (MSVC 19.29)"
43+
runs-on: windows-latest
44+
steps:
45+
- name: Checkout
46+
id: checkout
47+
uses: actions/checkout@v2
48+
- name: Configuring CMake files
49+
id: building-files
50+
run: |
51+
mkdir build
52+
cd build
53+
cmake ..
54+
- name: Building library
55+
id: building-lib
56+
run: |
57+
cd build
58+
cmake --build . --config Release --target casbin -j 10 --
59+
- name: Cleanup
60+
id: clean-up
61+
run: |
62+
rm -r build
63+
rm -r lib
4064
4165
macos:
42-
name: "macOS Catalina 10.15"
66+
name: "macOS Catalina 10.15 (AppleClang 12.0)"
4367
runs-on: macos-latest
4468
steps:
4569
- name: Checkout
4670
id: checkout
4771
uses: actions/checkout@v2
48-
- name: Building files
72+
- name: Configuring CMake files
4973
id: building-files
5074
run: |
51-
make
75+
mkdir build && cd build && cmake ..
5276
- name: Building library
5377
id: building-lib
5478
run: |
55-
make library
79+
cd build && cmake --build . --config Debug --target all -j 10 --
5680
- name: Cleanup
57-
id: cleanup
81+
id: clean-up
5882
run: |
59-
make clean
83+
rm -r build lib

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ MigrationBackup/
354354
.idea/
355355
*.iml
356356
.vscode
357+
.DS_Store
357358

358359
# CMake work directory
359360
cmake-build/

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.16)
1+
cmake_minimum_required(VERSION 3.19)
22

33
set(CMAKE_WARN_DEPRECATED ON)
44

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Casbin-CPP
1313
Operating Systems | Availability status
1414
----------------- | -------------------
1515
Windows (VS C++) | :heavy_check_mark: Available
16-
Linux and MacOS | :heavy_check_mark: Available
16+
Linux | :heavy_check_mark: Available
17+
macOS | :heavy_check_mark: Available
1718

1819

1920
![casbin Logo](casbin-logo.png)

casbin/CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
22

3-
FILE(GLOB_RECURSE SC_FILES "*.cpp" "*.h")
3+
FILE(GLOB_RECURSE SRC_FILES "*.cpp" "*.h")
44

5-
add_library(casbin ${SC_FILES})
5+
add_library(casbin ${SRC_FILES})
6+
include_directories(${CMAKE_SOURCE_DIR}/casbin)
7+
8+
target_precompile_headers(casbin PUBLIC "pch.h")
69

710
set_target_properties(casbin PROPERTIES PREFIX "")
8-
set_target_properties(casbin PROPERTIES SUFFIX ".o")
11+
if(WIN32 OR MSVC)
12+
set_target_properties(casbin PROPERTIES SUFFIX ".lib")
13+
elseif(UNIX)
14+
set_target_properties(casbin PROPERTIES SUFFIX ".a")
15+
endif()

casbin/casbin.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
<ClInclude Include="model\model.h" />
274274
<ClInclude Include="model\pch.h" />
275275
<ClInclude Include="model\scope_config.h" />
276+
<ClInclude Include="pch.h" />
276277
<ClInclude Include="persist.h" />
277278
<ClInclude Include="persist\adapter.h" />
278279
<ClInclude Include="persist\batch_adapter.h" />
@@ -286,6 +287,7 @@
286287
<ClInclude Include="persist\pch.h" />
287288
<ClInclude Include="persist\watcher.h" />
288289
<ClInclude Include="persist\watcher_ex.h" />
290+
<ClInclude Include="persist\watcher_update.h" />
289291
<ClInclude Include="rbac.h" />
290292
<ClInclude Include="rbac\default_role_manager.h" />
291293
<ClInclude Include="rbac\pch.h" />

casbin/casbin.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,12 @@
494494
<ClInclude Include="log\Logger.h">
495495
<Filter>Header Files</Filter>
496496
</ClInclude>
497+
<ClInclude Include="pch.h">
498+
<Filter>Header Files</Filter>
499+
</ClInclude>
500+
<ClInclude Include="persist\watcher_update.h">
501+
<Filter>Header Files</Filter>
502+
</ClInclude>
497503
</ItemGroup>
498504
<ItemGroup>
499505
<None Include=".clang-format" />

casbin/internal_api.cpp

Lines changed: 44 additions & 9 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,12 +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-
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;
171188
}
172189

173-
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) {
174-
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;
175210
}
176211

177212
} // namespace casbin

casbin/ip_parser/parser/Print.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef IP_PARSER_PARSER_PRINT
22
#define IP_PARSER_PARSER_PRINT
33

4-
#include <iostream>
5-
64
#include "./IP.h"
75

86
namespace casbin {

casbin/ip_parser/parser/pch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#ifndef IPPARSER_PARSER_PCH
22
#define IPPARSER_PARSER_PCH
33

4+
#include <iostream>
5+
46
#endif

0 commit comments

Comments
 (0)