PHP code example of fazzinipierluigi / just-a-gate

1. Go to this page and download the library: Download fazzinipierluigi/just-a-gate 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/ */

    

fazzinipierluigi / just-a-gate example snippets




namespace App\Models;

use Fazzinipierluigi\JustAGate\Traits\Authorizable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Authorizable;
}



return [

    /*
    | Name used to register the ACL middleware.
    | Apply it to routes with Route::middleware('acl').
    | Type: string
    | Default: 'acl'
    */
    'middleware' => 'acl',

    /*
    | Extra permissions created during `permission:import`.
    | Format: 'permission_key' => 'Display name'
    | Type: array<string, string>
    | Default: []
    */
    'additional' => [
        // 'reports.export' => 'Export reports',
    ],

    /*
    | When true, `permission:import` auto-generates one permission per existing role,
    | with key "user.create.role_{slug}". Useful for controlling which roles a user
    | is allowed to assign during registration/admin flows.
    | Type: bool
    | Default: true
    */
    'role_user_creation' => true,

    /*
    | When true, `permission:import` deletes permissions that no longer correspond
    | to any route protected by the 'acl' middleware or any 'additional' entry.
    | Type: bool
    | Default: true
    */
    'clean_permission' => true,

    /*
    | Maps permission keys to human-readable names, applied during `permission:import`.
    | Useful for overriding auto-generated names.
    | Format: 'permission_key' => 'Display name'
    | Type: array<string, string>
    | Default: []
    */
    'translate' => [
        // 'user.index' => 'View user list',
    ],

    /*
    | Auto-assigns permissions to roles during `permission:import`.
    | Format: 'permission_key' => ['role_slug_1', 'role_slug_2']
    | Type: array<string, list<string>>
    | Default: []
    */
    'assign' => [
        // 'user.index' => ['admin', 'moderator'],
    ],

];

use Fazzinipierluigi\JustAGate\Facades\JustAGate;

// 1. Create roles
$admin  = JustAGate::createRole('Administrator', 'admin', isAdmin: true);
$editor = JustAGate::createRole('Editor', 'editor');

// 2. Create permissions
JustAGate::createPermission('post.index',   'View posts');
JustAGate::createPermission('post.create',  'Create posts');
JustAGate::createPermission('post.destroy', 'Delete posts');

// 3. Assign permissions to roles
JustAGate::givePermission('editor', 'post.index', 'post.create');
// admin has is_admin=true, so it bypasses all permission checks automatically

// 4. Assign roles to users
$user = User::find(1);
JustAGate::assignRole($user, 'editor');

// 5. Check access
$user->can('post.index');   // true
$user->can('post.destroy'); // false
$user->hasRole('editor');   // true

// Single route
Route::get('/posts', [PostController::class, 'index'])->middleware('acl');

// Group
Route::middleware('acl')->group(function () {
    Route::get('/posts',         [PostController::class, 'index']);
    Route::post('/posts',        [PostController::class, 'store']);
    Route::delete('/posts/{id}', [PostController::class, 'destroy']);
});

Route::middleware(['auth', 'acl'])->group(function () {
    // ...
});

public function roles(): BelongsToMany

$user->roles;              // Collection<Role>
$user->roles()->count();   // int

$user->can('post.index');    // bool
$user->can('post.destroy');  // bool

public function cannot(string $ability, mixed $arguments = []): bool

$user->cannot('post.destroy'); // bool

public function clearPermissionCache(): void

$user->assignRole('editor');
// cache already cleared automatically by assignRole()

// Manual clear if you modified pivot tables directly:
$user->clearPermissionCache();

public function assignRole(Role|string|int ...$roles): static

$user->assignRole('editor');
$user->assignRole($roleA, $roleB);
$user->assignRole('editor', 'moderator');
$user->assignRole(3);

public function removeRole(Role|string|int ...$roles): static

$user->removeRole('editor');
$user->removeRole($roleA, $roleB);

public function syncRoles(array $roles): static

$user->syncRoles(['editor', 'moderator']);
$user->syncRoles([]);  // removes all roles

public function hasRole(Role|string $role): bool

$user->hasRole('admin');    // bool
$user->hasRole($roleObj);   // bool

public function hasAnyRole(Role|string ...$roles): bool

$user->hasAnyRole('editor', 'moderator', 'admin'); // bool

public function hasAllRoles(Role|string ...$roles): bool

$user->hasAllRoles('editor', 'moderator'); // bool

public function getRoles(): \Illuminate\Database\Eloquent\Collection

$user->getRoles()->pluck('slug'); // Collection<string>

use Fazzinipierluigi\JustAGate\Models\Role;

// Direct model creation
$role = Role::create([
    'name'      => 'Editor',
    'slug'      => 'editor',
    'is_admin'  => false,
    'is_system' => false,
]);

// Via Facade (recommended)
$role = JustAGate::createRole('Editor', 'editor');
$admin = JustAGate::createRole('Administrator', 'admin', isAdmin: true, isSystem: true);

