2014-11-28 12:08:33 +01:00
|
|
|
<?php
|
2024-05-29 11:32:54 +02:00
|
|
|
|
2014-11-28 12:08:33 +01:00
|
|
|
/**
|
2024-05-29 11:32:54 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-11-28 12:08:33 +01:00
|
|
|
*/
|
2016-05-12 09:59:29 +02:00
|
|
|
namespace OCA\User_LDAP\Command;
|
2014-11-28 12:08:33 +01:00
|
|
|
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCA\User_LDAP\Group_Proxy;
|
|
|
|
use OCA\User_LDAP\Helper;
|
|
|
|
use OCA\User_LDAP\LDAP;
|
|
|
|
use OCA\User_LDAP\User_Proxy;
|
|
|
|
use OCP\IConfig;
|
2025-02-03 15:34:01 +01:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\Server;
|
2019-11-22 20:52:10 +01:00
|
|
|
|
2014-11-28 12:08:33 +01:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
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 Search extends Command {
|
2024-01-24 11:08:56 +03:30
|
|
|
public function __construct(
|
|
|
|
protected IConfig $ocConfig,
|
|
|
|
private User_Proxy $userProxy,
|
|
|
|
private Group_Proxy $groupProxy,
|
|
|
|
) {
|
2015-01-06 23:28:49 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2024-01-24 11:08:56 +03:30
|
|
|
protected function configure(): void {
|
2014-11-28 12:08:33 +01:00
|
|
|
$this
|
|
|
|
->setName('ldap:search')
|
|
|
|
->setDescription('executes a user or group search')
|
|
|
|
->addArgument(
|
|
|
|
'search',
|
|
|
|
InputArgument::REQUIRED,
|
|
|
|
'the search string (can be empty)'
|
2020-04-09 09:22:29 +02:00
|
|
|
)
|
2014-11-28 12:08:33 +01:00
|
|
|
->addOption(
|
|
|
|
'group',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'searches groups instead of users'
|
2020-04-09 09:22:29 +02:00
|
|
|
)
|
2014-11-28 12:08:33 +01:00
|
|
|
->addOption(
|
|
|
|
'offset',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_REQUIRED,
|
|
|
|
'The offset of the result set. Needs to be a multiple of limit. defaults to 0.',
|
2021-06-09 13:25:31 +02:00
|
|
|
'0'
|
2020-04-09 09:22:29 +02:00
|
|
|
)
|
2014-11-28 12:08:33 +01:00
|
|
|
->addOption(
|
|
|
|
'limit',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_REQUIRED,
|
|
|
|
'limit the results. 0 means no limit, defaults to 15',
|
2021-06-09 13:25:31 +02:00
|
|
|
'15'
|
2020-04-09 09:22:29 +02:00
|
|
|
)
|
2014-11-28 12:08:33 +01:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests whether the offset and limit options are valid
|
2024-01-24 11:08:56 +03:30
|
|
|
*
|
2014-11-28 12:08:33 +01:00
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
2024-01-30 12:57:26 +03:30
|
|
|
protected function validateOffsetAndLimit(int $offset, int $limit): void {
|
2014-11-28 12:08:33 +01:00
|
|
|
if ($limit < 0) {
|
|
|
|
throw new \InvalidArgumentException('limit must be 0 or greater');
|
|
|
|
}
|
2020-10-05 15:12:57 +02:00
|
|
|
if ($offset < 0) {
|
2014-11-28 12:08:33 +01:00
|
|
|
throw new \InvalidArgumentException('offset must be 0 or greater');
|
|
|
|
}
|
|
|
|
if ($limit === 0 && $offset !== 0) {
|
|
|
|
throw new \InvalidArgumentException('offset must be 0 if limit is also set to 0');
|
|
|
|
}
|
|
|
|
if ($offset > 0 && ($offset % $limit !== 0)) {
|
|
|
|
throw new \InvalidArgumentException('offset must be a multiple of limit');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 15:12:11 +02:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
2025-02-03 15:34:01 +01:00
|
|
|
$helper = new Helper($this->ocConfig, Server::get(IDBConnection::class));
|
2014-08-21 17:59:13 +02:00
|
|
|
$configPrefixes = $helper->getServerConfigurationPrefixes(true);
|
2014-11-28 12:08:33 +01:00
|
|
|
$ldapWrapper = new LDAP();
|
|
|
|
|
2018-01-25 23:06:53 +01:00
|
|
|
$offset = (int)$input->getOption('offset');
|
|
|
|
$limit = (int)$input->getOption('limit');
|
2014-11-28 12:08:33 +01:00
|
|
|
$this->validateOffsetAndLimit($offset, $limit);
|
|
|
|
|
|
|
|
if ($input->getOption('group')) {
|
2020-10-22 11:25:33 +02:00
|
|
|
$proxy = $this->groupProxy;
|
2014-11-28 12:08:33 +01:00
|
|
|
$getMethod = 'getGroups';
|
|
|
|
$printID = false;
|
2016-07-12 09:04:48 +02:00
|
|
|
// convert the limit of groups to null. This will show all the groups available instead of
|
|
|
|
// nothing, and will match the same behaviour the search for users has.
|
|
|
|
if ($limit === 0) {
|
|
|
|
$limit = null;
|
|
|
|
}
|
2014-11-28 12:08:33 +01:00
|
|
|
} else {
|
2020-10-22 11:25:33 +02:00
|
|
|
$proxy = $this->userProxy;
|
2014-11-28 12:08:33 +01:00
|
|
|
$getMethod = 'getDisplayNames';
|
|
|
|
$printID = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $proxy->$getMethod($input->getArgument('search'), $limit, $offset);
|
|
|
|
foreach ($result as $id => $name) {
|
|
|
|
$line = $name . ($printID ? ' (' . $id . ')' : '');
|
|
|
|
$output->writeln($line);
|
|
|
|
}
|
2024-01-24 11:08:56 +03:30
|
|
|
return self::SUCCESS;
|
2014-11-28 12:08:33 +01:00
|
|
|
}
|
|
|
|
}
|