65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
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' => 'user-'.fake()->unique()->numerify('########').'@example.test',
|
|
'email_verified_at' => now(),
|
|
'password' => static::$password ??= Hash::make('password'),
|
|
'remember_token' => Str::random(10),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Indicate that the model's email address should be unverified.
|
|
*/
|
|
public function unverified(): static
|
|
{
|
|
return $this->state(fn (array $attributes) => [
|
|
'email_verified_at' => null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Legacy compatibility: a user created with is_admin => true is an operator,
|
|
* so give them the full-control Owner role (roles are seeded by migration).
|
|
*/
|
|
public function configure(): static
|
|
{
|
|
return $this->afterCreating(function (User $user) {
|
|
if ($user->is_admin && ! $user->hasAnyRole(User::OPERATOR_ROLES)) {
|
|
$user->assignRole('Owner');
|
|
}
|
|
});
|
|
}
|
|
|
|
/** Explicitly give the user an operator role. */
|
|
public function operator(string $role = 'Owner'): static
|
|
{
|
|
return $this->afterCreating(fn (User $user) => $user->syncRoles([$role]));
|
|
}
|
|
}
|