CreateUserCommand.php 4.3 KB

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