Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added implementation and tests for UpdatePolicy API #106

Merged
merged 7 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,65 @@ on: [push, pull_request]

jobs:
linux:
name: "Linux Ubuntu 20.04"
name: "Linux Ubuntu 20.04 (GNU 9.3.0)"
runs-on: ubuntu-20.04
steps:
- name: Checkout
id: checkout
uses: actions/checkout@v2
- name: Building files
- name: Configuring CMake files
id: building-files
run: |
make
mkdir build && cd build && cmake ..
- name: Building library
id: building-lib
run: |
make library
cd build && cmake --build . --config Debug --target all -j 10 --
- name: Cleanup
id: cleanup
id: clean-up
run: |
make clean
rm -r build lib

windows:
name: "Windows 10 (MSVC 19.29)"
runs-on: windows-latest
steps:
- name: Checkout
id: checkout
uses: actions/checkout@v2
- name: Configuring CMake files
id: building-files
run: |
mkdir build
cd build
cmake ..
- name: Building library
id: building-lib
run: |
cd build
cmake --build . --config Release --target casbin -j 10 --
- name: Cleanup
id: clean-up
run: |
rm -r build
rm -r lib

macos:
name: "macOS Catalina 10.15"
name: "macOS Catalina 10.15 (AppleClang 12.0)"
runs-on: macos-latest
steps:
- name: Checkout
id: checkout
uses: actions/checkout@v2
- name: Building files
- name: Configuring CMake files
id: building-files
run: |
make
mkdir build && cd build && cmake ..
- name: Building library
id: building-lib
run: |
make library
cd build && cmake --build . --config Debug --target all -j 10 --
- name: Cleanup
id: cleanup
id: clean-up
run: |
make clean
rm -r build lib
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ MigrationBackup/
.idea/
*.iml
.vscode
.DS_Store

# CMake work directory
cmake-build/
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.16)
cmake_minimum_required(VERSION 3.19)

set(CMAKE_WARN_DEPRECATED ON)

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Casbin-CPP
Operating Systems | Availability status
----------------- | -------------------
Windows (VS C++) | :heavy_check_mark: Available
Linux and MacOS | :heavy_check_mark: Available
Linux | :heavy_check_mark: Available
macOS | :heavy_check_mark: Available


![casbin Logo](casbin-logo.png)
Expand Down
13 changes: 10 additions & 3 deletions casbin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

FILE(GLOB_RECURSE SC_FILES "*.cpp" "*.h")
FILE(GLOB_RECURSE SRC_FILES "*.cpp" "*.h")

add_library(casbin ${SC_FILES})
add_library(casbin ${SRC_FILES})
include_directories(${CMAKE_SOURCE_DIR}/casbin)

target_precompile_headers(casbin PUBLIC "pch.h")

set_target_properties(casbin PROPERTIES PREFIX "")
set_target_properties(casbin PROPERTIES SUFFIX ".o")
if(WIN32 OR MSVC)
set_target_properties(casbin PROPERTIES SUFFIX ".lib")
elseif(UNIX)
set_target_properties(casbin PROPERTIES SUFFIX ".a")
endif()
2 changes: 2 additions & 0 deletions casbin/casbin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
<ClInclude Include="model\model.h" />
<ClInclude Include="model\pch.h" />
<ClInclude Include="model\scope_config.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="persist.h" />
<ClInclude Include="persist\adapter.h" />
<ClInclude Include="persist\batch_adapter.h" />
Expand All @@ -286,6 +287,7 @@
<ClInclude Include="persist\pch.h" />
<ClInclude Include="persist\watcher.h" />
<ClInclude Include="persist\watcher_ex.h" />
<ClInclude Include="persist\watcher_update.h" />
<ClInclude Include="rbac.h" />
<ClInclude Include="rbac\default_role_manager.h" />
<ClInclude Include="rbac\pch.h" />
Expand Down
6 changes: 6 additions & 0 deletions casbin/casbin.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,12 @@
<ClInclude Include="log\Logger.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="persist\watcher_update.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include=".clang-format" />
Expand Down
53 changes: 44 additions & 9 deletions casbin/internal_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
#include "./util/util.h"
#include "./persist/watcher_ex.h"
#include "./exception/unsupported_operation_exception.h"
#include "./persist/watcher_update.h"

