PHP code example of joseph / rbac

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

    

joseph / rbac example snippets


'providers' => array(

    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    ...
    'Joseph\Rbac\RbacServiceProvider',

),

'aliases' => array(

    'App'        => 'Illuminate\Support\Facades\App',
    'Artisan'    => 'Illuminate\Support\Facades\Artisan',
    ...
    'Rbac'    => 'Joseph\Rbac\RbacFacade',

),



use Joseph\Rbac\RbacRole;

class Role extends RbacRole
{

}



use Joseph\Rbac\RbacPermission;

class Permission extends RbacPermission
{

}



use Joseph\Rbac\HasRole;

class User extends Eloquent /* or ConfideUser 'wink' */{ 
    use HasRole; // Add this trait to your user model
    
...

$owner = new Role;
$owner->name = 'Owner';
$owner->save();

$admin = new Role;
$admin->name = 'Admin';
$admin->save();


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

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

/* OR the eloquent's original: */
$user->roles()->attach( $admin->id ); // id only

$managePosts = new Permission;
$managePosts->name = 'manage_posts';
$managePosts->display_name = 'Manage Posts';
$managePosts->save();

$manageUsers = new Permission;
$manageUsers->name = 'manage_users';
$manageUsers->display_name = 'Manage Users';
$manageUsers->save();

$owner->perms()->sync(array($managePosts->id,$manageUsers->id));
$admin->perms()->sync(array($managePosts->id));

$user->hasRole("Owner");    // false
$user->hasRole("Admin");    // true
$user->can("manage_posts"); // true
$user->can("manage_users"); // false

$user->ability(array('Admin','Owner'), array('manage_posts','manage_users'));
//or
$user->ability('Admin,Owner', 'manage_posts,manage_users');


$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('manage_posts','manage_users'), $options);

// Output
var_dump($validate);
bool(false)
var_dump($allValidations);
array(4) {
  ['role'] => bool(true)
  ['role_2'] => bool(false)
  ['manage_posts'] => bool(true)
  ['manage_users'] => bool(false)
}

// Only users with roles that have the 'manage_posts' permission will
// be able to access any route within admin/post.
Rbac::routeNeedsPermission( 'admin/post*', 'manage_posts' );

// Only owners will have access to routes within admin/advanced
Rbac::routeNeedsRole( 'admin/advanced*', 'Owner' );

// Optionally the second parameter can be an array of permissions or roles.
// User would need to match all roles or permissions for that route.
Rbac::routeNeedsPermission( 'admin/post*', array('manage_posts','manage_comments') );

Rbac::routeNeedsRole( 'admin/advanced*', array('Owner','Writer') );

Rbac::routeNeedsRole( 'admin/advanced*', 'Owner', Redirect::to('/home') );

// If a user has `manage_posts`, `manage_comments` or both they will have access.
Rbac::routeNeedsPermission( 'admin/post*', array('manage_posts','manage_comments'), null, false );

// If a user is a member of `Owner`, `Writer` or both they will have access.
Rbac::routeNeedsRole( 'admin/advanced*', array('Owner','Writer'), null, false );

// If a user is a member of `Owner`, `Writer` or both, or user has `manage_posts`, `manage_comments` they will have access.
// You can set the 4th parameter to true then user must be member of Role and must has Permission.
Rbac::routeNeedsRoleOrPermission( 'admin/advanced*', array('Owner','Writer'), array('manage_posts','manage_comments'), null, false);

Route::filter('manage_posts', function()
{
    if (! Rbac::can('manage_posts') ) // Checks the current user
    {
        return Redirect::to('admin');
    }
});

// Only users with roles that have the 'manage_posts' permission will
// be able to access any admin/post route.
Route::when('admin/post*', 'manage_posts'); 

Route::filter('owner_role', function()
{
    if (! Rbac::hasRole('Owner') ) // Checks the current user
    {
        App::abort(404);
    }
});

// Only owners will have access to routes within admin/advanced
Route::when('admin/advanced*', 'owner_role'); 



use Joseph\Rbac\RbacRole;

class Role extends RbacRole
{
    /**
     * Ardent validation rules
     *
     * @var array
     */
    public static $rules = array(
      'name' => '