2015-02-10 02:06:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Console\Command;
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Api\ConfigLoader;
|
|
|
|
use Api\Client;
|
2015-03-05 15:04:08 +01:00
|
|
|
use Api\ClientException;
|
2015-02-10 02:06:06 +01:00
|
|
|
|
|
|
|
class AuthLoginCommand extends Command
|
|
|
|
{
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('auth:login')
|
|
|
|
->setDescription('Login on t411')
|
2015-02-10 20:13:46 +01:00
|
|
|
->setHelp("<info>%command.name%</info>
|
|
|
|
|
|
|
|
Generate the config to access the API. You must have a valid login/password.
|
|
|
|
|
|
|
|
<comment>The login and the password are not saved.</comment>
|
|
|
|
|
|
|
|
Usage: <comment>auth:login</comment>");
|
2015-02-10 02:06:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$configLoader = new ConfigLoader();
|
|
|
|
$dialog = $this->getHelperSet()->get('dialog');
|
|
|
|
$client = new Client();
|
|
|
|
|
|
|
|
if ($configLoader->configExists()) {
|
2015-02-10 16:22:33 +01:00
|
|
|
$continue = $dialog->askConfirmation(
|
2015-02-10 02:06:06 +01:00
|
|
|
$output,
|
2015-02-10 16:22:33 +01:00
|
|
|
'<info>The configuration file already exists</info>. Do you want to continue? [yes] ',
|
|
|
|
true
|
2015-02-10 02:06:06 +01:00
|
|
|
);
|
|
|
|
|
2015-02-10 16:22:33 +01:00
|
|
|
if (!$continue) {
|
2015-02-10 02:06:06 +01:00
|
|
|
$output->writeln('Aborded.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$username = $dialog->ask($output, 'Username: ', null);
|
2015-02-10 16:22:33 +01:00
|
|
|
$password = $dialog->askHiddenResponse($output, 'Password (hidden): ', null);
|
2015-02-10 02:06:06 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $client->getAuthorization($username, $password);
|
|
|
|
|
|
|
|
if ($response->hasError()) {
|
|
|
|
$output->writeln(sprintf(
|
|
|
|
'Login failed: <error>%s</error> <comment>(%d)</comment>',
|
|
|
|
$response->getErrorMessage(),
|
|
|
|
$response->getErrorCode()
|
|
|
|
));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$configLoader->save(array(
|
|
|
|
'auth' => array(
|
|
|
|
'uid' => $response->getData()['uid'],
|
|
|
|
'token' => $response->getData()['token'],
|
|
|
|
)
|
|
|
|
));
|
|
|
|
} catch (ClientException $e) {
|
|
|
|
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|