vendor/xearts/taobao-daiko-bundle/src/Command/ChangeRolesCommand.php line 16

Open in your IDE?
  1. <?php
  2. namespace Xearts\Bundle\TaobaoDaikoBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Console\Question\ChoiceQuestion;
  8. use Symfony\Component\Console\Question\Question;
  9. use Xearts\Bundle\TaobaoDaikoBundle\Security\AdminUserManipulator;
  10. /**
  11.  * Class ChangeRolesCommand.
  12.  */
  13. class ChangeRolesCommand extends ContainerAwareCommand
  14. {
  15.     /**
  16.      * @var AdminUserManipulator
  17.      */
  18.     private $manipulater;
  19.     public function __construct(AdminUserManipulator $manipulater)
  20.     {
  21.         parent::__construct();
  22.         $this->manipulater $manipulater;
  23.     }
  24.     /**
  25.      * @see Command
  26.      */
  27.     protected function configure()
  28.     {
  29.         $this
  30.             ->setName('taobao:admin:change-roles')
  31.             ->setDescription('Change roles of a user')
  32.             ->setDefinition([
  33.                 new InputArgument('username'InputArgument::REQUIRED'The username'),
  34.                 new InputArgument('roles'InputArgument::IS_ARRAY'The roles'),
  35.             ])
  36.             ->setHelp(<<<EOT
  37. The <info>holiday:maintenance:set-roles</info> command change roles of a user:
  38. EOT
  39.             );
  40.     }
  41.     /**
  42.      * @see Command
  43.      */
  44.     protected function execute(InputInterface $inputOutputInterface $output)
  45.     {
  46.         $username $input->getArgument('username');
  47.         $roles $input->getArgument('roles');
  48.         $this->manipulater->changeRoles($username$roles);
  49.         $output->writeln(sprintf('Change roles of user <comment>%s</comment>'$username));
  50.     }
  51.     /**
  52.      * @see Command
  53.      */
  54.     protected function interact(InputInterface $inputOutputInterface $output)
  55.     {
  56.         if (!$input->getArgument('username')) {
  57.             $helper $this->getHelper('question');
  58.             $question = new Question('Please choose a username:');
  59.             $question->setValidator(
  60.                 function ($username) {
  61.                     if (empty($username)) {
  62.                         throw new \Exception('Username can not be empty');
  63.                     }
  64.                     return $username;
  65.                 }
  66.             );
  67.             $username $helper->ask($input$output$question);
  68.             $input->setArgument('username'$username);
  69.         }
  70.         if (!$input->getArgument('roles')) {
  71.             $helper $this->getHelper('question');
  72.             $choices = [
  73.                 'ROLE_MEMBER_A',
  74.                 'ROLE_STAFF',
  75.                 'ROLE_ADMIN',
  76.                 'ROLE_SUPER_ADMIN',
  77.             ];
  78.             $question = new ChoiceQuestion(
  79.                 'Please choose a role:',
  80.                 $choices
  81.             );
  82.             $question->setMultiselect(false);
  83.             $question->setValidator(
  84.                 function ($roles) {
  85.                     if (empty($roles)) {
  86.                         throw new \Exception('Roles can not be empty');
  87.                     }
  88.                     return $roles;
  89.                 }
  90.             );
  91.             $roles $helper->ask($input$output$question);
  92.             $input->setArgument('roles'preg_split('/[\s,]+/'$roles));
  93.         }
  94.     }
  95. }