MAPG-115 make possible to add new users via command line
This commit is contained in:
parent
e8f9d74283
commit
d93a758cd2
1
mapg
1
mapg
@ -6,5 +6,6 @@ require 'main.php';
|
||||
$app = new Symfony\Component\Console\Application('MapGuesser Console', '');
|
||||
|
||||
$app->add(new MapGuesser\Cli\DatabaseMigration());
|
||||
$app->add(new MapGuesser\Cli\AddUserCommand());
|
||||
|
||||
$app->run();
|
||||
|
50
src/Cli/AddUserCommand.php
Normal file
50
src/Cli/AddUserCommand.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php namespace MapGuesser\Cli;
|
||||
|
||||
use MapGuesser\Database\Query\Modify;
|
||||
use MapGuesser\Model\User;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class AddUserCommand extends Command
|
||||
{
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('user:add')
|
||||
->setDescription('Adding of user.')
|
||||
->addArgument('email', InputArgument::REQUIRED, 'Email of user')
|
||||
->addArgument('password', InputArgument::REQUIRED, 'Password of user')
|
||||
->addArgument('type', InputArgument::OPTIONAL, 'Type of user');;
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$user = new User([
|
||||
'email' => $input->getArgument('email'),
|
||||
'password' => $input->getArgument('password')
|
||||
]);
|
||||
|
||||
if ($input->hasArgument('type')) {
|
||||
$user->setType($input->getArgument('type'));
|
||||
}
|
||||
|
||||
try {
|
||||
$modify = new Modify(\Container::$dbConnection, 'users');
|
||||
$modify->fill($user->toArray());
|
||||
$modify->save();
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln('<error>Adding user failed!</error>');
|
||||
$output->writeln('');
|
||||
|
||||
$output->writeln((string) $e);
|
||||
$output->writeln('');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
$output->writeln('<info>User was successfully added!</info>');
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user