PHP code example of casbin / dbal-adapter

1. Go to this page and download the library: Download casbin/dbal-adapter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

casbin / dbal-adapter example snippets




use Casbin\Enforcer;
use CasbinAdapter\DBAL\Adapter as DatabaseAdapter;
use Doctrine\DBAL\DriverManager; // Required if creating a new connection object

// Option 1: Using DBAL connection parameters array
$dbConnectionParams = [
    // Supported drivers: pdo_mysql, pdo_sqlite, pdo_pgsql, pdo_oci, pdo_sqlsrv, 
    // mysqli, sqlanywhere, sqlsrv, ibm_db2, drizzle_pdo_mysql
    'driver' => 'pdo_mysql',
    'host' => '127.0.0.1',
    'dbname' => 'casbin_db', // Your database name
    'user' => 'root',
    'password' => '',
    'port' => '3306', // Optional, defaults to driver's standard port
    // 'policy_table_name' => 'casbin_rules', // Optional, defaults to 'casbin_rule'
];

// Initialize the Adapter with the DBAL parameters array (without Redis)
$adapter = DatabaseAdapter::newAdapter($dbConnectionParams);
// Alternatively, using the constructor:
// $adapter = new DatabaseAdapter($dbConnectionParams);

// Option 2: Using an existing Doctrine DBAL Connection instance
// $dbalConnection = DriverManager::getConnection($dbConnectionParams);
// $adapter = DatabaseAdapter::newAdapter($dbalConnection);
// Or using the constructor:
// $adapter = new DatabaseAdapter($dbalConnection);


$e = new Enforcer('path/to/model.conf', $adapter);

$sub = "alice"; // the user that wants to access a resource.
$obj = "data1"; // the resource that is going to be accessed.
$act = "read"; // the operation that the user performs on the resource.

if ($e->enforce($sub, $obj, $act) === true) {
    // permit alice to read data1
} else {
    // deny the request, show an error
}



use Casbin\Enforcer;
use CasbinAdapter\DBAL\Adapter as DatabaseAdapter;
use Doctrine\DBAL\DriverManager; // Required if creating a new connection object

// Database connection parameters (can be an array or a Connection object)
$dbConnectionParams = [
    'driver' => 'pdo_mysql',
    'host' => '127.0.0.1',
    'dbname' => 'casbin_db',
    'user' => 'root',
    'password' => '',
    'port' => '3306',
];
// Example with DBAL connection object:
// $dbalConnection = DriverManager::getConnection($dbConnectionParams);

// Redis configuration
$redisConfig = [
    'host' => '127.0.0.1',      // Optional, defaults to '127.0.0.1'
    'port' => 6379,             // Optional, defaults to 6379
    'password' => null,         // Optional, defaults to null
    'database' => 0,            // Optional, defaults to 0
    'ttl' => 7200,              // Optional, Cache policies for 2 hours (default is 3600)
    'prefix' => 'myapp_casbin:' // Optional, Custom prefix (default is 'casbin_policies:')
];

// Initialize adapter with DB parameters array and Redis configuration
$adapter = DatabaseAdapter::newAdapter($dbConnectionParams, $redisConfig);
// Or, using a DBAL Connection object:
// $adapter = DatabaseAdapter::newAdapter($dbalConnection, $redisConfig);
// Alternatively, using the constructor:
// $adapter = new DatabaseAdapter($dbConnectionParams, $redisConfig);

$e = new Enforcer('path/to/model.conf', $adapter);

// ... rest of your Casbin usage

if ($adapter->preheatCache()) {
    // Cache preheating was successful
    echo "Casbin policy cache preheated successfully.\n";
} else {
    // Cache preheating failed (e.g., Redis not available or DB error)
    echo "Casbin policy cache preheating failed.\n";
}