PHP code example of foss-haas / laravel-permission-objects

1. Go to this page and download the library: Download foss-haas/laravel-permission-objects 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/ */

    

foss-haas / laravel-permission-objects example snippets


$permission = $article::getPermission("edit");
$user->permissions->grant($permission, $article);

$user->permissions->revoke($permission);

$user->permissions->revokeAll();

$table->jsonb('permissions');

use FossHaas\LaravelPermissionObjects\AsPermissions;

protected function casts(): array
{
    return [
        'permissions' => AsPermissions::class,
    ];
}

use App\Models\User;
use FossHaas\LaravelPermissionObjects\Permission;

Permission::register(User::class, [
    'manage' => fn() => __('Manage users'),
    'change-passwords' => fn() => __('Change user passwords'),
]);

use Illuminate\Database\Eloquent\Relations\Relation;

Relation::morphMap([
    'user' => \App\Models\User::class,
]);

Permission::register(Permission::class, [
    'assign' => fn() => __('Assign permissions'),
]);

use App\Models\User;

$manageUsersPermission = Permission::resolve('manage', User::class);

// This also works if your morph map is set up correctly:
$manageUsersPermission = Permission::resolve('manage', 'user');

use App\Models\User;

$manageUsersPermission = Permission::find(User::class . '.' . 'manage');

// This also works if your morph map is set up correctly:
$manageUsersPermission = Permission::find('user.manage');

use FossHaas\LaravelPermissionObjects\Traits\HasPermissions;

class User extends Authenticatable {
    use HasPermissions;
    // ...
}

$manageUsersPermission = User::getPermission('manage');

use FossHaas\LaravelPermissionObjects\Permission;

$assignPermissionsPermission = Permission::getPermission('assign');

use App\Models\User;
use FossHaas\LaravelPermissionObjects\Permission;

$permission = Permission::find('article.edit');

// grant permission to edit only a specific article
$user->permissions->grant($permission, (string) $article->id);

// grant permission to edit any article
$user->permissions->grant($permission, null);

// revoke only permission to edit a specific article, if it was granted
$user->permissions->revoke($permission, (string) $article->id);

// revoke only permission to edit any article, if it was granted
$user->permissions->revoke($permission, null);

// revoke all permissions of the given permission type
$user->permissions->revokeAll($permission);

// revoke all permissions ever granted to the user
$user->permissions->revokeAll();

use App\Models\User;
use FossHaas\LaravelPermissionObjects\Permission;

$permission = Permission::find('article.edit');

// Check if the user has the permission for this instance
$user->permissions->has($permission, (string) $article->id);

// Check if the user has the permission for all instances
$user->permissions->has($permission, null);

$user->permissions->grant($permission, null);
// This will always pass
$user->permissions->has($permission, (string) $article->id);

use App\Models\User;
use Illuminate\Support\Facades\Gate;

Gate::after(function (User $user, string $ability, bool|null $result, mixed $arguments) {
    if ($result !== null) return $result;
    $object = isset($arguments[0]) ? $arguments[0] : null;
    return $user->permissions->can($ability, $object);
});

// Elsewhere ...
Gate::authorize('edit', [$article]);

Gate::after(function (User $user, string $ability, bool|null $result, mixed $arguments) {
    if ($result !== null) return $result;
    $object = isset($arguments[0]) ? $arguments[0] : null;
    $scopes = isset($arguments[1]) ? $arguments[1] : "";
    return $user->permissions->can($ability, $object, $scopes);
});

// Elsewhere ...
Gate::authorize('edit', [$article, $scope]);

Permission::register(null, [
    'self-destruct' => fn() => __('Initiate self-destruct')
]);

$permission = Permission::find('self-destruct');
$user->permissions->has($permission, null);

// This also works:
$user->permissions->can('self-destruct', null);

// This DOES NOT work:
$user->permissions->can('self-destruct', '1234'); // Always returns null!

use App\Models\User;
use Illuminate\Support\Facades\Gate;
use FossHaas\LaravelPermissionObjects\Permission;

Permission::register(null, [
    'is-super-admin' => fn() => __('Is Super Admin'),
]);

// Use as a fallback check if no other Gate or Policy applied
Gate::after(function (User $user): bool {
    return $user->permissions->has('is-super-admin', null);
});

// Alternatively, if super admins should always bypass all rules
Gate::before(function (User $user): bool|null {
    return $user->permissions->has('is-super-admin', null) ?: null;
});

$user->permissions->grant($permission, $objectId, $scope);

$user->permissions->scope($scope)->grant($permission, $objectId);

// This is also the default behavior of `revokeAll` if no scopes are specified
$user->permissions->revokeAll($permission, AsScopedPermissions::ALL_SCOPES);

Schema::create('roles', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->json('permissions');
});

Schema::create('user_roles', function (Blueprint $table) {
    $table->foreignIdFor(User::class)
        ->constrained('users')->cascadeOnDelete();
    $table->foreignIdFor(Role::class)
        ->constrained('roles')->cascadeOnDelete();
});