Files
pricebuddy/app/Policies/UserPolicy.php
2026-06-13 13:21:33 +10:00

58 lines
1.2 KiB
PHP

<?php
namespace App\Policies;
use App\Enums\Role;
use App\Models\User;
class UserPolicy
{
/**
* Determine whether the user can view the list of users.
*/
public function viewAny(User $user): bool
{
return $user->isAdmin();
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, User $model): bool
{
return $user->isAdmin() || $user->is($model);
}
/**
* Determine whether the user can create users.
*/
public function create(User $user): bool
{
return $user->isAdmin();
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, User $model): bool
{
return $user->isAdmin() || $user->is($model);
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, User $model): bool
{
if ($user->is($model)) {
return false;
}
if ($model->isAdmin() && User::where('role', Role::Admin)->count() <= 1) {
return false;
}
return $user->isAdmin();
}
}