* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ namespace Chill\MainBundle\Command; use Doctrine\ORM\EntityManager; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; use Chill\MainBundle\Entity\User; use Symfony\Component\Security\Core\Encoder\EncoderFactory; use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder; use Symfony\Component\Security\Core\Security; /** * Class SetPasswordCommand * * @package Chill\MainBundle\Command * @author Julien Fastré */ class SetPasswordCommand extends Command { /** * @var EntityManager */ private $entityManager; /** * SetPasswordCommand constructor. * * @param EntityManager $entityManager */ public function __construct(EntityManager $entityManager) { $this->entityManager = $entityManager; parent::__construct(); } public function configure() { $this->setName('chill:user:set_password') ->setDescription('set a password to user') ->addArgument('username', InputArgument::REQUIRED, 'the user\'s ' . 'username you want to change password') ->addArgument('password', InputArgument::OPTIONAL, 'the new password') ; } public function execute(InputInterface $input, OutputInterface $output) { $user = $this->_getUser($input->getArgument('username')); if ($user === NULL) { throw new \LogicException("The user with username '". $input->getArgument('username')."' is not found"); } $password = $input->getArgument('password'); if ($password === NULL) { $dialog = $this->getHelperSet()->get('dialog'); $password = $dialog->askHiddenResponse($output, "the new password :" . ""); } $this->_setPassword($user, $password); } public function _getUser($username) { return $this->entityManager ->getRepository('ChillMainBundle:User') ->findOneBy(array('username' => $username)); } public function _setPassword(User $user, $password) { $defaultEncoder = new MessageDigestPasswordEncoder('sha512', true, 5000); $encoders = [ User::class => $defaultEncoder ]; $encoderFactory = new EncoderFactory($encoders); $user->setPassword( $encoderFactory->getEncoder($user)->encodePassword($password, $user->getSalt()) ); $this->entityManager->flush($user); } }