PHP code example of buzz / laravel-advanced-authorization

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

    

buzz / laravel-advanced-authorization example snippets




namespace App;

use Buzz\Authorization\Traits\PermissionForUserTrait;
use Buzz\Authorization\Traits\RoleForUserTrait;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Notifiable, Authenticatable, CanResetPassword, RoleForUserTrait, PermissionForUserTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

$role = new \App\Role();//depend "model_role" config 
$role->name = 'Admin';
$role->slug = 'Admin';// can use str_slug('Create posts', '.');
$role->save();

//attach
$role->attachPermission($permission);//input is object
$role->attachPermission([$permission, $permission2, $permission3]);//input is array objects
$role->attachPermission(1);//assume 1 is id of $permission
$role->attachPermission([1,2,3]);//assume 1,2,3 is id of $permission, $permission2, $permission3

//detach
$role->detachPermission($permission);//input is object
$role->detachPermission([$permission, $permission2, $permission3]);//input is array objects
$role->detachPermission(1);//assume 1 is id of $permission
$role->detachPermission([1,2,3]);//assume 1,2,3 is id of $permission, $permission2, $permission3
$role->detachPermission([]);//detach all permissions

$user = \App\User::find(1);//depend "model_user" config 
//attach
$role->attachRole($role);//input is object
$role->attachRole([$role, $role2, $role3]);//input is array objects
$role->attachRole(1);//assume 1 is id of $role
$role->attachRole([1,2,3]);//assume 1,2,3 is id of $role, $role2, $role3

//detach
$role->detachRole($role);//input is object
$role->detachRole([$role, $role2, $role3]);//input is array objects
$role->detachRole(1);//assume 1 is id of $role
$role->detachRole([1,2,3]);//assume 1,2,3 is id of $role, $role2, $role3
$role->detachRole([]);//detach all roles

//someAction: is, isAny, can, canAny
//check user with database
$user = \App\User::find(1);
$user->someAction
//check current user login
$user = \Auth::user();
$user->someAction
//or
Authorization::someAction
//or
app('authorization')->someAction

$user->is('admin');//admin is slug of role
//OR
$user->is(['admin', 'mod']);//['admin', 'mod'] is array slugs of role
//Blade
@role('admin')
    //your code
@endRole

$user->isAny(['admin', 'mod']);
//Blade
@anyRole('admin')
    //your code
@endAnyRole

$user->can('post.create');//admin is slug of permission
//OR
$user->can(['post.create', 'post.delete']);//['admin', 'mod'] is array slugs of permission
//Blade
@permission('post.create')
    //your code
@endPermission

$user->canAny(['post.create', 'post.delete']);
//Blade
@anyPermission('post.create')
    //your code
@endAnyPermission

@greaterLevel('3')// check smallest level of user > 3
    //your code
@endGreaterLevel
@endLessLevel('3')// check smallest level of user < 3
    //your code
@endGreaterLevel
@betweenLevel(3, 5)// check smallest level of user between 3 and 5
    //your code
@endBetweenLevel
@matchAnyLevel([3,5])// check smallest level of user has in array [3,5]
    //your code
@endMatchAnyLevel

//check user can delete post
Route::get('/permission', ['middleware' => ['permission:post.delete'], 'uses' => function () {
    return 'permission';
}]);
//check user can delete post and create post
Route::get('/permission', ['middleware' => ['permission:post.delete&post.create'], 'uses' => function () {
    return 'permission';
}]);
//check user can delete post or create post
Route::get('/permission', ['middleware' => ['permission:post.delete|post.create'], 'uses' => function () {
    return 'permission';
}]);

//check user is admin
Route::get('/role', ['middleware' => ['role:admin'], 'uses' => function () {
    return 'role';
}]);
//check user is admin and mod
Route::get('/role', ['middleware' => ['role:admin&mod'], 'uses' => function () {
    return 'role';
}]);
//check user is admin or mod
Route::get('/role', ['middleware' => ['role:admin|mod'], 'uses' => function () {
    return 'role';
}]);

//check level smallest of user equal 1
Route::get('/level', ['middleware' => ['level:1'], 'uses' => function () {
    return 'level';
}]);
//check 1 <= level smallest of user <= 3
Route::get('/level', ['middleware' => ['level:1<=>3'], 'uses' => function () {
    return 'level';
}]);
//check level smallest of user < 3
Route::get('/level', ['middleware' => ['level:<3'], 'uses' => function () {
    return 'level';
}]);
//check level smallest of user > 3
Route::get('/level', ['middleware' => ['level:>3'], 'uses' => function () {
    return 'level';
}]);
//check user has all level in list 1,2,3
Route::get('/level', ['middleware' => ['level:1&2&3'], 'uses' => function () {
    return 'level';
}]);
//check user has one level in list 1,2,3
Route::get('/level', ['middleware' => ['level:1|2|3'], 'uses' => function () {
    return 'level';
}]);

php artisan vendor:publish --provider="Buzz\Authorization\AuthorizationServiceProvider"