Skip to content

Commit

Permalink
Merge pull request #7348 from GawainLynch/hotfix/nut-user
Browse files Browse the repository at this point in the history
Nut command to list all users
  • Loading branch information
bobdenotter committed Feb 18, 2018
2 parents 99acf4c + 2f08a8d commit d98da95
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/Nut/UsersList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Bolt\Nut;

use Bolt\Common\Json;
use Bolt\Storage\Entity;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Nut command to list all users.
*/
class UsersList extends BaseCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('users:list')
->setDescription('List all Bolt users')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var \Bolt\Storage\Repository\UsersRepository $repo */
$repo = $this->app['storage']->getRepository(Entity\Users::class);

$userEntities = $repo->findAll();
if ($userEntities === false) {
$this->io->error('No user accounts found.');

return 1;
}

$this->io->title("Account details for all users");
$headers = ['User name', 'Email', 'Display Name', 'Roles', 'Enabled'];

/** @var Entity\Users $userEntity */
foreach ($userEntities as $userEntity) {
$roles = array_filter($userEntity->getRoles(), function ($var) {
return $var !== 'everyone';
});
$rows[] = [
$userEntity->getUsername(),
$userEntity->getEmail(),
$userEntity->getDisplayname(),
implode(', ', $roles),
Json::dump($userEntity->getEnabled()),
];
}

$this->io->table($headers, $rows);

return 0;
}
}
1 change: 1 addition & 0 deletions src/Provider/NutServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function ($app) {
new Nut\UserResetPassword(),
new Nut\UserRoleAdd(),
new Nut\UserRoleRemove(),
new Nut\UsersList(),
new Nut\DebugEvents(),
new Nut\DebugServiceProviders(),
new Nut\DebugRouter(),
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/unit/Nut/UsersListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bolt\Tests\Nut;

use Bolt\Nut\UsersList;
use Bolt\Tests\BoltUnitTest;
use Symfony\Component\Console\Tester\CommandTester;

/**
* @covers \Bolt\Nut\UsersList
*
* @author Gawain Lynch <gawain.lynch@gmail.com>
*/
class UsersListTest extends BoltUnitTest
{
public function testListing()
{
$app = $this->getApp();
$this->resetDb();
$this->addNewUser($app, 'koala', 'Kenny Koala', 'editor', true);
$this->addNewUser($app, 'bruce', 'Bruce D. Dropbear', 'admin', true);

$command = new UsersList($app);
$tester = new CommandTester($command);

$tester->execute([]);
$result = $tester->getDisplay();
$this->assertRegExp('#koala(\s*).*(\s*)koala@example\.com(\s*).*(\s*)Kenny Koala(\s*)editor(\s*)\d*(\s*).*(\s*)true#', $result);
$this->assertRegExp('#bruce(\s*).*(\s*)bruce@example\.com(\s*).*(\s*)Bruce D. Dropbear(\s*)admin(\s*)\d*(\s*).*(\s*)true#', $result);
}
}

0 comments on commit d98da95

Please sign in to comment.