-
-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7348 from GawainLynch/hotfix/nut-user
Nut command to list all users
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |