Search.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\User_LDAP\Command;
  28. use OCA\User_LDAP\Group_Proxy;
  29. use OCA\User_LDAP\Helper;
  30. use OCA\User_LDAP\LDAP;
  31. use OCA\User_LDAP\User_Proxy;
  32. use OCP\IConfig;
  33. use Symfony\Component\Console\Command\Command;
  34. use Symfony\Component\Console\Input\InputArgument;
  35. use Symfony\Component\Console\Input\InputInterface;
  36. use Symfony\Component\Console\Input\InputOption;
  37. use Symfony\Component\Console\Output\OutputInterface;
  38. class Search extends Command {
  39. public function __construct(
  40. protected IConfig $ocConfig,
  41. private User_Proxy $userProxy,
  42. private Group_Proxy $groupProxy,
  43. ) {
  44. parent::__construct();
  45. }
  46. protected function configure(): void {
  47. $this
  48. ->setName('ldap:search')
  49. ->setDescription('executes a user or group search')
  50. ->addArgument(
  51. 'search',
  52. InputArgument::REQUIRED,
  53. 'the search string (can be empty)'
  54. )
  55. ->addOption(
  56. 'group',
  57. null,
  58. InputOption::VALUE_NONE,
  59. 'searches groups instead of users'
  60. )
  61. ->addOption(
  62. 'offset',
  63. null,
  64. InputOption::VALUE_REQUIRED,
  65. 'The offset of the result set. Needs to be a multiple of limit. defaults to 0.',
  66. '0'
  67. )
  68. ->addOption(
  69. 'limit',
  70. null,
  71. InputOption::VALUE_REQUIRED,
  72. 'limit the results. 0 means no limit, defaults to 15',
  73. '15'
  74. )
  75. ;
  76. }
  77. /**
  78. * Tests whether the offset and limit options are valid
  79. *
  80. * @throws \InvalidArgumentException
  81. */
  82. protected function validateOffsetAndLimit(int $offset, int $limit): void {
  83. if ($limit < 0) {
  84. throw new \InvalidArgumentException('limit must be 0 or greater');
  85. }
  86. if ($offset < 0) {
  87. throw new \InvalidArgumentException('offset must be 0 or greater');
  88. }
  89. if ($limit === 0 && $offset !== 0) {
  90. throw new \InvalidArgumentException('offset must be 0 if limit is also set to 0');
  91. }
  92. if ($offset > 0 && ($offset % $limit !== 0)) {
  93. throw new \InvalidArgumentException('offset must be a multiple of limit');
  94. }
  95. }
  96. protected function execute(InputInterface $input, OutputInterface $output): int {
  97. $helper = new Helper($this->ocConfig, \OC::$server->getDatabaseConnection());
  98. $configPrefixes = $helper->getServerConfigurationPrefixes(true);
  99. $ldapWrapper = new LDAP();
  100. $offset = (int)$input->getOption('offset');
  101. $limit = (int)$input->getOption('limit');
  102. $this->validateOffsetAndLimit($offset, $limit);
  103. if ($input->getOption('group')) {
  104. $proxy = $this->groupProxy;
  105. $getMethod = 'getGroups';
  106. $printID = false;
  107. // convert the limit of groups to null. This will show all the groups available instead of
  108. // nothing, and will match the same behaviour the search for users has.
  109. if ($limit === 0) {
  110. $limit = null;
  111. }
  112. } else {
  113. $proxy = $this->userProxy;
  114. $getMethod = 'getDisplayNames';
  115. $printID = true;
  116. }
  117. $result = $proxy->$getMethod($input->getArgument('search'), $limit, $offset);
  118. foreach ($result as $id => $name) {
  119. $line = $name . ($printID ? ' ('.$id.')' : '');
  120. $output->writeln($line);
  121. }
  122. return self::SUCCESS;
  123. }
  124. }