Add CMAKE_PREFIX_PATH to find installed packages Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
Sqlpp11 Adapter is a sqlpp11 adapter for Casbin. With this library, Casbin can load policy from MySQL database or save policy to it.
git clone https://github.com/casbin-cpp/sqlpp11-adapter cd sqlpp11-adapter mkdir build && cd build cmake .. cmake --build .
#include "include/sqlpp11_adapter.h" #include <casbin/casbin.h> int main() { // Create adapter with MySQL connection parameters auto adapter = std::make_shared<casbin::Sqlpp11Adapter>( "localhost", // host "root", // user "password", // password "casbin", // database 3306 // port (default: 3306) ); // Create the table (if it doesn't exist) adapter->CreateTable(); // Create enforcer with model file and adapter auto enforcer = std::make_shared<casbin::Enforcer>( "path/to/model.conf", adapter ); // Load policy from database enforcer->LoadPolicy(); // Add policies enforcer->AddPolicy("alice", "data1", "read"); enforcer->AddPolicy("bob", "data2", "write"); // Save policy to database enforcer->SavePolicy(); // Check permissions if (enforcer->Enforce("alice", "data1", "read")) { // alice can read data1 } return 0; }
Create a model configuration file (e.g., rbac_model.conf):
[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
Sqlpp11Adapter(const std::string& host, const std::string& user, const std::string& password, const std::string& database, unsigned int port = 3306);
Creates a new adapter instance with the specified MySQL connection parameters.
void LoadPolicy(const std::shared_ptr<Model>& model) override;
Loads all policy rules from the database.
void SavePolicy(Model& model) override;
Saves all policy rules to the database (replaces existing policies).
void CreateTable();
Creates the casbin_rule table if it doesn't exist.
void DropTable();
Drops the casbin_rule table if it exists.
The adapter uses a table named casbin_rule with the following schema:
CREATE TABLE casbin_rule ( id INT AUTO_INCREMENT PRIMARY KEY, ptype VARCHAR(100) NOT NULL, v0 VARCHAR(100), v1 VARCHAR(100), v2 VARCHAR(100), v3 VARCHAR(100), v4 VARCHAR(100), v5 VARCHAR(100) );
To run the tests:
cd build ./test
Make sure you have a MySQL server running and accessible with the connection parameters used in the test.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.