PHP code example of callcocam / inertia-rbac

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

    

callcocam / inertia-rbac example snippets


use Callcocam\InertiaRbac\Concerns\HasRbac;

class User extends Authenticatable
{
    use HasRbac; // HasRoles (Spatie) + HasUlids + isSuperAdmin()
}

// config/rbac.php
'permissions' => [
    'products.viewAny', 'products.view', 'products.create', 'products.update', 'products.delete',
    'orders.viewAny', 'orders.view',
],

// opcional: classificar recursos por tipo/contexto
'type_map' => ['products' => 'catalog', 'orders' => 'sales'],

use Callcocam\InertiaRbac\Concerns\ChecksRbacPermission;

class ProductPolicy
{
    use ChecksRbacPermission;

    public function viewAny($user): bool { return $this->allowByContext($user, 'products.viewAny'); }
    public function create($user): bool  { return $this->allowByContext($user, 'products.create'); }
    // ...
}

use Callcocam\InertiaRbac\Support\PermissionResolver;
use Callcocam\InertiaRbac\Models\Role;

$resolver = app(PermissionResolver::class);

$menu = array_values(array_filter([
    $resolver->allows($user, 'viewAny', Role::class)
        ? ['label' => 'Papéis', 'href' => RoleController::index()] : null,
]));

// config/rbac.php
'teams' => [
    'enabled' => true,
    'foreign_key' => 'tenant_id',                             // coluna de tenant nas tabelas do Spatie
    'resolver' => fn ($request) => $request->user()?->tenant_id, // null = lê a foreign_key do usuário
],
bash
php artisan wayfinder:generate --with-form