update command user:password

This commit is contained in:
aynsix
2020-02-28 18:31:29 +03:00
parent f80506f55e
commit 3d4d3aed36
3 changed files with 142 additions and 81 deletions

View File

@@ -49,7 +49,7 @@ use Alchemy\Phrasea\Command\Task\TaskStart;
use Alchemy\Phrasea\Command\Task\TaskState; use Alchemy\Phrasea\Command\Task\TaskState;
use Alchemy\Phrasea\Command\Task\TaskStop; use Alchemy\Phrasea\Command\Task\TaskStop;
use Alchemy\Phrasea\Command\User\UserCreateCommand; use Alchemy\Phrasea\Command\User\UserCreateCommand;
use Alchemy\Phrasea\Command\User\UserSetPasswordCommand; use Alchemy\Phrasea\Command\User\UserPasswordCommand;
use Alchemy\Phrasea\Command\User\UserListCommand; use Alchemy\Phrasea\Command\User\UserListCommand;
use Alchemy\Phrasea\Command\UpgradeDBDatas; use Alchemy\Phrasea\Command\UpgradeDBDatas;
@@ -120,7 +120,7 @@ $cli->command(new CreateDataboxCommand('databox:create'));
$cli->command(new UserCreateCommand('user:create')); $cli->command(new UserCreateCommand('user:create'));
$cli->command(new UserSetPasswordCommand('user:set-password')); $cli->command(new UserPasswordCommand('user:password'));
$cli->command(new UserListCommand('user:list')); $cli->command(new UserListCommand('user:list'));

View File

@@ -0,0 +1,140 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Command\User;
use Alchemy\Phrasea\Application\Helper\NotifierAware;
use Alchemy\Phrasea\Command\Command;
use Alchemy\Phrasea\Core\LazyLocator;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Notification\Receiver;
use Alchemy\Phrasea\Notification\Mail\MailRequestPasswordSetup;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
class UserPasswordCommand extends Command
{
use NotifierAware;
/**
* Constructor
*/
public function __construct($name = null)
{
parent::__construct('user:password');
$this->setDescription('Set user password in Phraseanet')
->addOption('user_id', null, InputOption::VALUE_REQUIRED, 'The id of user.')
->addOption('generate', null, InputOption::VALUE_NONE, 'Generate the password')
->addOption('password', null, InputOption::VALUE_OPTIONAL, 'The password')
->addOption('send_mail_password', null, InputOption::VALUE_NONE, 'Send email link to user for password renewing, work only if password and generate are not define')
->addOption('password_hash', null, InputOption::VALUE_OPTIONAL, 'Define a password hashed, work only with password_nonce')
->addOption('password_nonce', null, InputOption::VALUE_OPTIONAL, 'Define a password nonce, work only with password_hash')
->addOption('get_hash', null, InputOption::VALUE_NONE, 'return the password hashed')
->setHelp('');
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getHelperSet()->get('dialog');
$userRepository = $this->container['repo.users'];
$userManipulator = $this->container['manipulator.user'];
$user = $userRepository->find($input->getOption('user_id'));
$password = $input->getOption('password');
$generate = $input->getOption('generate');
$sendMailPassword = $input->getOption('send_mail_password');
$getHash = $input->getOption('get_hash');
$passwordHash = $input->getOption('password_hash');
$passwordNonce = $input->getOption('password_nonce');
if ($user === null) {
$output->writeln('<info>Not found User.</info>');
return 0;
}
if ($passwordHash && $passwordNonce) {
$user->setNonce($passwordNonce);
$user->setPassword($passwordHash);
$userManipulator->updateUser($user);
$output->writeln('<info>password set with hashed pass</info>');
return 0;
}
if ($generate) {
$password = $this->container['random.medium']->generateString(64);
} else {
if (!$password && $sendMailPassword) {
$this->sendPasswordSetupMail($user);
return 0;
} elseif (!$password && !$sendMailPassword && ! $getHash) {
$output->writeln('<error>choose one option to set a password!</error>');
return 0;
}
}
if ($password) {
do {
$continue = mb_strtolower($dialog->ask($output, '<question>Do you want really set password to this user? (y/N)</question>', 'N'));
} while (!in_array($continue, ['y', 'n']));
if ($continue !== 'y') {
$output->writeln('Aborting !');
return;
}
$userManipulator->setPassword($user,$password);
}
if (($password || $generate || $getHash) && $user->getPassword()) {
$hash = [
'password' => $user->getPassword(),
'nonce' => $user->getNonce()
];
echo json_encode($hash);
} elseif (is_null($password)) {
$output->writeln('<info>password undefined</info>');
}
return 0;
}
/**
* Send mail for renew password
* @param User $user
*/
private function sendPasswordSetupMail(User $user)
{
$this->setDelivererLocator(new LazyLocator($this->container, 'notification.deliverer'));
$receiver = Receiver::fromUser($user);
$token = $this->container['manipulator.token']->createResetPasswordToken($user);
$mail = MailRequestPasswordSetup::create($this->container, $receiver);
$servername = $this->container['conf']->get('servername');
$mail->setButtonUrl('http://'.$servername.'/login/renew-password/?token='.$token->getValue());
$mail->setLogin($user->getLogin());
$this->deliver($mail);
}
}

View File

@@ -1,79 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Command\User;
use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
class UserSetPasswordCommand extends Command
{
/**
* Constructor
*/
public function __construct($name = null)
{
parent::__construct('user:set-password');
$this->setDescription('Set user password in Phraseanet')
->addOption('user_id', null, InputOption::VALUE_REQUIRED, 'The id of user.')
->addOption('generate', null, InputOption::VALUE_NONE, 'Generate the password')
->addOption('password', null, InputOption::VALUE_OPTIONAL, 'The password')
->setHelp('');
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getHelperSet()->get('dialog');
$userRepository = $this->container['repo.users'];
$userManipulator = $this->container['manipulator.user'];
$user = $userRepository->find($input->getOption('user_id'));
$password = $input->getOption('password');
$generate = $input->getOption('generate');
if ($user === null) {
$output->writeln('<info>Not found User.</info>');
return 0;
}
if ($generate) {
$password = $this->container['random.medium']->generateString(64);
} else {
if (!$password) {
$output->writeln('<error>--password option not specified</error>');
return 0;
}
}
do {
$continue = mb_strtolower($dialog->ask($output, '<question>Do you want really set password to this user? (y/N)</question>', 'N'));
} while (!in_array($continue, ['y', 'n']));
if ($continue !== 'y') {
$output->writeln('Aborting !');
return;
}
$userManipulator->setPassword($user,$password);
$output->writeln('New password: <info>' . $password . '</info>');
return 0;
}
}