You've already forked php-auth
generated from siteworxpro/Php-Template
83 lines
3.0 KiB
PHP
83 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Siteworxpro\App\Cli\Commands\User;
|
|
|
|
use Siteworxpro\App\Cli\ClimateOutput;
|
|
use Siteworxpro\App\Cli\Commands\Command;
|
|
use Siteworxpro\App\CommandBus\Commands\CreateUser;
|
|
use Siteworxpro\App\Models\ClientUser;
|
|
use Siteworxpro\App\Models\User;
|
|
use Siteworxpro\App\Services\Facades\CommandBus;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Command\Command as SCommand;
|
|
use Symfony\Component\Console\Helper\QuestionHelper;
|
|
use Symfony\Component\Console\Input\ArgvInput;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Question\Question as QuestionInput;
|
|
|
|
#[AsCommand('user:add', 'Add a new user associated with an OAuth client')]
|
|
class Add extends Command
|
|
{
|
|
public function __invoke(ClimateOutput|ArgvInput|InputInterface $input, $output): int
|
|
{
|
|
$client = $this->askForClient($output, $input);
|
|
if (!$client) {
|
|
$output->error('Client not found.');
|
|
return SCommand::FAILURE;
|
|
}
|
|
|
|
$output->info("Adding a new user for client: $client->name");
|
|
$emailQuestion = new QuestionInput('Enter the user\'s email: ');
|
|
$emailQuestion->setValidator(function ($input): string {
|
|
if (empty($input) || !filter_var($input, FILTER_VALIDATE_EMAIL)) {
|
|
throw new \InvalidArgumentException('Invalid email address.');
|
|
}
|
|
|
|
return $input;
|
|
});
|
|
|
|
/** @var QuestionHelper $helper */
|
|
$helper = $this->getHelper('question');
|
|
|
|
$email = $helper->ask($input, $output, $emailQuestion);
|
|
|
|
/** @var User| null $user */
|
|
$user = User::where('email', $email)->first();
|
|
if ($user) {
|
|
$output->yellow('A user with this email already exists. Associating the user with the client.');
|
|
$clientUser = new ClientUser();
|
|
$clientUser->client_id = $client->id;
|
|
$clientUser->user_id = $user->id;
|
|
$clientUser->save();
|
|
|
|
return SCommand::SUCCESS;
|
|
}
|
|
|
|
$passwordQuestion = new QuestionInput('Enter the user\'s password: ');
|
|
$passwordQuestion->setHidden(true);
|
|
$passwordQuestion->setHiddenFallback(false);
|
|
$password = $helper->ask($input, $output, $passwordQuestion);
|
|
|
|
$firstNameQuestion = new QuestionInput('Enter the user\'s first name: ');
|
|
$firstName = $helper->ask($input, $output, $firstNameQuestion);
|
|
|
|
$lastNameQuestion = new QuestionInput('Enter the user\'s last name: ');
|
|
$lastName = $helper->ask($input, $output, $lastNameQuestion);
|
|
|
|
$createUserCommand = new CreateUser($client, $email, $password, $firstName, $lastName);
|
|
|
|
/** @var User $user */
|
|
$user = CommandBus::handle($createUserCommand);
|
|
|
|
$output->green('User added and associated with the client successfully.');
|
|
$output->info('User Details:');
|
|
$output->out("ID: $user->id");
|
|
$output->out("Email: $user->email");
|
|
$output->out("Name: $user->first_name $user->last_name");
|
|
|
|
return SCommand::SUCCESS;
|
|
}
|
|
}
|