DeactivateUserCommand.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * @author Antoine Hérault <antoine.herault@gmail.com>
  17. */
  18. class DeactivateUserCommand extends ContainerAwareCommand
  19. {
  20. /**
  21. * @see Command
  22. */
  23. protected function configure()
  24. {
  25. $this
  26. ->setName('fos:user:deactivate')
  27. ->setDescription('Deactivate a user')
  28. ->setDefinition(array(
  29. new InputArgument('username', InputArgument::REQUIRED, 'The username'),
  30. ))
  31. ->setHelp(<<<EOT
  32. The <info>fos:user:deactivate</info> command deactivates a user (will not be able to log in)
  33. <info>php app/console fos:user:deactivate matthieu</info>
  34. EOT
  35. );
  36. }
  37. /**
  38. * @see Command
  39. */
  40. protected function execute(InputInterface $input, OutputInterface $output)
  41. {
  42. $username = $input->getArgument('username');
  43. $manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
  44. $manipulator->deactivate($username);
  45. $output->writeln(sprintf('User "%s" has been deactivated.', $username));
  46. }
  47. /**
  48. * @see Command
  49. */
  50. protected function interact(InputInterface $input, OutputInterface $output)
  51. {
  52. if (!$input->getArgument('username')) {
  53. $username = $this->getHelper('dialog')->askAndValidate(
  54. $output,
  55. 'Please choose a username:',
  56. function($username) {
  57. if (empty($username)) {
  58. throw new \Exception('Username can not be empty');
  59. }
  60. return $username;
  61. }
  62. );
  63. $input->setArgument('username', $username);
  64. }
  65. }
  66. }