ChangePasswordCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /**
  16. * CreateUserCommand
  17. */
  18. class ChangePasswordCommand extends ContainerAwareCommand
  19. {
  20. /**
  21. * @see Command
  22. */
  23. protected function configure()
  24. {
  25. $this
  26. ->setName('fos:user:change-password')
  27. ->setDescription('Change the password of a user.')
  28. ->setDefinition(array(
  29. new InputArgument('username', InputArgument::REQUIRED, 'The username'),
  30. new InputArgument('password', InputArgument::REQUIRED, 'The password'),
  31. ))
  32. ->setHelp(<<<EOT
  33. The <info>fos:user:change-password</info> command changes the password of a user:
  34. <info>php app/console fos:user:change-password matthieu</info>
  35. This interactive shell will first ask you for a password.
  36. You can alternatively specify the password as a second argument:
  37. <info>php app/console fos:user:change-password matthieu mypassword</info>
  38. EOT
  39. );
  40. }
  41. /**
  42. * @see Command
  43. */
  44. protected function execute(InputInterface $input, OutputInterface $output)
  45. {
  46. $username = $input->getArgument('username');
  47. $password = $input->getArgument('password');
  48. $manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
  49. $manipulator->changePassword($username, $password);
  50. $output->writeln(sprintf('Changed password for user <comment>%s</comment>', $username));
  51. }
  52. /**
  53. * @see Command
  54. */
  55. protected function interact(InputInterface $input, OutputInterface $output)
  56. {
  57. if (!$input->getArgument('username')) {
  58. $username = $this->getHelper('dialog')->askAndValidate(
  59. $output,
  60. 'Please give the username:',
  61. function($username) {
  62. if (empty($username)) {
  63. throw new \Exception('Username can not be empty');
  64. }
  65. return $username;
  66. }
  67. );
  68. $input->setArgument('username', $username);
  69. }
  70. if (!$input->getArgument('password')) {
  71. $password = $this->getHelper('dialog')->askAndValidate(
  72. $output,
  73. 'Please enter the new password:',
  74. function($password) {
  75. if (empty($password)) {
  76. throw new \Exception('Password can not be empty');
  77. }
  78. return $password;
  79. }
  80. );
  81. $input->setArgument('password', $password);
  82. }
  83. }
  84. }