CreateUserCommand.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\InputOption;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use FOS\UserBundle\Model\User;
  17. /**
  18. * @author Matthieu Bontemps <matthieu@knplabs.com>
  19. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  20. * @author Luis Cordova <cordoval@gmail.com>
  21. */
  22. class CreateUserCommand extends ContainerAwareCommand
  23. {
  24. /**
  25. * @see Command
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setName('fos:user:create')
  31. ->setDescription('Create a user.')
  32. ->setDefinition(array(
  33. new InputArgument('username', InputArgument::REQUIRED, 'The username'),
  34. new InputArgument('email', InputArgument::REQUIRED, 'The email'),
  35. new InputArgument('password', InputArgument::REQUIRED, 'The password'),
  36. new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'),
  37. new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'),
  38. ))
  39. ->setHelp(<<<EOT
  40. The <info>fos:user:create</info> command creates a user:
  41. <info>php app/console fos:user:create matthieu</info>
  42. This interactive shell will ask you for an email and then a password.
  43. You can alternatively specify the email and password as the second and third arguments:
  44. <info>php app/console fos:user:create matthieu matthieu@example.com mypassword</info>
  45. You can create a super admin via the super-admin flag:
  46. <info>php app/console fos:user:create admin --super-admin</info>
  47. You can create an inactive user (will not be able to log in):
  48. <info>php app/console fos:user:create thibault --inactive</info>
  49. EOT
  50. );
  51. }
  52. /**
  53. * @see Command
  54. */
  55. protected function execute(InputInterface $input, OutputInterface $output)
  56. {
  57. $username = $input->getArgument('username');
  58. $email = $input->getArgument('email');
  59. $password = $input->getArgument('password');
  60. $inactive = $input->getOption('inactive');
  61. $superadmin = $input->getOption('super-admin');
  62. $manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
  63. $manipulator->create($username, $password, $email, !$inactive, $superadmin);
  64. $output->writeln(sprintf('Created user <comment>%s</comment>', $username));
  65. }
  66. /**
  67. * @see Command
  68. */
  69. protected function interact(InputInterface $input, OutputInterface $output)
  70. {
  71. if (!$input->getArgument('username')) {
  72. $username = $this->getHelper('dialog')->askAndValidate(
  73. $output,
  74. 'Please choose a username:',
  75. function($username) {
  76. if (empty($username)) {
  77. throw new \Exception('Username can not be empty');
  78. }
  79. return $username;
  80. }
  81. );
  82. $input->setArgument('username', $username);
  83. }
  84. if (!$input->getArgument('email')) {
  85. $email = $this->getHelper('dialog')->askAndValidate(
  86. $output,
  87. 'Please choose an email:',
  88. function($email) {
  89. if (empty($email)) {
  90. throw new \Exception('Email can not be empty');
  91. }
  92. return $email;
  93. }
  94. );
  95. $input->setArgument('email', $email);
  96. }
  97. if (!$input->getArgument('password')) {
  98. $password = $this->getHelper('dialog')->askAndValidate(
  99. $output,
  100. 'Please choose a password:',
  101. function($password) {
  102. if (empty($password)) {
  103. throw new \Exception('Password can not be empty');
  104. }
  105. return $password;
  106. }
  107. );
  108. $input->setArgument('password', $password);
  109. }
  110. }
  111. }