DeactivateUserCommand.php 2.2KB

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