From a9a577fe824ae4544d5162c726d577b691c3ff52 Mon Sep 17 00:00:00 2001 From: ljyofme <2477354249@qq.com> Date: Sun, 12 May 2024 18:17:51 +0800 Subject: [PATCH] feat: add the adapter of sqlpp11 --- .github/workflows/ci.yml | 133 ++++++++++++++++++++++++++ CMakeLists.txt | 30 ++++++ MANIFEST.in | 4 + Makefile | 22 +++++ dependencies/CMakeLists.txt | 96 +++++++++++++++++++ examples/model.conf | 14 +++ examples/policy.csv | 2 + include/sql_casbin.h | 181 ++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 37 ++++++++ src/main.cpp | 45 +++++++++ src/sqlpp11_adapter.cpp | 166 +++++++++++++++++++++++++++++++++ src/test.cpp | 60 ++++++++++++ 12 files changed, 790 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 CMakeLists.txt create mode 100644 MANIFEST.in create mode 100644 Makefile create mode 100644 dependencies/CMakeLists.txt create mode 100644 examples/model.conf create mode 100644 examples/policy.csv create mode 100644 include/sql_casbin.h create mode 100644 src/CMakeLists.txt create mode 100644 src/main.cpp create mode 100644 src/sqlpp11_adapter.cpp create mode 100644 src/test.cpp diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..ebc1616 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,133 @@ +name: CI + +on: [push, pull_request] + +jobs: + linux: + name: "Ubuntu Latest (GNU 9.3.0)" + runs-on: ubuntu-latest + services: + mysql: + image: mysql:5.7 + ports: + - 3306:3306 + options: >- + --health-cmd="mysqladmin ping -uroot -p125463" + --health-interval=10s + --health-timeout=5s + --health-retries=3 + env: + MYSQL_ROOT_PASSWORD: 125463 + MYSQL_DATABASE: casbin + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Wait for MySQL to be ready + run: | + while ! mysqladmin ping -h "127.0.0.1" -uroot -p125463 --silent; do + echo 'Waiting for MySQL...' + sleep 1 + done + + - name: Configuring CMake files + run: | + mkdir build + cd build + cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTING=ON + + - name: Building library + run: | + cd build + cmake --build . --config Release --target all -j 10 -- + + - name: Tests + env: + MYSQL_HOST: 127.0.0.1 + MYSQL_PORT: 3306 + MYSQL_USER: root + MYSQL_PASSWORD: 125463 + MYSQL_DATABASE: casbin + run: | + cd build + ctest -j10 -C Release -T test --output-on-failure + + - name: Cleanup + run: | + rm -r build + + windows: + name: "Windows Latest (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 Debug --target casbin sqlpp11::sqlpp11 sqlpp11::mysql nlohmann_json::nlohmann_json GTest::gtest_main GTest::gtest 11sql_test -j 10 -- + + - name: Running Tests + env: + MYSQL_HOST: 127.0.0.1 + MYSQL_PORT: 3306 + MYSQL_USER: root + MYSQL_PASSWORD: 125463 + MYSQL_DATABASE: casbin + run: | + cd build + ctest -j10 -C Release -T test --output-on-failure + + macos: + name: "macOS Latest (AppleClang 12.0)" + runs-on: macos-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Install MySQL + run: | + brew install mysql + brew install mysql-connector-c + export MYSQL_ROOT_DIR=$(brew --prefix mysql) + export MYSQL_INCLUDE_DIR=$MYSQL_ROOT_DIR/include + export MYSQL_LIB_DIR=$MYSQL_ROOT_DIR/lib + + - name: Configuring CMake files + env: + MYSQL_INCLUDE_DIR: ${{ env.MYSQL_INCLUDE_DIR }} + MYSQL_LIBRARY: ${{ env.MYSQL_LIB_DIR }}/libmysqlclient.dylib + run: | + mkdir build + cd build + cmake .. -DCMAKE_INSTALL_PREFIX=~/local -DMYSQL_INCLUDE_DIR=${MYSQL_INCLUDE_DIR} -DMYSQL_LIBRARY=${MYSQL_LIBRARY} -DENABLE_TESTING=ON + + - name: Building library + run: | + cd build + cmake --build . --config Debug --target all install -j 10 -- + + - name: Tests + env: + MYSQL_HOST: 127.0.0.1 + MYSQL_PORT: 3306 + MYSQL_USER: root + MYSQL_PASSWORD: 125463 + MYSQL_DATABASE: casbin + run: | + cd build + ctest -j10 -C Release -T test --output-on-failure + + - name: Cleanup + run: | + rm -r build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..72ce5be --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,30 @@ +# Copyright 2021 The casbin Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +### Preamble ### +cmake_minimum_required(VERSION 3.14) +project(11sql VERSION 0.1 LANGUAGES CXX) + +##### Project wide setup #### +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +add_definitions(-DNOMINMAX) + +### Dependencies ### +add_subdirectory(dependencies) + +### Main Targets ### +add_definitions(-Dmodelpath=${CMAKE_SOURCE_DIR}) +enable_testing() +add_subdirectory(src) \ No newline at end of file diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..1dd4dd5 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,4 @@ +include README.md LICENSE +graft cmake +graft include +global-include CMakeLists.txt *.cmake \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6ad75e2 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ + +CXX = g++ + + +CXXFLAGS = -Wall -std=c++11 + +TARGET = test + + +OBJS = sqlpp11_adapter.o test.o + +$(TARGET): $(OBJS) + $(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS) + +sqlpp11_adapter.o: sqlpp11_adapter.cpp sqlpp11_adapter.h + $(CXX) $(CXXFLAGS) -c sqlpp11_adapter.cpp + +test.o: test.cpp sqlpp11_adapter.h + $(CXX) $(CXXFLAGS) -c test.cpp + +clean: + rm -f $(TARGET) $(OBJS) diff --git a/dependencies/CMakeLists.txt b/dependencies/CMakeLists.txt new file mode 100644 index 0000000..e2853b6 --- /dev/null +++ b/dependencies/CMakeLists.txt @@ -0,0 +1,96 @@ +# Copyright 2021 The casbin Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#sqlpp11 +include(FetchContent) + +FetchContent_Declare(sqlpp11 + GIT_REPOSITORY https://github.com/rbock/sqlpp11 + GIT_TAG origin/main +) + +# Configure the project here as needed +set(BUILD_MYSQL_CONNECTOR ON) +# set(BUILD_MARIADB_CONNECTOR ON) +# set(BUILD_POSTGRESQL_CONNECTOR ON) +# set(BUILD_SQLITE3_CONNECTOR ON) +# set(BUILD_SQLCIPHER_CONNECTOR ON) + +# set(USE_SYSTEM_DATE ON) + +FetchContent_MakeAvailable(sqlpp11) + +#casbin +include(FetchContent) + +FetchContent_Declare( + casbin + GIT_REPOSITORY https://github.com/casbin/casbin-cpp.git + GIT_TAG v1.59.0 +) + +set(CASBIN_BUILD_TEST OFF) # If you don't need to build tests for casbin +set(CASBIN_BUILD_BENCHMARK OFF) # If you don't need to build benchmarks for casbin +set(CASBIN_BUILD_BINDINGS OFF) # If you don't need language bindings provided by casbin +set(CASBIN_BUILD_PYTHON_BINDINGS OFF) # If you don't need python bindings provided by casbin + +# Making casbin and its targets accessible to our project +FetchContent_MakeAvailable(casbin) + +FetchContent_GetProperties(casbin) + +# If casbin wasn't populated, then manually populate it +if(NOT casbin_POPULATED) + FetchContent_Populate(casbin) + add_subdirectory(${casbin_SOURCE_DIR} ${casbin_BINARY_DIR}) +endif() + + +#gtest +include(FetchContent) + +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG v1.14.0 + DOWNLOAD_EXTRACT_TIMESTAMP FALSE +) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + +FetchContent_MakeAvailable(googletest) +FetchContent_GetProperties(googletest) + +# Populating manually, if not done automatically +if(NOT googletest_POPULATED) + FetchContent_Populate(googletest) + add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR}) +endif() + + +#json +include(FetchContent) + +set(JSON_Install ON) + +FetchContent_Declare( + json + GIT_REPOSITORY https://github.com/nlohmann/json.git + GIT_TAG v3.11.2 + DOWNLOAD_EXTRACT_TIMESTAMP FALSE +) + +FetchContent_MakeAvailable(json) + diff --git a/examples/model.conf b/examples/model.conf new file mode 100644 index 0000000..71159e3 --- /dev/null +++ b/examples/model.conf @@ -0,0 +1,14 @@ +[request_definition] +r = sub, obj, act + +[policy_definition] +p = sub, obj, act + +[role_definition] +g = _, _ + +[policy_effect] +e = some(where (p.eft == allow)) + +[matchers] +m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act \ No newline at end of file diff --git a/examples/policy.csv b/examples/policy.csv new file mode 100644 index 0000000..8f18a7c --- /dev/null +++ b/examples/policy.csv @@ -0,0 +1,2 @@ +p alice data1 read +p bob data2 write \ No newline at end of file diff --git a/include/sql_casbin.h b/include/sql_casbin.h new file mode 100644 index 0000000..56fff4d --- /dev/null +++ b/include/sql_casbin.h @@ -0,0 +1,181 @@ +/* + * Copyright 2020 The casbin Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +// generated by ddl2cpp casbin.sql ./casbin SQLPP11 + +#include +#include +#include + +namespace SQLPP11 +{ + namespace CasbinRule_ + { + struct Id + { + struct _alias_t + { + static constexpr const char _literal[] = "id"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T id; + T& operator()() { return id; } + const T& operator()() const { return id; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct Ptype + { + struct _alias_t + { + static constexpr const char _literal[] = "ptype"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T ptype; + T& operator()() { return ptype; } + const T& operator()() const { return ptype; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct V0 + { + struct _alias_t + { + static constexpr const char _literal[] = "v0"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T v0; + T& operator()() { return v0; } + const T& operator()() const { return v0; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct V1 + { + struct _alias_t + { + static constexpr const char _literal[] = "v1"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T v1; + T& operator()() { return v1; } + const T& operator()() const { return v1; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct V2 + { + struct _alias_t + { + static constexpr const char _literal[] = "v2"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T v2; + T& operator()() { return v2; } + const T& operator()() const { return v2; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct V3 + { + struct _alias_t + { + static constexpr const char _literal[] = "v3"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T v3; + T& operator()() { return v3; } + const T& operator()() const { return v3; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct V4 + { + struct _alias_t + { + static constexpr const char _literal[] = "v4"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T v4; + T& operator()() { return v4; } + const T& operator()() const { return v4; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct V5 + { + struct _alias_t + { + static constexpr const char _literal[] = "v5"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T v5; + T& operator()() { return v5; } + const T& operator()() const { return v5; } + }; + }; + using _traits = sqlpp::make_traits; + }; + } // namespace CasbinRule_ + + struct CasbinRule : sqlpp::table_t + { + struct _alias_t + { + static constexpr const char _literal[] = "casbin_rule"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T casbinRule; + T& operator()() { return casbinRule; } + const T& operator()() const { return casbinRule; } + }; + }; + }; +} // namespace SQLPP11 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..f557e19 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,37 @@ +# Copyright 2021 The casbin Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +add_executable(11sql) +target_link_libraries(11sql PRIVATE casbin) + +set(11sql_INCLUDE_DIR ${casbin_SOURCE_DIR}/include ../include ) +target_include_directories(11sql PRIVATE ${11sql_INCLUDE_DIR}) + +target_link_libraries(11sql PRIVATE casbin sqlpp11::sqlpp11 sqlpp11::mysql nlohmann_json::nlohmann_json ) +target_sources(11sql + PRIVATE + sqlpp11_adapter.cpp + main.cpp) + + + +add_executable(11sql_test) +target_include_directories(11sql_test PRIVATE ${11sql_INCLUDE_DIR}) +target_link_libraries(11sql_test PRIVATE casbin sqlpp11::sqlpp11 sqlpp11::mysql nlohmann_json::nlohmann_json GTest::gtest_main GTest::gtest) +target_sources(11sql_test + PRIVATE + test.cpp sqlpp11_adapter.cpp +) +include(GoogleTest) +gtest_discover_tests(11sql_test) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..73624f0 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,45 @@ + +#include +#include +#include "sqlpp11/sqlpp11.h" +#include "sqlpp11_adapter.cpp"' +#include "casbin/casbin.h" +#include "casbin/model/model.h" +using namespace sqlpp; +using namespace std; +using namespace sqlpp::mysql; +using namespace casbin; + + +int main() + +{ + // std::shared_ptr db = connect_database(); + Sqlpp11Adapter ss; + std::shared_ptr m = casbin::Model::NewModel(); + m->LoadModel("/model.conf"); + + // m->AddPolicy("p", "p", { "alice", "data1", "write" }); + + ss.LoadPolicy(m); + + PoliciesValues p_policies = m->GetPolicy("p", "p"); int p = p_policies.size(); + std::cout << "p policies:" << std::endl; + for (const auto& policy : p_policies) { + for (const auto& field : policy) { + std::cout << field << " "; + + } + std::cout< f = { "alice", "data1", "write" };s= m->HasPolicy("p","p", f); + std::cout << f.size() << std::endl; + std::cout< +#include +#include "casbin/persist/adapter.h" +#include "casbin/util/util.h" +#include "sqlpp11/mysql/mysql.h" +#include "sqlpp11/sqlpp11.h" +#include "sql_casbin.h" +#include // MySQL's own driver, needs to be loaded manually +#include "sql_casbin.h" +#define STRINGIFY_IMPL(x) #x +#define STRINGIFY(x) STRINGIFY_IMPL(x) + +using namespace casbin; +using namespace sqlpp; +using namespace std; +using namespace mysql; +using namespace SQLPP11; +static const std::string relative_path = STRINGIFY(modelpath); +class Sqlpp11Adapter { +public: + sqlpp::mysql::connection db; + sqlpp::mysql::connection connect(string host, unsigned int port, string user, string password, string database) { + sqlpp::mysql::connection_config config; + // Database address + config.host = host; + // Port number + config.port = port; + // Database login account + config.user = user; + // Database login password + config.password = password; + // The database to operate + config.database = database; + config.debug = true; + sqlpp::mysql::connection db(config); + return db; + } + + void NewAdapter(string host,unsigned int port, string user, string password, string database) { + db=connect(host,port,user,password,database); + // If the database does not exist, create the database and table + createTable(); + } + // Load policy from database + void LoadPolicy(const std::shared_ptr& model) { + SQLPP11::CasbinRule casbin_rule{}; + + try { + for (const auto& row : db(sqlpp::select(all_of(casbin_rule)).from(casbin_rule).unconditionally())) { + std::string ptype = row.ptype.is_null() ? "" : row.ptype.value(); + std::string v0 = row.v0.is_null() ? "" : row.v0.value(); + std::string v1 = row.v1.is_null() ? "" : row.v1.value(); + std::string v2 = row.v2.is_null() ? "" : row.v2.value(); + std::string v3 = row.v3.is_null() ? "" : row.v3.value(); + std::string v4 = row.v4.is_null() ? "" : row.v4.value(); + std::string v5 = row.v5.is_null() ? "" : row.v5.value(); + + std::string lineText = ptype; + if (!row.v0.is_null() && !v0.empty()) lineText += ", " + v0; + if (!row.v1.is_null() && !v1.empty()) lineText += ", " + v1; + if (!row.v2.is_null() && !v2.empty()) lineText += ", " + v2; + if (!row.v3.is_null() && !v3.empty()) lineText += ", " + v3; + if (!row.v4.is_null() && !v4.empty()) lineText += ", " + v4; + if (!row.v5.is_null() && !v5.empty()) lineText += ", " + v5; + casbin::LoadPolicyLine(lineText, model); + } + } + catch (const sqlpp::exception& e) { + std::cerr << "Error loading policy: " << e.what() << std::endl; + } + } + + void SavePolicy(const std::shared_ptr& model) { + SQLPP11::CasbinRule casbin_rule{}; + std::string v0; + std::string v1; + std::string v2; + std::string v3; + std::string v4; + std::string v5; + + PoliciesValues p_policies = model->GetPolicy("p", "p"); + for (const auto& policy : p_policies) { + if (policy.size() > 0 && !policy[0].empty()) v0 = policy[0]; + if (policy.size() > 1 && !policy[1].empty()) v1 = policy[1]; + if (policy.size() > 2 && !policy[2].empty()) v2 = policy[2]; + if (policy.size() > 3 && !policy[3].empty()) v3 = policy[3]; + if (policy.size() > 4 && !policy[4].empty()) v4 = policy[4]; + if (policy.size() > 5 && !policy[5].empty()) v5 = policy[5]; + + // Query whether the data already exists in the database + auto rows = db(select(all_of(casbin_rule)).from(casbin_rule).where(casbin_rule.ptype == "p" and casbin_rule.v0 == v0 and casbin_rule.v1 == v1 and casbin_rule.v2 == v2 and casbin_rule.v3 == v3 and casbin_rule.v4 == v4 and casbin_rule.v5 == v5)); + if (rows.empty()) { + // If it does not exist, insert it + db(insert_into(casbin_rule).set(casbin_rule.ptype = "p", casbin_rule.v0 = v0, casbin_rule.v1 = v1, casbin_rule.v2 = v2, casbin_rule.v3 = v3, casbin_rule.v4 = v4, casbin_rule.v5 = v5)); + } + } + + PoliciesValues g_policies = model->GetPolicy("g", "g"); + for (const auto& policy : g_policies) { + if (policy.size() > 0 && !policy[0].empty()) v0 = policy[0]; + if (policy.size() > 1 && !policy[1].empty()) v1 = policy[1]; + if (policy.size() > 2 && !policy[2].empty()) v2 = policy[2]; + if (policy.size() > 3 && !policy[3].empty()) v3 = policy[3]; + if (policy.size() > 4 && !policy[4].empty()) v4 = policy[4]; + if (policy.size() > 5 && !policy[5].empty()) v5 = policy[5]; + + // Query whether the data already exists in the database + auto rows = db(select(all_of(casbin_rule)).from(casbin_rule).where(casbin_rule.ptype == "g" and casbin_rule.v0 == v0 and casbin_rule.v1 == v1 and casbin_rule.v2 == v2 and casbin_rule.v3 == v3 and casbin_rule.v4 == v4 and casbin_rule.v5 == v5)); + if (rows.empty()) { + // If it does not exist, insert it + db(insert_into(casbin_rule).set(casbin_rule.ptype = "g", casbin_rule.v0 = v0, casbin_rule.v1 = v1, casbin_rule.v2 = v2, casbin_rule.v3 = v3, casbin_rule.v4 = v4, casbin_rule.v5 = v5)); + } + } + } + + //if the table or database does not exist, create it + void createTable() { + try { + db.execute("CREATE DATABASE IF NOT EXISTS casbin"); + db.execute("USE casbin"); + db.execute("CREATE TABLE IF NOT EXISTS casbin_rule " + " (id INT AUTO_INCREMENT PRIMARY KEY, " + "ptype VARCHAR(255), v0 VARCHAR(255), v1 VARCHAR(255), " + "v2 VARCHAR(255), v3 VARCHAR(255), v4 VARCHAR(255), v5 VARCHAR(255))"); + } + catch (const sqlpp::exception& e) { + std::cerr << "Error creating database or table: " << e.what() << std::endl; + } + } + + void dropTable() { + try { + db.execute("DROP TABLE IF EXISTS casbin_rule" ); + } + catch (const sqlpp::exception& e) { + std::cerr << "Error dropping table: " << e.what() << std::endl; + } + } + + //empty function + void AddPolicy() { + + }; + + void RemovePolicy() { + } + + void RemoveFilteredPolicy() { + } +}; \ No newline at end of file diff --git a/src/test.cpp b/src/test.cpp new file mode 100644 index 0000000..39b38e3 --- /dev/null +++ b/src/test.cpp @@ -0,0 +1,60 @@ +/* + * Copyright 2020 The casbin Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "sqlpp11_adapter.cpp" +#include +#include "casbin/casbin.h" +static const std::string model_path = relative_path+"/examples/model.conf"; +TEST(Sqlpp11AdapterTest, LoadPolicy) { +// Create a Sqlpp11Adapter instance +Sqlpp11Adapter adapter; +adapter.NewAdapter("127.0.0.1",3306, "root", "125463", "casbin"); +// Create a model +std::shared_ptr model = casbin::Model::NewModel(); +model->LoadModel("../examples/model.conf"); + +// Load policy +adapter.LoadPolicy(model); + +// Check if policy exists +ASSERT_TRUE(model->HasPolicy("p", "p", {"alice", "data1", "write"})); +} + +TEST(Sqlpp11AdapterTest, SavePolicy) { +// Create a Sqlpp11Adapter instance +Sqlpp11Adapter adapter; +adapter.NewAdapter("127.0.0.1", 3306, "root", "125463", "casbin"); +// Create a model +std::shared_ptr model = casbin::Model::NewModel(); +model->LoadModel("../examples/model.conf"); + +// Add some policies +model->AddPolicy("p", "p", {"alice", "data1", "write"}); + +// Save policy +adapter.SavePolicy(model); + +// Reload policy to check if it has been saved +model->ClearPolicy(); +adapter.LoadPolicy(model); + +// Check if new policy exists +ASSERT_TRUE(model->HasPolicy("p", "p", {"alice", "data1", "write"})); +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}