Skip to content

Commit

Permalink
Merge pull request #320 from woutervandamme/feature/adminUser
Browse files Browse the repository at this point in the history
[AdminBundle] Show list of available groups when creating a new admin user via the console
  • Loading branch information
krispypen committed Apr 14, 2015
2 parents b37b6b5 + 6e9c1e1 commit 6c7ce27
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/Kunstmaan/AdminBundle/Command/CreateUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

// Attach groups
$groupNames = explode(',', $groupOption);
$groups = $this->getContainer()->get('fos_user.group_manager')->findGroups();

$groupOutput = "";

foreach ($groupNames as $groupName) {
$group = $em->getRepository('KunstmaanAdminBundle:Group')->findOneBy(array('name' => $groupName));
$group = $em->getRepository('KunstmaanAdminBundle:Group')->findOneBy(array('name' => $groups[$groupName]->getName()));
$groupOutput .= $groups[$groupName] . ", ";
if (!empty($group)) {
$user->getGroups()->add($group);
}
Expand All @@ -115,7 +120,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$em->persist($user);
$em->flush();

$output->writeln(sprintf('Added user <comment>%s</comment> to groups <comment>%s</comment>', $input->getArgument('username'), $groupOption));
// Remove trailing comma
$groupOutput = substr($groupOutput, 0, -2);

$output->writeln(sprintf('Added user <comment>%s</comment> to groups <comment>%s</comment>', $input->getArgument('username'), $groupOutput ));
}

/**
Expand Down Expand Up @@ -183,10 +191,20 @@ function($password) {
$input->setArgument('locale', $locale);
}

$groups = $this->getContainer()->get('fos_user.group_manager')->findGroups();

if (!$input->getOption('group')) {

$output->writeln('<info>available groups:</info>');

for($i = 0 ; $i < count($groups); $i++){
$output->writeln('[<info>' . $i . '</info>] ' . $groups[$i]);
}

$group = $this->getHelper('dialog')->askAndValidate(
$output,
'Please enter the group(s) the user should be a member of:',
'Please enter the group(s) the user should be a member of (multiple possible, separated by comma):',

function($group) {
if (empty($group)) {
throw new \InvalidArgumentException('Groups can not be empty');
Expand Down

0 comments on commit 6c7ce27

Please sign in to comment.