Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to change the password of a user #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions Command/ChangePasswordCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\UserBundle\Command;

use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\User\Model\UserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

/**
* @author Loïc Frémont <[email protected]>
*/
class ChangePasswordCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('sylius:user:change-password')
->setDescription('Change the password of a user.')
->setDefinition(array(
new InputArgument('email', InputArgument::REQUIRED, 'The email'),
new InputArgument('password', InputArgument::REQUIRED, 'The password'),
))
->setHelp(<<<EOT
The <info>sylius:user:change-password</info> command changes the password of a user:
<info>php app/console sylius:user:change-password matthieu</info>
This interactive shell will first ask you for a password.
You can alternatively specify the password as a second argument:
<info>php app/console fos:user:change-password matthieu mypassword</info>
EOT
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$email = $input->getArgument('email');
$password = $input->getArgument('password');

/** @var UserInterface $user */
$user = $this->getUserRepository()->findOneByEmail($email);

if (null === $user) {
throw new \InvalidArgumentException(sprintf('Could not find user by email "%s"', $email));
}

$this->changePassword($user, $password);
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();

$output->writeln(sprintf('Change password user <comment>%s</comment>', $email));
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('email')) {
$email = $this->getHelper('dialog')->askAndValidate(
$output,
'Please enter an email:',
function($username) {
if (empty($username)) {
throw new \Exception('Email can not be empty');
}
return $username;
}
);

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

if (!$input->getArgument('password')) {
$password = $this->getHelper('dialog')->askHiddenResponseAndValidate(
$output,
'Please choose a password:',
function($password) {
if (empty($password)) {
throw new \Exception('Password can not be empty');
}
return $password;
}
);

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

/**
* @param UserInterface $user
* @param string $password
*/
protected function changePassword(UserInterface $user, $password)
{
$user->setPlainPassword($password);
$this->getContainer()->get('sylius.user.password_updater')->updatePassword($user);
}

/**
* @return EntityManagerInterface
*/
protected function getEntityManager()
{
return $this->getContainer()->get('sylius.manager.user');
}

/**
* @return UserRepositoryInterface
*/
protected function getUserRepository()
{
return $this->getContainer()->get('sylius.repository.user');
}
}