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/ */
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
// 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') );
// 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' => '
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.