PHP code example of michalisantoniou6 / laravel-cerberus

1. Go to this page and download the library: Download michalisantoniou6/laravel-cerberus 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/ */

    

michalisantoniou6 / laravel-cerberus example snippets


Michalisantoniou6\Cerberus\CerberusServiceProvider::class,

'Cerberus'   => Michalisantoniou6\Cerberus\CerberusFacade::class,

    'role' => \Michalisantoniou6\Cerberus\Middleware\CerberusRole::class,
    'permission' => \Michalisantoniou6\Cerberus\Middleware\CerberusPermission::class,
    'ability' => \Michalisantoniou6\Cerberus\Middleware\CerberusAbility::class,

 namespace App;

use Michalisantoniou6\Cerberus\CerberusRole;

class Role extends CerberusRole
{
}

 namespace App;

use Michalisantoniou6\Cerberus\CerberusPermission;

class Permission extends CerberusPermission
{
}



use Michalisantoniou6\Cerberus\Traits\CerberusUserTrait;

class User extends Eloquent
{
    use CerberusUserTrait; // add this trait for a single tenancy site
    
    //...
}



use Michalisantoniou6\Cerberus\Traits\CerberusSiteUserTrait;

class User extends Eloquent
{
    use CerberusSiteUserTrait; //add this trait for a multi-tenant site
    
    //...
}

$role = Role::findOrFail(1); // Pull back a given role

// Regular Delete
$role->delete(); // This will work no matter what

// Force Delete
$role->users()->sync([]); // Delete relationship data
$role->perms()->sync([]); // Delete relationship data

$role->forceDelete(); // Now force delete will work regardless of whether the pivot table has cascading delete

$owner = new Role();
$owner->name         = 'owner';
$owner->display_name = 'Project Owner'; // optional
$owner->description  = 'User is the owner of a given project'; // optional
$owner->save();

$admin = new Role();
$admin->name         = 'admin';
$admin->display_name = 'User Administrator'; // optional
$admin->description  = 'User is allowed to manage and edit other users'; // optional
$admin->save();

$user = User::where('username', '=', 'michele')->first();

// role attach alias
$user->attachRole($admin); // parameter can be an Role object, array, or id

// or eloquent's original technique
$user->roles()->attach($admin->id); // id only

$createPost = new Permission();
$createPost->name         = 'create-post';
$createPost->display_name = 'Create Posts'; // optional
// Allow a user to...
$createPost->description  = 'create new blog posts'; // optional
$createPost->save();

$editUser = new Permission();
$editUser->name         = 'edit-user';
$editUser->display_name = 'Edit Users'; // optional
// Allow a user to...
$editUser->description  = 'edit existing users'; // optional
$editUser->save();

$admin->attachPermission($createPost);
// equivalent to $admin->perms()->sync(array($createPost->id));

$owner->attachPermissions(array($createPost, $editUser));
// equivalent to $owner->perms()->sync(array($createPost->id, $editUser->id));

$user->hasRole('owner');   // false
$user->hasRole('admin');   // true
$user->hasPermission('edit-user');   // false
$user->hasPermission('create-post'); // true

$user->hasRole(['owner', 'admin']);       // true
$user->hasPermission(['edit-user', 'create-post']); // true

$user->hasRole(['owner', 'admin']);             // true
$user->hasRole(['owner', 'admin'], true);       // false, user does not have admin role
$user->hasPermission(['edit-user', 'create-post']);       // true
$user->hasPermission(['edit-user', 'create-post'], true); // false, user does not have edit-user permission

Cerberus::hasRole('role-name');
Cerberus::hasPermission('permission-name');

// is identical to

Auth::user()->hasRole('role-name');
Auth::user()->hasPermission('permission-name');

// match any admin permission
$user->hasPermission("admin.*"); // true

// match any permission about users
$user->hasPermission("*_users"); // true

$user->ability(array('admin', 'owner'), array('create-post', 'edit-user'));

// or

$user->ability('admin,owner', 'create-post,edit-user');

$options = array(
    'validate_all' => true | false (Default: false),
    'return_type'  => boolean | array | both (Default: boolean)
);

$options = array(
    'validate_all' => true,
    'return_type' => 'both'
);

list($validate, $allValidations) = $user->ability(
    array('admin', 'owner'),
    array('create-post', 'edit-user'),
    $options
);

var_dump($validate);
// bool(false)

var_dump($allValidations);
// array(4) {
//     ['role'] => bool(true)
//     ['role_2'] => bool(false)
//     ['create-post'] => bool(true)
//     ['edit-user'] => bool(false)
// }


Cerberus::ability('admin,owner', 'create-post,edit-user');

// is identical to

Auth::user()->ability('admin,owner', 'create-post,edit-user');

@role('admin')
    <p>This is visible to users with the admin role. Gets translated to 
    \Cerberus::role('admin')</p>
@endrole

@permission('manage-admins')
    <p>This is visible to users with the given permissions. Gets translated to 
    \Cerberus::hasPermission('manage-admins'). The @can directive is already taken by core 
    laravel authorization package, hence the @permission directive instead.</p>
@endpermission

@ability('admin,owner', 'create-post,edit-user')
    <p>This is visible to users with the given abilities. Gets translated to 
    \Cerberus::ability('admin,owner', 'create-post,edit-user')</p>
@endability

@roleforsite('admin', 15)
    <p>This is visible to users with the admin role for site with id 15. Gets translated to 
    \Cerberus::roleForSite('admin', 15)</p>
@endroleforsite

@permissionforsite('manage-admins', 15)
    <p>This is visible to users with the given permissions for site with id 15. Gets translated to 
    \Cerberus::hasPermissionForSite('manage-admins', 15). The @can directive is already taken by core 
    laravel authorization package, hence the @permission directive instead.</p>
@endpermissionforsite

@abilityforsite('admin,owner', 'create-post,edit-user', 15)
    <p>This is visible to users with the given abilities for site with id 15. Gets translated to 
    \Cerberus::abilityForSite('admin,owner', 'create-post,edit-user', 15)</p>
@endabilityforsite

Route::group(['prefix' => 'admin', 'middleware' => ['role:admin']], function() {
    Route::get('/', 'AdminController@welcome');
    Route::get('/manage', ['middleware' => ['permission:manage-admins'], 'uses' => 'AdminController@manageAdmins']);
});

'middleware' => ['role:admin|root']

'middleware' => ['role:owner', 'role:writer']

'middleware' => ['ability:admin|owner,create-post|edit-user,true']
shell
php artisan vendor:publish
bash
php artisan cerberus:migration
bash
php artisan migrate
bash
composer dump-autoload