Skip to content

Commit

Permalink
Merge pull request #4847 from coollabsio/feat-create-initial-user-via…
Browse files Browse the repository at this point in the history
…-env

feat: Ability to create root user via environment variables
  • Loading branch information
andrasbacsai authored Jan 16, 2025
2 parents dd29348 + a43805e commit 8f32c32
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ REDIS_PASSWORD=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

ROOT_USERNAME=
ROOT_USER_EMAIL=
ROOT_USER_PASSWORD=
4 changes: 4 additions & 0 deletions database/seeders/ProductionSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ public function run(): void
]);
}
}

if (InstanceSettings::find(0) == null) {
InstanceSettings::create([
'id' => 0,
]);
}

$this->call(RootUserSeeder::class);

if (GithubApp::find(0) == null) {
GithubApp::create([
'id' => 0,
Expand Down
74 changes: 74 additions & 0 deletions database/seeders/RootUserSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Database\Seeders;

use App\Models\InstanceSettings;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Password;

class RootUserSeeder extends Seeder
{
public function run(): void
{
try {
if (User::where('id', 0)->exists()) {
echo "\n INFO Root user already exists. Skipping creation.\n\n";

return;
}

if (! env('ROOT_USER_EMAIL') || ! env('ROOT_USER_PASSWORD')) {
return;
}

$validator = Validator::make([
'email' => env('ROOT_USER_EMAIL'),
'username' => env('ROOT_USERNAME', 'Root User'),
'password' => env('ROOT_USER_PASSWORD'),
], [
'email' => ['required', 'email:rfc,dns', 'max:255'],
'username' => ['required', 'string', 'min:3', 'max:255', 'regex:/^[\w\s-]+$/'],
'password' => ['required', 'string', 'min:8', Password::min(8)->mixedCase()->letters()->numbers()->symbols()->uncompromised()],
]);

if ($validator->fails()) {
echo "\n ERROR Invalid Root User Environment Variables\n";
foreach ($validator->errors()->all() as $error) {
echo "{$error}\n";
}
echo "\n";

return;
}

try {
User::create([
'id' => 0,
'name' => env('ROOT_USERNAME', 'Root User'),
'email' => env('ROOT_USER_EMAIL'),
'password' => Hash::make(env('ROOT_USER_PASSWORD')),
]);
echo "\n SUCCESS Root user created successfully.\n\n";
} catch (\Exception $e) {
echo "\n ERROR Failed to create root user: {$e->getMessage()}\n\n";

return;
}

try {
InstanceSettings::updateOrCreate(
['id' => 0],
['is_registration_enabled' => false]
);
echo "\n SUCCESS Registration has been disabled successfully.\n\n";
} catch (\Exception $e) {
echo "\n ERROR Failed to update instance settings: {$e->getMessage()}\n\n";
}
} catch (\Exception $e) {
echo "\n ERROR An unexpected error occurred: {$e->getMessage()}\n\n";
}
}
}

0 comments on commit 8f32c32

Please sign in to comment.