1. Go to this page and download the library: Download directorytree/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/ */
directorytree / authorization example snippets
namespace App\Models;
use DirectoryTree\Authorization\Traits\Authorizable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Authorizable;
// ...
}
use DirectoryTree\Authorization\Authorization;
/**
* Register any application services.
*
* @return void
*/
public function register()
{
Authorization::ignoreMigrations();
}
use DirectoryTree\Authorization\Authorization;
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Authorization::useUserModel(\App\Models\User::class);
Authorization::useRoleModel(\App\Models\Role::class);
Authorization::usePermissionModel(\App\Models\Permission::class);
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DirectoryTree\Authorization\Traits\ManagesPermissions;
class Role extends Model
{
use ManagesPermissions;
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DirectoryTree\Authorization\Traits\HasUsers;
use DirectoryTree\Authorization\Traits\HasRoles;
use DirectoryTree\Authorization\Traits\ClearsCachedPermissions;
class Permission extends Model
{
use HasUsers, HasRoles, ClearsCachedPermissions;
}
// Using the permission's name:
$admin->grant('users.create');
// Using a permission model:
$admin->grant($permission);
// Granting multiple permissions:
$admin->grant(['users.create', 'users.edit']);
// Granting a collection of models:
$admin->grant(Permission::all());
// Using a mix of models and permission name:
$admin->grant([$createUsers, 'users.edit']);
// All permissions will be removed except, except for the given:
$admin->grantOnly('users.create');
// Using the permission's name:
$admin->revoke('users.create');
// Using a permission model:
$admin->revoke($permission);
// Revoking multiple permissions:
$admin->revoke(['users.create', 'users.edit']);
// Revoking a collection of models:
$admin->revoke(Permission::all());
// Using a mix of models and permission name:
$admin->revoke([$createUsers, 'users.edit']);
// Using the permission's name:
$user->grant('users.create');
// Using a permission model:
$user->grant($permission);
// Granting multiple permissions:
$user->grant(['users.create', 'users.edit']);
// Granting a collection of models:
$user->grant(Permission::all());
// Using a mix of models and permission name:
$user->grant([$createUsers, 'users.edit']);
// All permissions will be removed except, except for the given:
$user->grantOnly('users.create');
// Using the permission's name:
$user->revoke('users.create');
// Using a permission model:
$user->revoke($permission);
// Granting multiple permissions:
$user->revoke(['users.create', 'users.edit']);
// Granting a collection of models:
$user->revoke(Permission::all());
// Using a mix of models and permission name:
$user->revoke([$createUsers, 'users.edit']);
if (Auth::user()->can('users.create')) {
// This user can create other users.
}
public function create()
{
$this->authorize('users.create');
User::create(['...']);
}
if (Gate::allows('users.create')) {
//
}
// Using the permissions name.
if ($user->hasPermission('users.create')) {
//
}
// Using the permissions model.
if ($user->hasPermission($createUsers)) {
//
}
if ($user->hasPermissions(['users.create', 'users.edit'])) {
// This user has both creation and edit rights.
} else {
// The user doesn't have one of the specified permissions.
}
if ($user->hasAnyPermissions(['users.create', 'users.edit', 'users.destroy'])) {
// This user either has create, edit or destroy permissions.
} else {
// The user doesn't have any of the specified permissions.
}
if ($user->hasRole('administrator')) {
// This user is an administrator.
} else {
// The user isn't an administrator.
}
if ($user->hasRoles(['administrator', 'member'])) {
// This user is an administrator and a member.
} else {
// The user isn't an administrator or member.
}
if ($user->hasAnyRoles(['administrator', 'member', 'guest'])) {
// This user is either an administrator, member or guest.
} else {
// The user doesn't have any of these roles.
}
use DirectoryTree\Authorization\Authorization;
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Authorization::disablePermissionCache();
}
use DirectoryTree\Authorization\Authorization;
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Authorization::cacheKey('my-key');
}
use DirectoryTree\Authorization\Authorization;
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Authorization::cacheExpiresIn(now()->addWeek());
}
use DirectoryTree\Authorization\Authorization;
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Authorization::disableGateRegistration();
}
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
// The role middleware:
'role' => \DirectoryTree\Authorization\Middleware\RoleMiddleware::class,
// The permission middleware:
'permission' => \DirectoryTree\Authorization\Middleware\PermissionMiddleware::class,
];
Route::get('users', [
'uses' => 'UsersController@index',
'middleware' => 'permission:users.index',
]);
// Multiple permissions:
Route::get('users', [
'uses' => 'UsersController@index',
// Users must have index **and** create rights to access this route.
'middleware' => 'permission:users.index,users.create',
]);
Route::get('users', [
'uses' => 'UsersController@index',
'middleware' => 'role:administrator',
]);
// Multiple roles:
Route::get('users', [
'uses' => 'UsersController@index',
// Users must be an administrator **and** a member to access this route.
'middleware' => 'role:administrator,member',
]);
use DirectoryTree\Authorization\PermissionRegistrar;
protected function setUp() : void
{
parent::setUp();
app(PermissionRegistrar::class)->register();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.