add admin command
This commit is contained in:
parent
4462879b7d
commit
fee174ec05
7 changed files with 120 additions and 0 deletions
36
backend/app/Console/Commands/EnsureInitialAdminCommand.php
Normal file
36
backend/app/Console/Commands/EnsureInitialAdminCommand.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\User\InitialAdminCredentialsProvider;
|
||||
use App\User\UseCases\EnsureInitialAdmin\EnsureInitialAdmin;
|
||||
use App\User\UseCases\EnsureInitialAdmin\EnsureInitialAdminRequest;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class EnsureInitialAdminCommand extends Command
|
||||
{
|
||||
protected $signature = 'rabbi-gerzi:ensure-initial-admin';
|
||||
|
||||
protected $description = 'Create the configured initial admin if missing';
|
||||
|
||||
public function __construct(
|
||||
private EnsureInitialAdmin $ensureInitialAdmin,
|
||||
private InitialAdminCredentialsProvider $credentialsProvider,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$credentials = $this->credentialsProvider->getCredentials();
|
||||
|
||||
$this->ensureInitialAdmin->execute(new EnsureInitialAdminRequest(
|
||||
email: $credentials->getEmail(),
|
||||
password: $credentials->getPassword(),
|
||||
));
|
||||
|
||||
$this->info('initial admin is ready');
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace App\User;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class EnvironmentInitialAdminCredentialsProvider implements
|
||||
InitialAdminCredentialsProvider
|
||||
{
|
||||
public function getCredentials(): InitialAdminCredentials
|
||||
{
|
||||
return new InitialAdminCredentials(
|
||||
email: $this->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;
|
||||
}
|
||||
}
|
||||
22
backend/app/User/InitialAdminCredentials.php
Normal file
22
backend/app/User/InitialAdminCredentials.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\User;
|
||||
|
||||
class InitialAdminCredentials
|
||||
{
|
||||
public function __construct(
|
||||
private string $email,
|
||||
private string $password,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getEmail(): string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function getPassword(): string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
}
|
||||
8
backend/app/User/InitialAdminCredentialsProvider.php
Normal file
8
backend/app/User/InitialAdminCredentialsProvider.php
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace App\User;
|
||||
|
||||
interface InitialAdminCredentialsProvider
|
||||
{
|
||||
public function getCredentials(): InitialAdminCredentials;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue