<?php
namespace Xearts\Bundle\TaobaoDaikoBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
use Xearts\Bundle\TaobaoDaikoBundle\Security\AdminUserManipulator;
/**
* Class ChangeRolesCommand.
*/
class ChangeRolesCommand extends ContainerAwareCommand
{
/**
* @var AdminUserManipulator
*/
private $manipulater;
public function __construct(AdminUserManipulator $manipulater)
{
parent::__construct();
$this->manipulater = $manipulater;
}
/**
* @see Command
*/
protected function configure()
{
$this
->setName('taobao:admin:change-roles')
->setDescription('Change roles of a user')
->setDefinition([
new InputArgument('username', InputArgument::REQUIRED, 'The username'),
new InputArgument('roles', InputArgument::IS_ARRAY, 'The roles'),
])
->setHelp(<<<EOT
The <info>holiday:maintenance:set-roles</info> command change roles of a user:
EOT
);
}
/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$username = $input->getArgument('username');
$roles = $input->getArgument('roles');
$this->manipulater->changeRoles($username, $roles);
$output->writeln(sprintf('Change roles of user <comment>%s</comment>', $username));
}
/**
* @see Command
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('username')) {
$helper = $this->getHelper('question');
$question = new Question('Please choose a username:');
$question->setValidator(
function ($username) {
if (empty($username)) {
throw new \Exception('Username can not be empty');
}
return $username;
}
);
$username = $helper->ask($input, $output, $question);
$input->setArgument('username', $username);
}
if (!$input->getArgument('roles')) {
$helper = $this->getHelper('question');
$choices = [
'ROLE_MEMBER_A',
'ROLE_STAFF',
'ROLE_ADMIN',
'ROLE_SUPER_ADMIN',
];
$question = new ChoiceQuestion(
'Please choose a role:',
$choices
);
$question->setMultiselect(false);
$question->setValidator(
function ($roles) {
if (empty($roles)) {
throw new \Exception('Roles can not be empty');
}
return $roles;
}
);
$roles = $helper->ask($input, $output, $question);
$input->setArgument('roles', preg_split('/[\s,]+/', $roles));
}
}
}