44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Enums\UserRole;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Creates the first admin panel account from the ADMIN_* settings in .env.
|
|
* An existing account is left untouched, so re-seeding never resets a
|
|
* password that was changed from the panel.
|
|
*/
|
|
class AdminUserSeeder extends Seeder
|
|
{
|
|
public function run(): void
|
|
{
|
|
$email = config('app.admin.email');
|
|
|
|
if (User::query()->where('email', $email)->exists()) {
|
|
$this->command?->info("Admin {$email} sudah ada, dilewati.");
|
|
|
|
return;
|
|
}
|
|
|
|
$password = config('app.admin.password') ?: Str::password(16, symbols: false);
|
|
|
|
$user = User::query()->create([
|
|
'name' => config('app.admin.name'),
|
|
'email' => $email,
|
|
'role' => UserRole::Admin,
|
|
'password' => $password,
|
|
]);
|
|
$user->forceFill(['email_verified_at' => now()])->save();
|
|
|
|
if (config('app.admin.password')) {
|
|
$this->command?->info("Admin {$email} dibuat dengan ADMIN_PASSWORD dari .env.");
|
|
} else {
|
|
$this->command?->warn("Admin {$email} dibuat. Password (simpan sekarang, hanya ditampilkan sekali): {$password}");
|
|
}
|
|
}
|
|
}
|