From 4b6690e5999a56f4fea7c3636bb096a10bd26e3f Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Thu, 16 Jan 2025 17:51:26 +0100 Subject: [PATCH] fix: RootUserSeeder - ensure that the existing root user is not overwritten - ensure that the seeder can only be run once - creating the initial root user --- database/seeders/RootUserSeeder.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/database/seeders/RootUserSeeder.php b/database/seeders/RootUserSeeder.php index 2e841503d..b356d8cf2 100644 --- a/database/seeders/RootUserSeeder.php +++ b/database/seeders/RootUserSeeder.php @@ -11,22 +11,26 @@ class RootUserSeeder extends Seeder { public function run(): void { + if (User::where('id', 0)->exists()) { + echo " Root user already exists. Skipping creation.\n"; + + return; + } + if (env('ROOT_USER_EMAIL') && env('ROOT_USER_PASSWORD')) { - User::updateOrCreate( - ['id' => 0], - [ - 'name' => env('ROOT_USER_NAME', 'Root User'), - 'email' => env('ROOT_USER_EMAIL'), - 'password' => Hash::make(env('ROOT_USER_PASSWORD')), - ] - ); + User::create([ + 'id' => 0, + 'name' => env('ROOT_USERNAME', 'Root User'), + 'email' => env('ROOT_USER_EMAIL'), + 'password' => Hash::make(env('ROOT_USER_PASSWORD')), + ]); InstanceSettings::updateOrCreate( ['id' => 0], ['is_registration_enabled' => false] ); - echo " Root user created/updated successfully.\n"; + echo " Root user created successfully.\n"; echo " Registration has been disabled.\n"; } else { echo " Warning: ROOT_USER_EMAIL and ROOT_USER_PASSWORD environment variables are required for root user creation.\n";