Skip to content

Commit

Permalink
Merge pull request #39583 from owncloud/user-home-all
Browse files Browse the repository at this point in the history
[10.9.0] occ user:home:list-users --all command
  • Loading branch information
Jan authored Dec 14, 2021
2 parents c688150 + 9cb38c1 commit 1fb8915
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 16 deletions.
2 changes: 2 additions & 0 deletions changelog/10.9.0_2021-12-09/39579
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Added two new users commands:

* `occ user:home:list-dirs` List all homes which are currently used by users
* `occ user:home:list-users <path>` List all users who have their home in a given path
* `occ user:home:list-users --all` List all users for every home path

https://github.com/owncloud/core/pull/39579
https://github.com/owncloud/core/pull/39583
https://github.com/owncloud/core/issues/39502
61 changes: 48 additions & 13 deletions core/Command/User/HomeListUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,29 @@

use OC\Core\Command\Base;
use OCP\IDBConnection;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class HomeListUsers extends Base {
/** @var IDBConnection */
protected $connection;

/** @var \OCP\IUserManager */
protected $userManager;

/**
* @param IDBConnection $connection
*/
public function __construct(IDBConnection $connection) {
public function __construct(
IDBConnection $connection,
IUserManager $userManager
) {
parent::__construct();
$this->connection = $connection;
$this->userManager = $userManager;
}

protected function configure() {
Expand All @@ -47,25 +56,51 @@ protected function configure() {
->setDescription('List all users that have their home in a given path')
->addArgument(
'path',
InputArgument::REQUIRED,
InputArgument::OPTIONAL,
'Path where the user home must be located'
)
->addOption(
'all',
null,
InputOption::VALUE_NONE,
'List all users for every home path.'
);
}

protected function execute(InputInterface $input, OutputInterface $output) {
$path = $input->getArgument('path');
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('accounts')
->where($query->expr()->like('home', $query->createNamedParameter("$path%")));

$result = $query->execute();
$users = [];
while ($row = $result->fetch()) {
$users[] = $row['user_id'];
if ($input->getOption('all')) {
if ($path !== null) {
$output->writeln('<error>--all and path option cannot be given together</error>');
return 1;
}
$users = $this->userManager->search(null, null, null, true);
$outputData = [];
foreach ($users as $user) {
$home = $user->getHome();
// Strip away the UID at the end of the path
$strippedHome = substr($home, 0, strrpos($home, '/'));
$uid = $user->getUID();
$outputData[$strippedHome][] = $uid;
}
} else {
if ($path === null) {
$output->writeln("<error>\n\n Not enough arguments (missing: \"path\").\n</error>\n");
return 1;
}
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('accounts')
->where($query->expr()->like('home', $query->createNamedParameter("$path%")));
$result = $query->execute();
$outputData = [];
while ($row = $result->fetch()) {
$outputData[] = $row['user_id'];
}
$result->closeCursor();
}
$result->closeCursor();
parent::writeArrayInOutputFormat($input, $output, $users, self::DEFAULT_OUTPUT_PREFIX);
parent::writeArrayInOutputFormat($input, $output, $outputData, self::DEFAULT_OUTPUT_PREFIX);

return 0;
}
}
2 changes: 1 addition & 1 deletion core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
$application->add(new OC\Core\Command\User\LastSeen(\OC::$server->getUserManager()));
$application->add(new OC\Core\Command\User\ListUsers(\OC::$server->getUserManager()));
$application->add(new OC\Core\Command\User\HomeListDirs(\OC::$server->getUserManager()));
$application->add(new OC\Core\Command\User\HomeListUsers(\OC::$server->getDatabaseConnection()));
$application->add(new OC\Core\Command\User\HomeListUsers(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()));
$application->add(new OC\Core\Command\User\ListUserGroups(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
$application->add(new OC\Core\Command\User\Report(\OC::$server->getUserManager(), new UserTypeHelper()));
$application->add(new OC\Core\Command\User\ResetPassword(
Expand Down
41 changes: 39 additions & 2 deletions tests/Core/Command/User/HomeListUsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use Doctrine\DBAL\ForwardCompatibility\DriverStatement;
use OC\Core\Command\User\HomeListUsers;
use OC\User\AccountMapper;
use OCP\IDBConnection;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;
Expand All @@ -39,17 +40,26 @@ class HomeListUsersTest extends TestCase {
/** @var IDBConnection | \PHPUnit\Framework\MockObject\MockObject */
private $connection;

/** @var \OCP\IUserManager | \PHPUnit\Framework\MockObject\MockObject */
protected $userManager;

protected function setUp(): void {
parent::setUp();

$this->connection = $this->getMockBuilder('\OC\DB\Connection')
->disableOriginalConstructor()
->getMock();
$command = new HomeListUsers($this->connection);
$this->userManager = $this->getMockBuilder('\OC\User\Manager')
->disableOriginalConstructor()
->getMock();
$command = new HomeListUsers(
$this->connection,
$this->userManager
);
$this->commandTester = new CommandTester($command);
}

public function testCommandInput() {
public function testCommandInputForHomePath() {
$homePath = '/path/to/homes';
$uid = 'user1';

Expand All @@ -66,4 +76,31 @@ public function testCommandInput() {
$output = $this->commandTester->getDisplay();
$this->assertStringContainsString($uid, $output);
}

public function testCommandInputAll() {
$uid = 'testhomeuser';
$path = '/some/path';
$userObject = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()
->getMock();
$userObject->method('getHome')->willReturn($path . '/' . $uid);
$userObject->method('getUID')->willReturn($uid);
$this->userManager->method('search')->willReturn([$uid => $userObject]);

$this->commandTester->execute(['--all' => true]);
$output = $this->commandTester->getDisplay();
$this->assertSame(" - $path:\n - $uid\n", $output);
}

public function testCommandInputBoth() {
$this->commandTester->execute(['--all' => true, 'path' => '/some/path']);
$output = $this->commandTester->getDisplay();
$this->assertStringContainsString('--all and path option cannot be given together', $output);
}

public function testCommandInputNone() {
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
$this->assertStringContainsString('Not enough arguments (missing: "path").', $output);
}
}

0 comments on commit 1fb8915

Please sign in to comment.