1. Go to this page and download the library: Download philsquare/permissions 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/ */
philsquare / permissions example snippets
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
}
namespace App\Policies;
use App\Models\Post;
use App\Models\User;
use Philsquare\Permissions\BasePolicy;
class PostPolicy extends BasePolicy
{
public function rolePermissions(): array
{
return [
'admin' => $this->permissions()->all(),
'editor' => $this->permissions()->crud(),
'viewer' => $this->permissions()->only(['viewAny', 'view']),
];
}
public function viewAny(User $user): bool
{
return true;
}
public function view(User $user, Post $post): bool
{
return true;
}
public function create(User $user): bool
{
return true;
}
public function update(User $user, Post $post): bool
{
return true;
}
public function delete(User $user, Post $post): bool
{
return true;
}
}
$user->assignRole('editor');
// Using Gate
Gate::authorize('update', $post);
// On User model
$user->can('update', $post);
// In Blade
@can('update', $post)
<button>Edit</button>
@endcan
use Illuminate\Contracts\Auth\Authenticatable;
public function before(Authenticatable $user, string $ability, mixed $subject = null): ?bool
{
if (! Feature::active('orders')) {
return false;
}
return parent::before($user, $ability, $subject);
}
public function update(User $user, Post $post): bool
{
// Only allow updates if post is draft OR user has force-update permission
return $post->status === 'draft' || $user->can('force-update', $post);
}
public function rolePermissions(): array
{
return [
'admin' => $this->permissions()->all(),
];
}
namespace App\Policies;
use App\Enums\OrderStatus;
use App\Models\Order;
use App\Models\User;
use Philsquare\Permissions\BasePolicy;
class OrderPolicy extends BasePolicy
{
public function rolePermissions(): array
{
return [
'admin' => $this->permissions()->all(),
'manager' => $this->permissions()->crud(['cancel', 'refund']),
'sales' => $this->permissions()->only(['viewAny', 'view', 'create']),
'support' => $this->permissions()->only(['viewAny', 'view', 'addNote']),
];
}
public function viewAny(User $user): bool
{
return true;
}
public function view(User $user, Order $order): bool
{
return true;
}
public function create(User $user): bool
{
return true;
}
public function update(User $user, Order $order): bool
{
// Can only update pending orders
return $order->status === OrderStatus::PENDING;
}
public function delete(User $user, Order $order): bool
{
// Can only delete draft orders with no items
return $order->status === OrderStatus::DRAFT
&& $order->items()->count() === 0;
}
public function cancel(User $user, Order $order): bool
{
return in_array($order->status, [
OrderStatus::PENDING,
OrderStatus::PROCESSING,
]);
}
public function refund(User $user, Order $order): bool
{
return $order->status === OrderStatus::COMPLETED
&& $order->paid_at !== null;
}
public function addNote(User $user, Order $order): bool
{
return true;
}
}