PHP code example of lake / larke-auth

1. Go to this page and download the library: Download lake/larke-auth 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/ */

    

lake / larke-auth example snippets


'providers' => [
    /*
     * Package Service Providers...
     */
    Larke\Auth\ServiceProvider::class,
]

'aliases' => [
    // ...
    'Enforcer' => Larke\Auth\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 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 App\Auth;

use Larke\Auth\Contracts\AuthUser;

class YourAuthUser extends AuthUser
{
    /**
     * @return string|int
     */
    public function getIdentifier()
    {
        $id = ...;
        return $id;
    }
}

$this->app->bind(
    \Larke\Auth\Contracts\AuthUser::class, 
    \App\Auth\YourAuthUser:class
);

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

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

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

return [
    'default' => 'basic',
    
    'guards' => [
        'basic' => [
            'model' => [
                // ...
            ],

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

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

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


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

'cache' => [
    // changes whether larkeauth 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 --tag=larke-auth-config

php artisan larke-auth:install
bash
php artisan policy:add eve,articles,read
bash
php artisan policy:add writer,articles,edit