Create a new Laravel policy with philsquare/permissions role-based permissions
Creates a new Laravel policy with role-based permissions using philsquare/permissions.
/plugin marketplace add philsquare/permissions/plugin install philsquare-permissions@philsquare-permissionsCreate a new Laravel policy for the model $model with role-based permissions using philsquare/permissions.
app/Policies/{$model}Policy.php<?php
namespace App\Policies;
use App\Models\{$model};
use App\Models\User;
use Philsquare\Permissions\BasePolicy;
class {$model}Policy extends BasePolicy
{
public function rolePermissions(): array
{
return [
'admin' => $this->permissions()->all(),
];
}
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return true;
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, {$model} $model): bool
{
return true;
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return true;
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, {$model} $model): bool
{
return true;
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, {$model} $model): bool
{
return true;
}
}
app/Enums/Roles.php exists and use it in rolePermissions() if availableapp/Providers/AuthServiceProvider.php if it uses explicit registrationrolePermissions() method with appropriate rolesphp artisan permissions:refresh to sync permissionsBasePolicy from philsquare/permissionstrue by default - add business logic as neededbefore() hook