vendor/bundles/FOS/UserBundle/Command/CreateUserCommand.php
author cavaliet
Thu, 20 Oct 2011 18:35:34 +0200
changeset 14 fc78844c8a76
parent 3 e54dfe4d0b2b
permissions -rwxr-xr-x
debug addtag for document
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
/*
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 * This file is part of the FOSUserBundle package.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 *
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 *
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * For the full copyright and license information, please view the LICENSE
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * file that was distributed with this source code.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
namespace FOS\UserBundle\Command;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use Symfony\Component\Console\Input\InputArgument;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
use Symfony\Component\Console\Input\InputOption;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
use Symfony\Component\Console\Input\InputInterface;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
use Symfony\Component\Console\Output\OutputInterface;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
use FOS\UserBundle\Model\User;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
/**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 * @author Matthieu Bontemps <matthieu@knplabs.com>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * @author Thibault Duplessis <thibault.duplessis@gmail.com>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 * @author Luis Cordova <cordoval@gmail.com>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
class CreateUserCommand extends ContainerAwareCommand
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
     * @see Command
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    protected function configure()
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        $this
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
            ->setName('fos:user:create')
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
            ->setDescription('Create a user.')
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
            ->setDefinition(array(
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
                new InputArgument('username', InputArgument::REQUIRED, 'The username'),
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
                new InputArgument('email', InputArgument::REQUIRED, 'The email'),
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
                new InputArgument('password', InputArgument::REQUIRED, 'The password'),
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
                new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'),
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
                new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'),
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            ))
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
            ->setHelp(<<<EOT
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
The <info>fos:user:create</info> command creates a user:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
  <info>php app/console fos:user:create matthieu</info>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
This interactive shell will ask you for an email and then a password.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
You can alternatively specify the email and password as the second and third arguments:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
  <info>php app/console fos:user:create matthieu matthieu@example.com mypassword</info>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
You can create a super admin via the super-admin flag:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
  <info>php app/console fos:user:create admin --super-admin</info>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
You can create an inactive user (will not be able to log in):
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
  <info>php app/console fos:user:create thibault --inactive</info>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
EOT
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            );
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
     * @see Command
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    protected function execute(InputInterface $input, OutputInterface $output)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        $username   = $input->getArgument('username');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        $email      = $input->getArgument('email');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        $password   = $input->getArgument('password');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        $inactive   = $input->getOption('inactive');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        $superadmin = $input->getOption('super-admin');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        $manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        $manipulator->create($username, $password, $email, !$inactive, $superadmin);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
        $output->writeln(sprintf('Created user <comment>%s</comment>', $username));
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
     * @see Command
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    protected function interact(InputInterface $input, OutputInterface $output)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        if (!$input->getArgument('username')) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
            $username = $this->getHelper('dialog')->askAndValidate(
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
                $output,
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
                'Please choose a username:',
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
                function($username)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
                {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
                    if (empty($username)) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
                        throw new \Exception('Username can not be empty');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
                    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
                    return $username;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
                }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
            );
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
            $input->setArgument('username', $username);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
        }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        if (!$input->getArgument('email')) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
            $email = $this->getHelper('dialog')->askAndValidate(
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
                $output,
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
                'Please choose an email:',
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
                function($email)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
                {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
                    if (empty($email)) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
                        throw new \Exception('Email can not be empty');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
                    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
                    return $email;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
                }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
            );
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
            $input->setArgument('email', $email);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
        if (!$input->getArgument('password')) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
            $password = $this->getHelper('dialog')->askAndValidate(
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
                $output,
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
                'Please choose a password:',
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
                function($password)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
                {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
                    if (empty($password)) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
                        throw new \Exception('Password can not be empty');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
                    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
                    return $password;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
                }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
            );
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
            $input->setArgument('password', $password);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
}