PHP code example of ordain / delegation

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

    

ordain / delegation example snippets


use Ordain\Delegation\Contracts\DelegatableUserInterface;
use Ordain\Delegation\Traits\HasDelegation;

class User extends Authenticatable implements DelegatableUserInterface
{
    use HasDelegation;

    protected $fillable = [
        // ... your fields
        'can_manage_users',
        'max_manageable_users',
        'created_by_user_id',
    ];
}

use Ordain\Delegation\Facades\Delegation;

// Can this user assign a role to another user?
if (Delegation::canAssignRole($delegator, $role, $target)) {
    Delegation::delegateRole($delegator, $target, $role);
}

// Can this user create new users?
if (Delegation::canCreateUsers($user)) {
    // Create user...
}

// What roles can this user assign?
$assignableRoles = Delegation::getAssignableRoles($user);

use Ordain\Delegation\Domain\ValueObjects\DelegationScope;

// Define what a manager can delegate
$scope = new DelegationScope(
    canManageUsers: true,
    maxManageableUsers: 10,
    assignableRoleIds: [1, 2, 3],
    assignablePermissionIds: [4, 5],
);

Delegation::setDelegationScope($manager, $scope);

// Using middleware
Route::middleware('can.delegate')->group(function () {
    Route::post('/users', [UserController::class, 'store']);
});

Route::middleware('can.assign.role:editor,moderator')
    ->post('/users/{user}/roles', [RoleController::class, 'store']);

// Using route macros
Route::post('/users', [UserController::class, 'store'])
    ->canDelegate();

Route::post('/users/{user}/roles', [RoleController::class, 'store'])
    ->canAssignRole(['editor', 'moderator']);
bash
php artisan vendor:publish --tag=delegation-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=delegation-config
bash
# Interactive installation wizard
php artisan delegation:install

# Display user's delegation scope
php artisan delegation:show {user}

# Assign role via CLI
php artisan delegation:assign {delegator} {target} {role}

# Clear delegation cache
php artisan delegation:cache-reset {user?}

# Health check
php artisan delegation:health