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.
59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?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;
|
|
}
|
|
}
|