Skip to content

Commit

Permalink
Merge pull request #1570 from mnico/1542-drupalstyle-user
Browse files Browse the repository at this point in the history
[user] #1542 Implement DrupalStyle and fixed interact method
  • Loading branch information
jmolivas committed Dec 23, 2015
2 parents c718cb2 + b514391 commit 0073fb7
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 93 deletions.
75 changes: 38 additions & 37 deletions src/Command/User/LoginCleanAttemptsCommand.php
Expand Up @@ -31,48 +31,45 @@ protected function configure()
->addArgument('uid', InputArgument::REQUIRED, $this->trans('commands.user.login.clear.attempts.options.user-id'));
}

/**
* Verify if given User ID is valid value for question uid.
*
* @param int $uid User ID to check.
* @return int A valid User ID.
* @throws \Symfony\Component\Process\Exception\InvalidArgumentException
*/
public function validateQuestionsUid($uid)
{
// Check if $uid is numeric.
$message = (!is_numeric($uid)) ?
$this->trans('commands.user.login.clear.attempts.questions.numeric-uid') :
false;
// Check if $uid is upper than zero.
if (!$message && $uid <= 0) {
$message = $this->trans('commands.user.login.clear.attempts.questions.invalid-uid');
}
// Check if message was defined.
if ($message) {
throw new \InvalidArgumentException(
$message
);
}
// Return a valid $uid.
return (int) $uid;
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$output = new DrupalStyle($input, $output);
$io = new DrupalStyle($input, $output);

$uid = $input->getArgument('uid');
// Check if $uid argument is already set.
if (!$uid = $input->getArgument('uid')) {
// Request $uid argument.
$uid = $output->ask(
$this->trans('commands.user.login.clear.attempts.questions.uid'),
1,
$this->validateQuestionsUid($uid)
);
if (!$uid) {
while (true) {
// Request $uid argument.
$uid = $io->ask(
$this->trans('commands.user.login.clear.attempts.questions.uid'),
1,
function ($uid) use ($io) {
$message = (!is_numeric($uid)) ?
$this->trans('commands.user.login.clear.attempts.questions.numeric-uid') :
false;
// Check if $uid is upper than zero.
if (!$message && $uid <= 0) {
$message = $this->trans('commands.user.login.clear.attempts.questions.invalid-uid');
}
// Check if message was defined.
if ($message) {
$io->error($message);

return false;
}
// Return a valid $uid.
return (int) $uid;
}
);

if ($uid) {
break;
}
}

$input->setArgument('uid', $uid);
}
}
Expand All @@ -82,17 +79,21 @@ protected function interact(InputInterface $input, OutputInterface $output)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$uid = $input->getArgument('uid');
$account = User::load($uid);

if (!$account) {
// Error loading User entity.
throw new \InvalidArgumentException(
$io->error(
sprintf(
$this->trans('commands.user.login.clear.attempts.errors.invalid-user'),
$uid
)
);

return;
}

// Define event name and identifier.
Expand All @@ -110,7 +111,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
->execute();

// Command executed successful.
$output->success(
$io->success(
sprintf(
$this->trans('commands.user.login.clear.attempts.messages.successful'),
$uid
Expand Down
10 changes: 6 additions & 4 deletions src/Command/User/LoginUrlCommand.php
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Console\Command\ContainerAwareCommand;
use Drupal\Console\Style\DrupalStyle;

/**
* Class UserLoginCommand.
Expand Down Expand Up @@ -41,20 +42,21 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$uid = $input->getArgument('user-id');
$user = $this->getEntityManager()->getStorage('user')->load($uid);
$message = $this->getMessageHelper();

if (!$user) {
$text = $this->trans('commands.user.login.url.errors.invalid-user');
$text = SafeMarkup::format($text, [':uid' => $uid]);
$message->addErrorMessage($text);
$text = SafeMarkup::format($text, ['@uid' => $uid]);
$io->error($text);
return;
}

$url = user_pass_reset_url($user);
$text = $this->trans('commands.user.login.url.messages.url');
$text = SafeMarkup::format($text, ['@name' => $user->getUsername(), '@url' => $url]);
$message->addSuccessMessage($text);
$io->success($text);
}
}
50 changes: 27 additions & 23 deletions src/Command/User/PasswordHashCommand.php
Expand Up @@ -12,7 +12,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Command\ConfirmationTrait;
use Drupal\Console\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
use Drupal\Console\Style\DrupalStyle;

class PasswordHashCommand extends ContainerAwareCommand
Expand All @@ -36,62 +35,67 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$passwords = $input->getArgument('password');

$passHandler = $this->getPassHandler();

$table = new Table($output);
$table->setHeaders(
[
$this->trans('commands.user.password.hash.messages.password'),
$this->trans('commands.user.password.hash.messages.hash'),
]
);

$table->setStyle('compact');
$tableHeader = [
$this->trans('commands.user.password.hash.messages.password'),
$this->trans('commands.user.password.hash.messages.hash'),
];

$tableRows = [];
foreach ($passwords as $password) {
$table->addRow(
[
$password,
$passHandler->hash($password),
]
);
$tableRows[] = [
$password,
$passHandler->hash($password),
];
}

$table->render();
$io->table($tableHeader, $tableRows, 'compact');
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$output = new DrupalStyle($input, $output);
$io = new DrupalStyle($input, $output);

$passwords = $input->getArgument('password');
if (!$passwords) {
$passwords = [];
while (true) {
$password = $output->ask(
$password = $io->ask(
$this->trans('commands.user.password.hash.questions.password'),
'',
function ($pass) use ($passwords) {
function ($pass) use ($passwords, $io) {
if (!empty($pass) || count($passwords) >= 1) {
if ($pass == '') {

return true;
}

return $pass;
} else {
throw new \InvalidArgumentException(
$io->error(
sprintf($this->trans('commands.user.password.hash.questions.invalid-pass'), $pass)
);

return false;
}
}
);

if (empty($password)) {
if ($password && !is_string($password)) {
break;
}

$passwords[] = $password;
if (is_string($password)) {
$passwords[] = $password;
}
}

$input->setArgument('password', $passwords);
Expand Down
89 changes: 60 additions & 29 deletions src/Command/User/PasswordResetCommand.php
Expand Up @@ -36,29 +36,33 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new DrupalStyle($input, $output);
$io = new DrupalStyle($input, $output);

$uid = $input->getArgument('user');

$user = User::load($uid);

if (!$user) {
throw new \InvalidArgumentException(
$io->error(
sprintf(
$this->trans('commands.user.password.reset.errors.invalid-user'),
$uid
)
);

return;
}

$password = $input->getArgument('password');
if (!$password) {
throw new \InvalidArgumentException(
$io->error(
sprintf(
$this->trans('commands.user.password.reset.errors.empty-password'),
$uid
)
);

return;
}

try {
Expand All @@ -68,10 +72,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->getChain()
->addCommand('user:login:clear:attempts', ['uid' => $uid]);
} catch (\Exception $e) {
throw new \InvalidArgumentException($e->getMessage());
$io->error($e->getMessage());

return;
}

$output->success(
$io->success(
sprintf(
$this->trans('commands.user.password.reset.messages.reset-successful'),
$uid
Expand All @@ -84,42 +90,67 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$output = new DrupalStyle($input, $output);
$io = new DrupalStyle($input, $output);

$user = $input->getArgument('user');
if (!$user) {
$user = $output->ask(
$this->trans('commands.user.password.reset.questions.user'),
'',
function ($uid) {
$uid = (int) $uid;
if (is_int($uid) && $uid > 0) {
return $uid;
} else {
throw new \InvalidArgumentException(
sprintf($this->trans('commands.user.password.reset.questions.invalid-uid'), $uid)
);

while (true) {
$user = $io->ask(
$this->trans('commands.user.password.reset.questions.user'),
'',
function ($uid) use ($io) {
if ($uid) {
$uid = (int) $uid;
if (is_int($uid) && $uid > 0) {
return $uid;
} else {
$io->error(
sprintf($this->trans('commands.user.password.reset.questions.invalid-uid'), $uid)
);

return false;
}
}
}
);

if ($user) {
break;
}
);
}

$input->setArgument('user', $user);
}

$password = $input->getArgument('password');
if (!$password) {
$password = $output->ask(
$this->trans('commands.user.password.hash.questions.password'),
'',
function ($pass) {
if (!empty($pass)) {
return $pass;
} else {
throw new \InvalidArgumentException(
sprintf($this->trans('commands.user.password.hash.questions.invalid-pass'), $pass)
);

while(true) {
$password = $io->ask(
$this->trans('commands.user.password.hash.questions.password'),
'',
function ($pass) use ($io) {
if ($pass) {
if (!empty($pass)) {
return $pass;
} else {
$io->error(
sprintf($this->trans('commands.user.password.hash.questions.invalid-pass'), $pass)
);

return false;
}
}

}
);

if ($password) {
break;
}
);
}


$input->setArgument('password', $password);
}
Expand Down

0 comments on commit 0073fb7

Please sign in to comment.