PHP code example of casbin / laravel-authz

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


'providers' => [
    /*
     * Package Service Providers...
     */
    Lauthz\LauthzServiceProvider::class,
]

'aliases' => [
    // ...
    'Enforcer' => Lauthz\Facades\Enforcer::class,
]


use 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 role
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

protected $routeMiddleware = [
    // ...
    // a basic Enforcer Middleware
    'enforcer' => \Lauthz\Middlewares\EnforcerMiddleware::class,
    // an HTTP Request Middleware
    'http_request' => \Lauthz\Middlewares\RequestMiddleware::class,
];

Route::group(['middleware' => ['enforcer:articles,read']], function () {
    // pass
});

Route::group(['middleware' => ['http_request']], function () {
    Route::resource('photo', 'PhotoController');
});

$user->can('articles,read');
// For multiple enforcers
$user->can('articles,read', 'second');
// The methods cant, cannot, canAny, etc. also work

return [
    'default' => 'basic',

    'basic' => [
        'model' => [
            // ...
        ],

        'adapter' => Lauthz\Adapters\DatabaseAdapter::class,
        // ...
    ],

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

        'adapter' => Lauthz\Adapters\DatabaseAdapter::class,
        // ...
    ],
];


Enforcer::guard('second')->enforce("eve", "articles", "edit");

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

    // cache store
    'store' => 'default',

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

    // ttl \DateTimeInterface|\DateInterval|int|null
    'ttl' => 24 * 60,
],

php artisan vendor:publish

php artisan migrate
bash
php artisan policy:add eve,articles,read
bash
php artisan policy:add writer,articles,edit