From fee174ec05d1ee59a2674ba0f6a5f17455b6299e Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Thu, 2 Jul 2026 01:21:04 +0300 Subject: [PATCH] add admin command --- .../Commands/EnsureInitialAdminCommand.php | 36 +++++++++++++++++++ backend/app/Providers/AppServiceProvider.php | 6 ++++ ...ronmentInitialAdminCredentialsProvider.php | 31 ++++++++++++++++ backend/app/User/InitialAdminCredentials.php | 22 ++++++++++++ .../User/InitialAdminCredentialsProvider.php | 8 +++++ backend/bootstrap/app.php | 4 +++ .../EnsureInitialAdminCommandTest.php | 13 +++++++ 7 files changed, 120 insertions(+) create mode 100644 backend/app/Console/Commands/EnsureInitialAdminCommand.php create mode 100644 backend/app/User/EnvironmentInitialAdminCredentialsProvider.php create mode 100644 backend/app/User/InitialAdminCredentials.php create mode 100644 backend/app/User/InitialAdminCredentialsProvider.php diff --git a/backend/app/Console/Commands/EnsureInitialAdminCommand.php b/backend/app/Console/Commands/EnsureInitialAdminCommand.php new file mode 100644 index 0000000..5a2b9e3 --- /dev/null +++ b/backend/app/Console/Commands/EnsureInitialAdminCommand.php @@ -0,0 +1,36 @@ +credentialsProvider->getCredentials(); + + $this->ensureInitialAdmin->execute(new EnsureInitialAdminRequest( + email: $credentials->getEmail(), + password: $credentials->getPassword(), + )); + + $this->info('initial admin is ready'); + + return self::SUCCESS; + } +} diff --git a/backend/app/Providers/AppServiceProvider.php b/backend/app/Providers/AppServiceProvider.php index 1d67ad1..38d7550 100644 --- a/backend/app/Providers/AppServiceProvider.php +++ b/backend/app/Providers/AppServiceProvider.php @@ -11,6 +11,8 @@ use App\Auth\SystemClock; use App\Auth\TokenGenerator; use App\Shared\Files\Filesystem; use App\Shared\Files\LaravelFilesystem; +use App\User\EnvironmentInitialAdminCredentialsProvider; +use App\User\InitialAdminCredentialsProvider; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -33,6 +35,10 @@ class AppServiceProvider extends ServiceProvider Filesystem::class, LaravelFilesystem::class, ); + $this->app->bind( + InitialAdminCredentialsProvider::class, + EnvironmentInitialAdminCredentialsProvider::class, + ); $this->app->bind( AuthCookieSettings::class, function (): AuthCookieSettings { diff --git a/backend/app/User/EnvironmentInitialAdminCredentialsProvider.php b/backend/app/User/EnvironmentInitialAdminCredentialsProvider.php new file mode 100644 index 0000000..15c22ef --- /dev/null +++ b/backend/app/User/EnvironmentInitialAdminCredentialsProvider.php @@ -0,0 +1,31 @@ +requiredEnvironmentValue( + 'RABBI_GERZI_INITIAL_ADMIN_EMAIL', + ), + password: $this->requiredEnvironmentValue( + 'RABBI_GERZI_INITIAL_ADMIN_PASSWORD', + ), + ); + } + + private function requiredEnvironmentValue(string $key): string + { + $value = getenv($key); + if (! is_string($value) || trim($value) === '') { + throw new RuntimeException("$key is required"); + } + + return $value; + } +} diff --git a/backend/app/User/InitialAdminCredentials.php b/backend/app/User/InitialAdminCredentials.php new file mode 100644 index 0000000..97493b9 --- /dev/null +++ b/backend/app/User/InitialAdminCredentials.php @@ -0,0 +1,22 @@ +email; + } + + public function getPassword(): string + { + return $this->password; + } +} diff --git a/backend/app/User/InitialAdminCredentialsProvider.php b/backend/app/User/InitialAdminCredentialsProvider.php new file mode 100644 index 0000000..011523a --- /dev/null +++ b/backend/app/User/InitialAdminCredentialsProvider.php @@ -0,0 +1,8 @@ +withCommands([ + EnsureInitialAdminCommand::class, + ]) ->withMiddleware(function (Middleware $middleware): void { // }) diff --git a/backend/tests/Unit/User/UseCases/EnsureInitialAdminCommandTest.php b/backend/tests/Unit/User/UseCases/EnsureInitialAdminCommandTest.php index 93831a9..5a2467f 100644 --- a/backend/tests/Unit/User/UseCases/EnsureInitialAdminCommandTest.php +++ b/backend/tests/Unit/User/UseCases/EnsureInitialAdminCommandTest.php @@ -7,6 +7,7 @@ use App\Shared\ValueObject\EmailAddress; use App\User\InitialAdminCredentials; use App\User\InitialAdminCredentialsProvider; use App\User\UseCases\EnsureInitialAdmin\EnsureInitialAdmin; +use Illuminate\Container\Container; use Symfony\Component\Console\Tester\CommandTester; use Tests\Fakes\FakeHasher; use Tests\Fakes\FakeUserRepository; @@ -25,6 +26,7 @@ class EnsureInitialAdminCommandTest extends TestCase 'secret-password', ), ); + $command->setLaravel($this->consoleContainer()); $commandTester = new CommandTester($command); $statusCode = $commandTester->execute([]); @@ -67,4 +69,15 @@ class EnsureInitialAdminCommandTest extends TestCase } }; } + + private function consoleContainer(): Container + { + return new class extends Container + { + public function runningUnitTests(): bool + { + return true; + } + }; + } }