PHP code example of casbin / codeigniter-permission

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



$enforcer = \Config\Services::enforcer();

// adds permissions to a user
$enforcer->addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
$enforcer->addRoleForUser('eve', 'writer');
// adds permissions to a rule
$enforcer->addPolicy('writer', 'articles','edit');


// to check if a user has permission
if ($enforcer->enforce("eve", "articles", "edit")) {
    // permit eve to edit articles
} else {
    // deny the request, show an error
}


$enforcer->getAllRoles(); // ['writer', 'reader']

$enforcer->getPolicy();

$enforcer->getRolesForUser('eve'); // ['writer']

$enforcer->getUsersForRole('writer'); // ['eve']

$enforcer->hasRoleForUser('eve', 'writer'); // true or false

$enforcer->addRoleForUser('eve', 'writer');

// to user
$enforcer->addPermissionForUser('eve', 'articles', 'read');
// to role
$enforcer->addPermissionForUser('writer', 'articles','edit');

$enforcer->deleteRoleForUser('eve', 'writer');

$enforcer->deleteRolesForUser('eve');

$enforcer->deleteRole('writer');

$enforcer->deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).

$enforcer->deletePermissionForUser('eve', 'articles', 'read');

// to user
$enforcer->deletePermissionsForUser('eve');
// to role
$enforcer->deletePermissionsForUser('writer');

$enforcer->getPermissionsForUser('eve'); // return array

$enforcer->hasPermissionForUser('eve', 'articles', 'read');  // true or false


namespace Config;

use Casbin\CodeIgniter\Config\Enforcer as BaseConfig;
use Casbin\CodeIgniter\Adapters\DatabaseAdapter;

class Enforcer extends BaseConfig
{
    /*
     * Default Enforcer driver
     *
     * @var string
     */
    public $default = 'basic';

    public $basic = [
        /*
        * Casbin model setting.
        */
        'model' => [
            // Available Settings: "file", "text"
            'config_type' => 'file',

            'config_file_path' => __DIR__.'/rbac-model.conf',

            'config_text' => '',
        ],

        /*
        * Casbin adapter .
        */
        'adapter' => DatabaseAdapter::class,

        /*
        * Database setting.
        */
        'database' => [
            // Database connection for following tables.
            'connection' => '',

            // Rule table name.
            'rules_table' => 'rules',
        ],

        'log' => [
            // changes whether Casbin will log messages to the Logger.
            'enabled' => false,

            // Casbin Logger
            'logger' => \Casbin\CodeIgniter\Logger::class,
        ],

        'cache' => [
            // changes whether Casbin will cache the rules.
            'enabled' => false,

            // cache Key
            'key' => 'rules',

            // ttl int|null
            'ttl' => 24 * 60,
        ],
    ];

    public $second = [
        'model' => [
            // ...
        ],

        'adapter' => DatabaseAdapter::class,
        // ...
    ];
}


$enforcer->guard('second')->enforce("eve", "articles", "edit");

'cache' => [
    // changes whether Casbin will cache the rules.
    'enabled' => false,
    // cache Key
    'key' => 'rules',
    // ttl int|null
    'ttl' => 24 * 60,
]

php spark migrate -n "Casbin\CodeIgniter"