PHP code example of casbin / cake-permission

1. Go to this page and download the library: Download casbin/cake-permission 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 / cake-permission example snippets




return [
    'Casbin' => [
         /*
         * Cake-casbin model setting.
         */
        'model' => [
            // Available Settings: "file", "text"
            'config_type' => 'file',
            'config_file_path' => __DIR__.'/casbin-model.conf',
            'config_text' => '',
        ],

        // Cake-casbin adapter .
        'adapter' => '\Cake\Permission\Adapter',

        /*
         * Cake-casbin database setting.
         */
        'database' => [
            // Database connection for following tables.
            'connection' => '',
            // CasbinRule tables and model.
            'casbin_rules_table' => '',
        ],
    ],
];


$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.

$casbin = new \Cake\Permission\Casbin();

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