RoleCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Util\UserManipulator;
  17. /**
  18. * @author Lenar Lõhmus <lenar@city.ee>
  19. */
  20. abstract class RoleCommand extends ContainerAwareCommand
  21. {
  22. /**
  23. * @see Command
  24. */
  25. protected function configure()
  26. {
  27. $this
  28. ->setDefinition(array(
  29. new InputArgument('username', InputArgument::REQUIRED, 'The username'),
  30. new InputArgument('role', InputArgument::OPTIONAL, 'The role'),
  31. new InputOption('super', null, InputOption::VALUE_NONE, 'Instead specifying role, use this to quickly add the super administrator role'),
  32. ));
  33. }
  34. /**
  35. * @see Command
  36. */
  37. protected function execute(InputInterface $input, OutputInterface $output)
  38. {
  39. $username = $input->getArgument('username');
  40. $role = $input->getArgument('role');
  41. $super = (true === $input->getOption('super'));
  42. if (null !== $role && $super) {
  43. throw new \InvalidArgumentException('You can pass either the role or the --super option (but not both simultaneously).');
  44. }
  45. if (null === $role && !$super) {
  46. throw new \RuntimeException('Not enough arguments.');
  47. }
  48. $manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
  49. $this->executeRoleCommand($manipulator, $output, $username, $super, $role);
  50. }
  51. /**
  52. * @see Command
  53. *
  54. * @param UserManipulator $manipulator
  55. * @param OutputInterface $output
  56. * @param string $username
  57. * @param boolean $super
  58. * @param string $role
  59. *
  60. * @return void
  61. */
  62. abstract protected function executeRoleCommand(UserManipulator $manipulator, OutputInterface $output, $username, $super, $role);
  63. /**
  64. * @see Command
  65. */
  66. protected function interact(InputInterface $input, OutputInterface $output)
  67. {
  68. if (!$input->getArgument('username')) {
  69. $username = $this->getHelper('dialog')->askAndValidate(
  70. $output,
  71. 'Please choose a username:',
  72. function($username) {
  73. if (empty($username)) {
  74. throw new \Exception('Username can not be empty');
  75. }
  76. return $username;
  77. }
  78. );
  79. $input->setArgument('username', $username);
  80. }
  81. if ((true !== $input->getOption('super')) && !$input->getArgument('role')) {
  82. $role = $this->getHelper('dialog')->askAndValidate(
  83. $output,
  84. 'Please choose a role:',
  85. function($role) {
  86. if (empty($role)) {
  87. throw new \Exception('Role can not be empty');
  88. }
  89. return $role;
  90. }
  91. );
  92. $input->setArgument('role', $role);
  93. }
  94. }
  95. }