Files
Jeremy d2ca105091 test: cover UserResource role-based page access
feat: add Role enum (Admin/User)

feat: add role column to users, backfill existing to admin

feat: cast user role to enum and add isAdmin helper

feat: user factory defaults to User role with admin() state

feat: add UserPolicy (admin manages all, users manage self)

feat: restrict AppSettingsPage to admins

feat: admin-only role select on user form with role-based resource access

test: prove non-admins cannot escalate role; guard global search

Add canGloballySearch() to UserResource so the user list is never
surfaced in Filament global search for non-admins (defense-in-depth
alongside the broad canAccess() needed for self-edit). Commit the
adversarial escalation regression tests that prove both fillForm and
raw data.role injection cannot persist a role change for a non-admin.

feat: seed test@test.com as admin, another@test.com as user
2026-06-13 13:08:33 +10:00

71 lines
1.6 KiB
PHP

<?php
namespace Database\Factories;
use App\Enums\Role;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
/**
* @extends Factory<User>
*/
class UserFactory extends Factory
{
/**
* The current password being used by the factory.
*/
protected static ?string $password;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'settings' => [],
'role' => Role::User,
];
}
/**
* Indicate that the model's email address should be unverified.
*/
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}
/**
* Indicate that the user is an Admin.
*/
public function admin(): static
{
return $this->state(fn (array $attributes) => [
'role' => Role::Admin,
]);
}
/**
* Configure notification settings.
*/
public function withNotificationSettings(array $settings): static
{
return $this->state(fn (array $attributes) => [
'settings' => [
'notifications' => $settings,
],
]);
}
}