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