namespace casbin {

// addPolicy adds a rule to the current policy.
bool Enforcer :: addPolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule) {
bool Enforcer::addPolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule) {
bool rule_added = m_model->AddPolicy(sec, p_type, rule);
if(!rule_added)
return rule_added;
Expand Down Expand Up @@ -59,7 +60,7 @@ bool Enforcer :: addPolicy(const std::string& sec, const std::string& p_type, co
}

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

// removePolicy removes a rule from the current policy.
bool Enforcer :: removePolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule) {
bool Enforcer::removePolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule) {
bool rule_removed = m_model->RemovePolicy(sec, p_type, rule);
if(!rule_removed)
return rule_removed;
Expand Down Expand Up @@ -113,7 +114,7 @@ bool Enforcer :: removePolicy(const std::string& sec, const std::string& p_type,
}

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

// removeFilteredPolicy removes rules based on field filters from the current policy.
bool Enforcer :: removeFilteredPolicy(const std::string& sec, const std::string& p_type, int field_index, const std::vector<std::string>& field_values){
bool Enforcer::removeFilteredPolicy(const std::string& sec, const std::string& p_type, int field_index, const std::vector<std::string>& field_values){
std::pair<int, std::vector<std::vector<std::string>>> p = m_model->RemoveFilteredPolicy(sec, p_type, field_index, field_values);
bool rule_removed = p.first;
std::vector<std::vector<std::string>> effects = p.second;
Expand Down Expand Up @@ -166,12 +167,46 @@ bool Enforcer :: removeFilteredPolicy(const std::string& sec, const std::string&
return rule_removed;
}

bool Enforcer :: updatePolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& oldRule, const std::vector<std::string>& newRule) {
return true;
bool Enforcer::updatePolicy(const std::string& sec, const std::string& p_type, const std::vector<std::string>& oldRule, const std::vector<std::string>& newRule) {
bool is_rule_updated = m_model->UpdatePolicy(sec, p_type, oldRule, newRule);
if(!is_rule_updated)
return false;

if(sec == "g") {
this->BuildIncrementalRoleLinks(policy_remove, p_type, { oldRule });
this->BuildIncrementalRoleLinks(policy_add, p_type, { newRule });
}
if (m_watcher && m_auto_notify_watcher) {
if(IsInstanceOf<WatcherUpdatable>(m_watcher.get())) {
std::dynamic_pointer_cast<WatcherUpdatable>(m_watcher)->UpdateForUpdatePolicy(oldRule, newRule);
}
else {
m_watcher->Update();
}
}
return is_rule_updated;
}

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) {
return true;
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) {
bool is_rules_updated = m_model->UpdatePolicies(sec, p_type, oldRules, newRules);
if(!is_rules_updated)
return false;

if(sec == "g") {
this->BuildIncrementalRoleLinks(policy_remove, p_type, oldRules);
this->BuildIncrementalRoleLinks(policy_add, p_type, newRules);
}

if (m_watcher && m_auto_notify_watcher) {
if(IsInstanceOf<WatcherUpdatable>(m_watcher.get())) {
std::dynamic_pointer_cast<WatcherUpdatable>(m_watcher)->UpdateForUpdatePolicies(oldRules, newRules);
}
else {
m_watcher->Update();
}
}

return is_rules_updated;
}

} // namespace casbin
Expand Down
2 changes: 0 additions & 2 deletions casbin/ip_parser/parser/Print.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef IP_PARSER_PARSER_PRINT
#define IP_PARSER_PARSER_PRINT

#include <iostream>

#include "./IP.h"

namespace casbin {
Expand Down
2 changes: 2 additions & 0 deletions casbin/ip_parser/parser/pch.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#ifndef IPPARSER_PARSER_PCH
#define IPPARSER_PARSER_PCH

#include <iostream>

#endif
5 changes: 2 additions & 3 deletions casbin/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
#ifndef LOGGER_CPP
#define LOGGER_CPP

#include <iostream>
#include "log/Logger.h"
#include "log/log_util.h"
#include "./log/Logger.h"
#include "./log/log_util.h"

namespace casbin {

Expand Down
Loading