Search.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\User_LDAP\Command;
  8. use OCA\User_LDAP\Group_Proxy;
  9. use OCA\User_LDAP\Helper;
  10. use OCA\User_LDAP\LDAP;
  11. use OCA\User_LDAP\User_Proxy;
  12. use OCP\IConfig;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. class Search extends Command {
  19. public function __construct(
  20. protected IConfig $ocConfig,
  21. private User_Proxy $userProxy,
  22. private Group_Proxy $groupProxy,
  23. ) {
  24. parent::__construct();
  25. }
  26. protected function configure(): void {
  27. $this
  28. ->setName('ldap:search')
  29. ->setDescription('executes a user or group search')
  30. ->addArgument(
  31. 'search',
  32. InputArgument::REQUIRED,
  33. 'the search string (can be empty)'
  34. )
  35. ->addOption(
  36. 'group',
  37. null,
  38. InputOption::VALUE_NONE,
  39. 'searches groups instead of users'
  40. )
  41. ->addOption(
  42. 'offset',
  43. null,
  44. InputOption::VALUE_REQUIRED,
  45. 'The offset of the result set. Needs to be a multiple of limit. defaults to 0.',
  46. '0'
  47. )
  48. ->addOption(
  49. 'limit',
  50. null,
  51. InputOption::VALUE_REQUIRED,
  52. 'limit the results. 0 means no limit, defaults to 15',
  53. '15'
  54. )
  55. ;
  56. }
  57. /**
  58. * Tests whether the offset and limit options are valid
  59. *
  60. * @throws \InvalidArgumentException
  61. */
  62. protected function validateOffsetAndLimit(int $offset, int $limit): void {
  63. if ($limit < 0) {
  64. throw new \InvalidArgumentException('limit must be 0 or greater');
  65. }
  66. if ($offset < 0) {
  67. throw new \InvalidArgumentException('offset must be 0 or greater');
  68. }
  69. if ($limit === 0 && $offset !== 0) {
  70. throw new \InvalidArgumentException('offset must be 0 if limit is also set to 0');
  71. }
  72. if ($offset > 0 && ($offset % $limit !== 0)) {
  73. throw new \InvalidArgumentException('offset must be a multiple of limit');
  74. }
  75. }
  76. protected function execute(InputInterface $input, OutputInterface $output): int {
  77. $helper = new Helper($this->ocConfig, \OC::$server->getDatabaseConnection());
  78. $configPrefixes = $helper->getServerConfigurationPrefixes(true);
  79. $ldapWrapper = new LDAP();
  80. $offset = (int)$input->getOption('offset');
  81. $limit = (int)$input->getOption('limit');
  82. $this->validateOffsetAndLimit($offset, $limit);
  83. if ($input->getOption('group')) {
  84. $proxy = $this->groupProxy;
  85. $getMethod = 'getGroups';
  86. $printID = false;
  87. // convert the limit of groups to null. This will show all the groups available instead of
  88. // nothing, and will match the same behaviour the search for users has.
  89. if ($limit === 0) {
  90. $limit = null;
  91. }
  92. } else {
  93. $proxy = $this->userProxy;
  94. $getMethod = 'getDisplayNames';
  95. $printID = true;
  96. }
  97. $result = $proxy->$getMethod($input->getArgument('search'), $limit, $offset);
  98. foreach ($result as $id => $name) {
  99. $line = $name . ($printID ? ' (' . $id . ')' : '');
  100. $output->writeln($line);
  101. }
  102. return self::SUCCESS;
  103. }
  104. }