PHP code example of inlayphp / authorization-spatie

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

    

inlayphp / authorization-spatie example snippets


return [
    'super_admin_role' => env('INLAY_SUPER_ADMIN_ROLE', 'super-admin'),
    'default_guard' => env('INLAY_PERMISSION_GUARD', 'web'),
    'teams' => [
        'resolver' => NullTeamResolver::class,
        'session_key' => env('INLAY_PERMISSION_TEAM_SESSION_KEY', 'inlay_team_id'),
        'user_relations' => ['roles', 'permissions'],
    ],
];

use Inlay\Authorization\Contracts\PermissionSynchronizer;

$result = app(PermissionSynchronizer::class)->sync(
    guard: 'web',
    dryRun: false,
    prune: false,
);

$result->created;
$result->existing;
$result->stale;
$result->deleted;

use Inlay\AuthorizationSpatie\Middleware\SetPermissionTeam;
use Inlay\AuthorizationSpatie\TeamResolvers\SessionTeamResolver;

// config/inlay-authorization-spatie.php
'teams' => [
    'resolver' => SessionTeamResolver::class,
    'session_key' => 'current_team_id',
    'user_relations' => ['roles', 'permissions'],
],

// bootstrap/app.php (Laravel 12+)
$middleware->web(append: [SetPermissionTeam::class]);

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Inlay\AuthorizationSpatie\Contracts\TeamResolver;

final class CurrentTenantResolver implements TeamResolver
{
    public function resolve(Request $request): int|string|Model|null
    {
        return $request->attributes->get('tenant');
    }
}
bash
composer vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan migrate
php artisan vendor:publish --tag=inlay-authorization-spatie-config
bash
php artisan inlay:permissions:sync --dry-run
php artisan inlay:permissions:sync
php artisan inlay:permissions:sync --guard=admin
bash
php artisan inlay:permissions:sync --dry-run --prune --force
php artisan inlay:permissions:sync --prune --force
bash
vendor/bin/pest tests/AuthorizationSpatieTest.php