1. Go to this page and download the library: Download micc83/rooles 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 \Rooles\Traits\UserRole;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, UserRole;
// ...
}
$role->grant('comments'); // Same as writing comments.*
$role->can('comments.write'); // true
$role->can('comments.pingbacks.write') // true
$role->grant('comments.write.*') // Same as writing comments.write
->deny('*.write');
$role->can('comments.write'); // true
$role->can('users.write') // false
$user = User::find(1);
if ($user->can('comments.post')){
// Do something...
}
public function index(Illuminate\Contracts\Auth\Guard $auth) {
if ( $auth->user->can('users.list') ){
// Do something...
}
}
if ( $user->cannot('users.list') ) redirect()->to('dashboard');
if ( $user->can(['users.list', 'users.read']) ) // Do something when the user has both the permissions (AND)
if ( $user->can('users.list&users.read') ) // Do something when the user has both the permissions (& > AND)
if ( $user->can('users.list|users.read') ) // Do something when the user has one of the requested permissions (| > OR)
if ( $user->role->is('admin') ) echo 'Hello Boss';
if ( $user->role->isIn(['lamer', 'trool']) ) echo 'Hello Looser';
// If in a string context:
echo $user->role;
// Otherwise:
if ($user->role->name() === 'Admin') // Do something
Route::get('admin/users/', [
'middleware' => [
'auth',
'role:admin|editor', // Give access to both admins and editors
],
function () {
return view('admin.users.index');
}
]);
Route::get('admin/users/', [
'middleware' => [
'auth',
'perms:users.list|users.edit', // Give access to users with users.list OR users.edit permissions
]
function () {
return view('admin.users.index');
}
]);
// Route Group
Route::group([
'middleware' => [
'auth',
'role:admin|editor' // Give access to both admins and editors
]
], function () {
Route::resource('users', 'UserController');
Route::resource('posts', 'PostController');
});
public function render($request, Exception $e)
{
if ($e instanceof \Rooles\ForbiddenHttpException) {
return redirect('/')->withErrors(['You don\'t have the needed permissions to perform this action!']);
}
return parent::render($request, $e);
}