ChangePasswordCommand.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Command;
  11. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use FOS\UserBundle\Model\User;
  16. /**
  17. * CreateUserCommand
  18. */
  19. class ChangePasswordCommand extends ContainerAwareCommand
  20. {
  21. /**
  22. * @see Command
  23. */
  24. protected function configure()
  25. {
  26. $this
  27. ->setName('fos:user:change-password')
  28. ->setDescription('Change the password of a user.')
  29. ->setDefinition(array(
  30. new InputArgument('username', InputArgument::REQUIRED, 'The username'),
  31. new InputArgument('password', InputArgument::REQUIRED, 'The password'),
  32. ))
  33. ->setHelp(<<<EOT
  34. The <info>fos:user:change-password</info> command changes the password of a user:
  35. <info>php app/console fos:user:change-password matthieu</info>
  36. This interactive shell will first ask you for a password.
  37. You can alternatively specify the password as a second argument:
  38. <info>php app/console fos:user:change-password matthieu mypassword</info>
  39. EOT
  40. );
  41. }
  42. /**
  43. * @see Command
  44. */
  45. protected function execute(InputInterface $input, OutputInterface $output)
  46. {
  47. $username = $input->getArgument('username');
  48. $password = $input->getArgument('password');
  49. $manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
  50. $manipulator->changePassword($username, $password);
  51. $output->writeln(sprintf('Changed password for user <comment>%s</comment>', $username));
  52. }
  53. /**
  54. * @see Command
  55. */
  56. protected function interact(InputInterface $input, OutputInterface $output)
  57. {
  58. if (!$input->getArgument('username')) {
  59. $username = $this->getHelper('dialog')->askAndValidate(
  60. $output,
  61. 'Please give the username:',
  62. function($username) {
  63. if (empty($username)) {
  64. throw new \Exception('Username can not be empty');
  65. }
  66. return $username;
  67. }
  68. );
  69. $input->setArgument('username', $username);
  70. }
  71. if (!$input->getArgument('password')) {
  72. $password = $this->getHelper('dialog')->askAndValidate(
  73. $output,
  74. 'Please enter the new password:',
  75. function($password) {
  76. if (empty($password)) {
  77. throw new \Exception('Password can not be empty');
  78. }
  79. return $password;
  80. }
  81. );
  82. $input->setArgument('password', $password);
  83. }
  84. }
  85. }