public function permissions(): BelongsToMany

public function users(): BelongsToMany

public function givePermission(Permission|string ...$permissions): static

$role->givePermission('post.index');
$role->givePermission('post.index', 'post.create', 'post.destroy');
$role->givePermission($permissionModel);

public function revokePermission(Permission|string ...$permissions): static

$role->revokePermission('post.destroy');
$role->revokePermission($permA, $permB);

public function syncPermissions(array $permissions): static

$role->syncPermissions(['post.index', 'post.create']);
$role->syncPermissions([]); // removes all permissions from this role

public function hasPermission(string $key): bool

$role->hasPermission('post.index'); // bool

$role->givePermission('post.index', 'post.create')
     ->revokePermission('post.destroy');

use Fazzinipierluigi\JustAGate\Models\Permission;

// Direct model creation
$perm = Permission::create(['key' => 'post.index', 'name' => 'View posts']);

// Via Facade (recommended — also lowercases the key)
$perm = JustAGate::createPermission('post.index', 'View posts', 'Allows viewing the posts list');

public static function findByKey(mixed $key): ?Permission

Permission::findByKey('post.index');  // Permission|null
Permission::findByKey(null);          // null
Permission::findByKey('');            // null
Permission::findByKey(42);            // null

public function roles(): BelongsToMany

$permission->roles; // Collection<Role>

use Fazzinipierluigi\JustAGate\Facades\JustAGate;

$role = JustAGate::createRole('Editor', 'editor');
$admin = JustAGate::createRole('Administrator', 'admin', isAdmin: true, isSystem: true);

$role = JustAGate::findRole('editor'); // Role|null

$roles = JustAGate::allRoles(); // Collection<Role>

JustAGate::deleteRole('editor');   // by slug
JustAGate::deleteRole($roleObj);   // by instance

$perm = JustAGate::createPermission('post.index', 'View posts', 'Allows listing all posts');

$perm = JustAGate::findPermission('post.index'); // Permission|null

$perms = JustAGate::allPermissions(); // Collection<Permission>

JustAGate::deletePermission('post.index');
JustAGate::deletePermission($permObj);

JustAGate::assignRole($user, 'editor');
JustAGate::assignRole($user, 'editor', 'moderator');
JustAGate::assignRole($user, $roleObj);

JustAGate::removeRole($user, 'editor');

JustAGate::syncRoles($user, ['editor', 'moderator']);
JustAGate::syncRoles($user, []);  // removes all roles

JustAGate::givePermission('editor', 'post.index', 'post.create');
JustAGate::givePermission($roleObj, $permObj);

JustAGate::revokePermission('editor', 'post.destroy');

JustAGate::syncPermissions('editor', ['post.index', 'post.create']);
JustAGate::syncPermissions('editor', []); // removes all permissions from role

JustAGate::userCan($user, 'post.index'); // bool

JustAGate::userHasRole($user, 'editor'); // bool

JustAGate::roleHasPermission('editor', 'post.index'); // bool

$editors = JustAGate::usersWithRole('editor'); // Collection<User>

$roles = JustAGate::rolesForPermission('post.index'); // Collection<Role>



namespace App\Livewire;

use Fazzinipierluigi\JustAGate\Attributes\RequiresPermission;
use Livewire\Component;

#[RequiresPermission('post.index')]
class PostsList extends Component
{
    public function render()
    {
        return view('livewire.posts-list');
    }
}



namespace App\Livewire;

use Fazzinipierluigi\JustAGate\Attributes\RequiresPermission;
use Livewire\Component;

class PostsList extends Component
{
    public function render()
    {
        return view('livewire.posts-list');
    }

    #[RequiresPermission('post.destroy')]
    public function delete(int $id): void
    {
        Post::findOrFail($id)->delete();
    }

    #[RequiresPermission('post.create')]
    public function create(array $data): void
    {
        Post::create($data);
    }
}

#[RequiresPermission('post.index')]          // enforced on every request
class PostsList extends Component
{
    #[RequiresPermission('post.destroy')]    // additionally enforced on delete()
    public function delete(int $id): void
    {
        Post::findOrFail($id)->delete();
    }
}

// routes/web.php
Route::middleware(['auth', 'acl'])->get('/posts', PostsList::class);

// App\Livewire\PostsList.php
#[RequiresPermission('post.index')]
class PostsList extends Component { ... }

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class RequiresPermission
{
    public function __construct(public readonly string $permission) {}
}
bash
php artisan vendor:publish --provider="Fazzinipierluigi\JustAGate\JustAGateServiceProvider"
bash
php artisan permission:init
bash
php artisan permission:init
bash
php artisan permission:create {key?} {name?}
bash
php artisan permission:create post.index "View posts"
php artisan permission:create   # launches interactive prompt
bash
php artisan permission:assign {key?} {role?}
bash
php artisan permission:assign post.index editor
php artisan permission:assign   # launches interactive selects