### Install Casbin Headers Source: https://github.com/apache/casbin-cpp/blob/master/CMakeLists.txt Installs the Casbin header files to the include directory of the installation prefix. ```cmake install( DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/casbin DESTINATION include ) ``` -------------------------------- ### Complete Authorization Example Source: https://github.com/apache/casbin-cpp/blob/master/README.md A consolidated C++ example demonstrating the initialization of the Enforcer, defining a request, and performing an authorization check. ```cpp #include #include void IsAuthorized() { casbin::Enforcer e("./path/to/model.conf", "./path/to/policy.csv"); std::string sub = "alice"; // the user that wants to access a resource. std::string obj = "data1"; // the resource that is going to be accessed. std::string act = "read"; // the operation that the user performs on the resource. if(e.Enforce({ sub, obj, act })) { // permit alice to read data1 } else { // deny the request, show an error } } ``` -------------------------------- ### Install Casbin-CPP Source: https://github.com/apache/casbin-cpp/blob/master/README.md Installs the Casbin-CPP library to the system after it has been built. This makes it available for use with `find_package`. ```bash cmake --build . --config Release --target install ``` -------------------------------- ### Basic pycasbin Usage Example Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/README.md A complete example demonstrating how to import pycasbin, create an Enforcer, and check authorization for multiple requests. ```python import pycasbin as casbin e = casbin.Enforcer('path/to/model.conf', 'path/to/policy.csv') def isAuthorized(req): result = e.Enforce(req) if result: print('Authorized') else: print('Not authorized!') isAuthorized(['subject1', 'object1', 'action1']) isAuthorized(['subject2', 'object2', 'action2']) # ... and so on ``` -------------------------------- ### Install Casbin-CPP Library Source: https://github.com/apache/casbin-cpp/blob/master/README.md Install the Casbin-CPP library to your system after building. This makes the library and its headers available for use in other projects. ```bash cmake --build . --target install ``` -------------------------------- ### Install pycasbin Module Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/README.md Install the pycasbin module locally using pip after navigating to the casbin-cpp directory. ```bash cd casbin-cpp && pip install --verbose . ``` -------------------------------- ### ACL Policy Example Source: https://github.com/apache/casbin-cpp/blob/master/README.md An example policy demonstrating user permissions for resources and actions in an ACL model. ```plaintext p, alice, data1, read p, bob, data2, write ``` -------------------------------- ### Update Python Packaging Tools Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/README.md Ensure you have the latest versions of wheel and setuptools for Python package installation. ```bash python -m pip install --upgrade wheel setuptools ``` -------------------------------- ### Find clang-format Binary Source: https://github.com/apache/casbin-cpp/blob/master/CMakeLists.txt Attempts to locate the clang-format executable if not explicitly defined. It searches in a predefined list of common installation paths. ```cmake if (NOT DEFINED CLANG_FORMAT_BIN) # attempt to find the binary if user did not specify find_program(CLANG_FORMAT_BIN NAMES clang-format clang-format-8 HINTS ${CASBIN_CLANG_SEARCH_PATH}) endif() if ("${CLANG_FORMAT_BIN}" STREQUAL "CLANG_FORMAT_BIN-NOTFOUND") message(WARNING "Casbin couldn't find clang-format.") else() message(STATUS "Casbin found clang-format at ${CLANG_FORMAT_BIN}") endif() ``` -------------------------------- ### Find clang-tidy Binary Source: https://github.com/apache/casbin-cpp/blob/master/CMakeLists.txt Attempts to locate the clang-tidy executable if not explicitly defined. It searches in a predefined list of common installation paths and enables export of compile commands. ```cmake if (NOT DEFINED CLANG_TIDY_BIN) # attempt to find the binary if user did not specify find_program(CLANG_TIDY_BIN NAMES clang-tidy clang-tidy-8 HINTS ${CASBIN_CLANG_SEARCH_PATH}) endif() if ("${CLANG_TIDY_BIN}" STREQUAL "CLANG_TIDY_BIN-NOTFOUND") message(WARNING "Casbin couldn't find clang-tidy.") else() # Output compile_commands.json set(CMAKE_EXPORT_COMPILE_COMMANDS 1) message(STATUS "Casbin found clang-tidy at ${CLANG_TIDY_BIN}") endif() ``` -------------------------------- ### Export Casbin Library Source: https://github.com/apache/casbin-cpp/blob/master/CMakeLists.txt Exports the Casbin library targets and creates a configuration file for package management when the installation is enabled. ```cmake export( TARGETS casbin NAMESPACE casbin:: FILE casbinConfig.cmake ) ``` -------------------------------- ### Include Casbin Header Source: https://github.com/apache/casbin-cpp/blob/master/README.md Include the main Casbin header file in your C++ project to start using the library's functionalities. ```cpp #include ``` -------------------------------- ### Get Implicit Roles for a User Source: https://github.com/apache/casbin-cpp/blob/master/README.md Retrieve all roles implicitly assigned to a user by calling the GetImplicitRolesForUser method on the Enforcer instance. ```cpp std::vector roles( e.GetImplicitRolesForUser(sub) ); ``` -------------------------------- ### Find Casbin Package in CMake Project Source: https://github.com/apache/casbin-cpp/blob/master/README.md Finds the installed Casbin package within your CMake project. This command imports Casbin's targets, making them available for linking. ```cmake find_package(casbin REQUIRED) ``` -------------------------------- ### Initialize Casbin Enforcer Source: https://github.com/apache/casbin-cpp/blob/master/README.md Create an instance of the Casbin Enforcer by providing paths to the model configuration file and the policy file. ```cpp casbin::Enforcer e("./path/to/model.conf", "./path/to/policy.csv"); ``` -------------------------------- ### Run Casbin-CPP Tests Source: https://github.com/apache/casbin-cpp/blob/master/README.md Execute the test suite for Casbin-CPP from the build directory to verify the library's functionality. ```bash ctest ``` -------------------------------- ### Set Benchmark Include Directories Source: https://github.com/apache/casbin-cpp/blob/master/tests/benchmarks/CMakeLists.txt Configures the include directories for the benchmark executable. ```cmake target_include_directories(casbin_benchmark PUBLIC ${CASBIN_INCLUDE_DIR}) ``` -------------------------------- ### Define Benchmark Header Files Source: https://github.com/apache/casbin-cpp/blob/master/tests/benchmarks/CMakeLists.txt Sets the list of header files for the Casbin benchmark. ```cmake set(CASBIN_BENCHMARK_HEADER config_path.h ) ``` -------------------------------- ### Build Casbin-CPP Library Source: https://github.com/apache/casbin-cpp/blob/master/README.md Compile the Casbin-CPP library using CMake after the project has been configured. ```bash cmake --build . ``` -------------------------------- ### Fetch Casbin using FetchContent in CMake Source: https://github.com/apache/casbin-cpp/blob/master/README.md Integrates Casbin into your CMake project by fetching it directly from its GitHub repository. Assumes CMake version 3.19 or higher. Customize build options like tests, benchmarks, and bindings as needed. ```cmake include(FetchContent) FetchContent_Declare( casbin GIT_REPOSITORY https://github.com/casbin/casbin-cpp.git GIT_TAG v1.38.1 ) 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() ``` -------------------------------- ### Define Request Structure Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/README.md Illustrates the expected formats for authorization requests, which can be a list or a dictionary. ```python req = ['subject1', 'object1', 'action1'] # and so on.. req = { "sub": "subject1", "obj": "object1", "act": "action1" # ... and so on } ``` -------------------------------- ### Add Benchmark Subdirectory Source: https://github.com/apache/casbin-cpp/blob/master/tests/CMakeLists.txt Includes the benchmarks subdirectory if CASBIN_BUILD_BENCHMARK is enabled. ```cmake if(CASBIN_BUILD_BENCHMARK) add_subdirectory(benchmarks) endif() ``` -------------------------------- ### Link Benchmark Libraries Source: https://github.com/apache/casbin-cpp/blob/master/tests/benchmarks/CMakeLists.txt Links the necessary libraries (benchmark, casbin, nlohmann_json) to the benchmark executable. ```cmake target_link_libraries( casbin_benchmark PRIVATE benchmark casbin nlohmann_json::nlohmann_json ) ``` -------------------------------- ### Create Benchmark Executable Source: https://github.com/apache/casbin-cpp/blob/master/tests/benchmarks/CMakeLists.txt Creates the benchmark executable, conditionally including intensive benchmark sources if the INTENSIVE_BENCHMARK variable is set to ON. ```cmake if(INTENSIVE_BENCHMARK STREQUAL ON) add_executable(casbin_benchmark ${CASBIN_BENCHMARK_SOURCE} ${CASBIN_INTENSIVE_BENCHMARK_SOURCE} ${CASBIN_BENCHMARK_HEADER}) else() add_executable(casbin_benchmark ${CASBIN_BENCHMARK_SOURCE} ${CASBIN_BENCHMARK_HEADER}) endif() ``` -------------------------------- ### Create Casbin Enforcer Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/README.md Instantiate a Casbin Enforcer by providing paths to the model configuration and policy files. ```python e = casbin.Enforcer('path/to/model.conf', 'path/to/policy.csv') ``` -------------------------------- ### Define Benchmark Source Files Source: https://github.com/apache/casbin-cpp/blob/master/tests/benchmarks/CMakeLists.txt Sets the list of source files for the standard Casbin benchmark executable. ```cmake set(CASBIN_BENCHMARK_SOURCE main.cpp model_b.cpp enforcer_cached_b.cpp management_api_b.cpp role_manager_b.cpp ) ``` -------------------------------- ### Link Target Against Casbin and Set Include Directory Source: https://github.com/apache/casbin-cpp/blob/master/README.md Links your target (e.g., an executable or library) against the Casbin library and specifies the include directory for Casbin's headers. Ensure MY_INCLUDE_DIR is set to the correct path on your system. ```cmake set(MY_INCLUDE_DIR "/usr/local/include") target_include_directories(MyTargetName PRIVATE ${MY_INCLUDE_DIR}) target_link_libraries(MyTargetName PRIVATE casbin::casbin) ``` -------------------------------- ### Clone casbin-cpp Repository Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/README.md Clone the casbin-cpp project from GitHub to access the source code. ```bash git clone https://github.com/casbin/casbin-cpp.git ``` -------------------------------- ### Enable pybind11 Extension Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/CMakeLists.txt Enables pybind11 extension for the pycasbin target. ```cmake pybind11_extension(pycasbin) ``` -------------------------------- ### Define Header Files for pycasbin Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/CMakeLists.txt Lists the header files used by the pycasbin library. ```cmake set(HEADERS src/py_casbin.h ) ``` -------------------------------- ### Define Source Files for pycasbin Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/CMakeLists.txt Lists the C++ source files used to build the pycasbin library. ```cmake set(SOURCES src/main.cpp src/py_cached_enforcer.cpp src/py_enforcer.cpp src/py_model.cpp src/py_config.cpp src/py_synced_enforcer.cpp src/py_adapter.cpp ) ``` -------------------------------- ### Build Casbin-CPP Release Version Source: https://github.com/apache/casbin-cpp/blob/master/README.md Builds the Casbin-CPP project in Release configuration using CMake. This compiles the library after configuration. ```bash cmake --build . --config Release ``` -------------------------------- ### Configure C++ Standard and Memory Checking for Tests Source: https://github.com/apache/casbin-cpp/blob/master/tests/CMakeLists.txt Sets the C++ standard to C++17 and configures Valgrind for memory leak checking when CASBIN_BUILD_TEST is enabled. ```cmake if(CASBIN_BUILD_TEST) set(CMAKE_CXX_STANDARD 17) # set memcheck find_program( MEMORYCHECK_COMMAND valgrind ) set(MEMORYCHECK_COMMAND_OPTIONS "--leak-check=full --error-exitcode=1") include(CTest) add_definitions(-DCASBIN_PROJECT_DIR=${CMAKE_SOURCE_DIR}) set(CASBIN_TEST_SOURCE built_in_functions_test.cpp config_test.cpp enforcer_test.cpp enforcer_cached_test.cpp enforcer_synced_test.cpp management_api_test.cpp model_enforcer_test.cpp model_test.cpp rbac_api_with_domains_test.cpp rbac_api_test.cpp role_manager_test.cpp util_test.cpp ) set(CASBIN_TEST_HEADER config_path.h ) add_executable(casbintest ${CASBIN_TEST_SOURCE} ${CASBIN_TEST_HEADER}) if(UNIX) set_target_properties(casbintest PROPERTIES POSITION_INDEPENDENT_CODE ON ) endif() target_include_directories(casbintest PUBLIC ${CASBIN_INCLUDE_DIR}) target_link_libraries( casbintest PRIVATE gtest_main casbin nlohmann_json::nlohmann_json ) include(GoogleTest) gtest_discover_tests(casbintest) endif() ``` -------------------------------- ### Link Executable Against Casbin Library Source: https://github.com/apache/casbin-cpp/blob/master/README.md After making Casbin's targets available, link your executable against the Casbin library and include its header directory. Ensure the include directory path is correctly set. ```cmake add_executable(myexec main.cpp) target_link_libraries(myexec PRIVATE casbin) set(myexec_INCLUDE_DIR ${casbin_SOURCE_DIR}/include) target_include_directories(myexec PRIVATE ${myexec_INCLUDE_DIR}) ``` -------------------------------- ### Set Include Directories for pycasbin Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/CMakeLists.txt Specifies the include directories for the pycasbin target, including the CASBIN_INCLUDE_DIR. ```cmake target_include_directories(pycasbin PUBLIC ${CASBIN_INCLUDE_DIR}) ``` -------------------------------- ### ACL Model Configuration Source: https://github.com/apache/casbin-cpp/blob/master/README.md This is the basic configuration for an Access Control List (ACL) model in Casbin. ```ini [request_definition] r = sub, obj, act [policy_definition] p = sub, obj, act [policy_effect] e = some(where (p.eft == allow)) [matchers] m = r.sub == p.sub && r.obj == p.obj && r.act == p.act ``` -------------------------------- ### Configure Casbin-CPP Build with CMake Source: https://github.com/apache/casbin-cpp/blob/master/README.md Configures the Casbin-CPP build using CMake in a separate build directory. This step generates build files and may provide important path information in its logs. ```bash mkdir build cd build cmake .. ``` -------------------------------- ### Import pycasbin Module Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/README.md Import the pycasbin module into your Python script to begin using its functionalities. ```python import pycasbin as casbin ``` -------------------------------- ### Define Intensive Benchmark Source Files Source: https://github.com/apache/casbin-cpp/blob/master/tests/benchmarks/CMakeLists.txt Sets the list of source files for the intensive Casbin benchmark executable. ```cmake set(CASBIN_INTENSIVE_BENCHMARK_SOURCE model_b_inten.cpp enforcer_cached_b_inten.cpp management_api_b_inten.cpp role_manager_b_inten.cpp ) ``` -------------------------------- ### Custom Target for Code Formatting Source: https://github.com/apache/casbin-cpp/blob/master/CMakeLists.txt Defines a custom CMake target 'format' that runs a Python script to format code using clang-format. It updates files in place. ```cmake add_custom_target(format ${CASBIN_BUILD_SUPPORT_DIR}/run_clang_format.py ${CLANG_FORMAT_BIN} ${CASBIN_BUILD_SUPPORT_DIR}/clang_format_exclusions.txt --source_dirs ${CASBIN_FORMAT_DIRS} --format_style "file" --fix --quiet ) ``` -------------------------------- ### Add pycasbin Library Target Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/CMakeLists.txt Creates a shared library target named 'pycasbin' using the defined source and header files. ```cmake add_library(pycasbin MODULE ${SOURCES} ${HEADERS}) ``` -------------------------------- ### Add Versioning Macro for pycasbin Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/CMakeLists.txt Defines a preprocessor macro PY_CASBIN_VERSION with the version specified by PY_CASBIN_VERSION. ```cmake add_definitions(-DPY_CASBIN_VERSION=${PY_CASBIN_VERSION}) ``` -------------------------------- ### Perform Policy Enforcement Check Source: https://github.com/apache/casbin-cpp/blob/master/README.md Use the Enforcer's Enforce method to check if a request (subject, object, action) is permitted according to the loaded policy. ```cpp std::string sub = "alice"; // the user that wants to access a resource. std::string obj = "data1"; // the resource that is going to be accessed. std::string act = "read"; // the operation that the user performs on the resource. if(e.Enforce({ sub, obj, act })) { // permit alice to read data1 } else { // deny the request, show an error } ``` -------------------------------- ### Set Position Independent Code for Unix Source: https://github.com/apache/casbin-cpp/blob/master/tests/benchmarks/CMakeLists.txt Enables Position Independent Code for the benchmark executable on Unix-like systems. ```cmake if(UNIX) set_target_properties(casbin_benchmark PROPERTIES POSITION_INDEPENDENT_CODE ON ) endif() ``` -------------------------------- ### Custom Target for Checking Code Format Source: https://github.com/apache/casbin-cpp/blob/master/CMakeLists.txt Defines a custom CMake target 'check-format' that runs a Python script to check code formatting with clang-format. It exits with a non-zero code if reformatting is needed. ```cmake add_custom_target(check-format ${CASBIN_BUILD_SUPPORT_DIR}/run_clang_format.py ${CLANG_FORMAT_BIN} ${CASBIN_BUILD_SUPPORT_DIR}/clang_format_exclusions.txt --source_dirs ${CASBIN_FORMAT_DIRS} --format_style "file" --quiet ) ``` -------------------------------- ### Link Libraries to pycasbin Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/CMakeLists.txt Links necessary libraries to the pycasbin target, including pybind11 modules, casbin, and nlohmann_json. ```cmake target_link_libraries(pycasbin PRIVATE pybind11::module pybind11::lto pybind11::windows_extras casbin nlohmann_json::nlohmann_json ) ``` -------------------------------- ### Set C++ Standard for pycasbin Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/CMakeLists.txt Configures the pycasbin target to use C++17 standard. ```cmake set_target_properties(pycasbin PROPERTIES CXX_STANDARD 17 ) ``` -------------------------------- ### Strip Debug Symbols from pycasbin Source: https://github.com/apache/casbin-cpp/blob/master/pycasbin/CMakeLists.txt Strips debug symbols from the pycasbin target for release builds. ```cmake pybind11_strip(pycasbin) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.