PHP code example of ahmdrv / laravel-permission-registry
1. Go to this page and download the library: Download ahmdrv/laravel-permission-registry 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/ */
ahmdrv / laravel-permission-registry example snippets
declare(strict_types=1);
namespace App\Authorization\Permissions;
use Ahmdrv\PermissionRegistry\Enums\PermissionPreset;
use Ahmdrv\PermissionRegistry\Enums\RiskLevel;
use Ahmdrv\PermissionRegistry\Resources\PermissionResource;
use Ahmdrv\PermissionRegistry\ValueObjects\PermissionAction;
final class ProductPermissionResource extends PermissionResource
{
protected static PermissionPreset $preset = PermissionPreset::CRUD;
public static function key(): string
{
return 'products';
}
public static function label(): string
{
return 'Products';
}
public static function group(): string
{
return 'catalog';
}
public static function description(): ?string
{
return 'Product catalog management';
}
protected static function actions(): array
{
return [
PermissionAction::make('publish')
->label('Publish product')
->risk(RiskLevel::HIGH)
->directGrantable(),
PermissionAction::make('delete')
->risk(RiskLevel::CRITICAL),
];
}
}
protected static function exceptActions(): array
{
return ['delete'];
}
use Ahmdrv\PermissionRegistry\ValueObjects\PermissionRecommendation;
PermissionAction::make('export')->recommend(
PermissionRecommendation::make('reports.view_any')
->reason('Useful when the user also needs to browse report history.'),
);
$directPermissionManager->grant($actor, $user, 'products.publish');
$directPermissionManager->revoke($actor, $user, 'products.publish');
$directPermissionManager->sync($actor, $user, ['products.publish']);
$sources = $accessInspector->inspect($user);
// Each item reports the permission, whether it is direct, and contributing roles.
$this->authorize('products.create');
$this->authorize('update', $product); // ownership, tenant, state, or data-scope policy
$this->authorize('users.assign_roles');
// This action may load role options specifically for this workflow.
// It does not rely on, imply, recommend by default, or grant roles.view_any.