entityManager = $entityManager; parent::__construct(); } public function _getUser($username) { return $this->entityManager ->getRepository(\Chill\MainBundle\Entity\User::class) ->findOneBy(['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); } 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 (null === $user) { throw new LogicException("The user with username '" . $input->getArgument('username') . "' is not found"); } $password = $input->getArgument('password'); if (null === $password) { $dialog = $this->getHelperSet()->get('dialog'); $password = $dialog->askHiddenResponse($output, 'the new password :' . ''); } $this->_setPassword($user, $password); } }