You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Search.php 4.2KB

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