add user:promote artisan command
Marks the user with the given email as an admin. Used by the cypress harness to bootstrap an admin without a public promote endpoint and is also useful for ops.
This commit is contained in:
parent
eb59f4ca2d
commit
30738b163d
1 changed files with 59 additions and 0 deletions
59
backend/app/Console/Commands/UserPromoteCommand.php
Normal file
59
backend/app/Console/Commands/UserPromoteCommand.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\User\User;
|
||||
use App\User\UserRepository;
|
||||
use Illuminate\Console\Command;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class UserPromoteCommand extends Command
|
||||
{
|
||||
protected $signature = 'user:promote {email}';
|
||||
|
||||
protected $description = 'Mark the user with the given email as an admin';
|
||||
|
||||
public function handle(UserRepository $userRepo): int
|
||||
{
|
||||
$rawEmail = $this->argument('email');
|
||||
if (! is_string($rawEmail) || $rawEmail === '') {
|
||||
$this->error('email is required');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
try {
|
||||
$email = new EmailAddress($rawEmail);
|
||||
} catch (InvalidArgumentException $exception) {
|
||||
$this->error($exception->getMessage());
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$user = $userRepo->findByEmail($email);
|
||||
if ($user === null) {
|
||||
$this->error("user not found: {$rawEmail}");
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
if ($user->isAdmin()) {
|
||||
$this->info("{$rawEmail} is already an admin");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$userRepo->update(new User(
|
||||
id: $user->getId(),
|
||||
email: $user->getEmail(),
|
||||
displayName: $user->getDisplayName(),
|
||||
passwordHash: $user->getPasswordHash(),
|
||||
isAdmin: true,
|
||||
emailConfirmedAt: $user->getEmailConfirmedAt(),
|
||||
));
|
||||
$this->info("{$rawEmail} is now an admin");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue