Skip to content

casbin-cpp/sqlpp11-adapter

Repository files navigation

Sqlpp11 Adapter

CI License

Sqlpp11 Adapter is a sqlpp11 adapter for Casbin. With this library, Casbin can load policy from MySQL database or save policy to it.

Features

  • Load policy from MySQL database
  • Save policy to MySQL database
  • Full support for Casbin policy management
  • Built on top of the powerful sqlpp11 library

Installation

Requirements

Building from source

git clone https://github.com/casbin-cpp/sqlpp11-adapter
cd sqlpp11-adapter
mkdir build && cd build
cmake ..
cmake --build .

Usage

Basic Example

#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;
}

Model Configuration

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

API Reference

Constructor

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.

Methods

LoadPolicy

void LoadPolicy(const std::shared_ptr<Model>& model) override;

Loads all policy rules from the database.

SavePolicy

void SavePolicy(Model& model) override;

Saves all policy rules to the database (replaces existing policies).

CreateTable

void CreateTable();

Creates the casbin_rule table if it doesn't exist.

DropTable

void DropTable();

Drops the casbin_rule table if it exists.

Database Schema

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)
);

Testing

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.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Acknowledgments

  • Casbin - An authorization library that supports access control models
  • sqlpp11 - A type safe SQL template library for C++

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •