Search.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /** @var \OCP\IConfig */
  40. protected $ocConfig;
  41. /** @var User_Proxy */
  42. private $userProxy;
  43. /** @var Group_Proxy */
  44. private $groupProxy;
  45. public function __construct(IConfig $ocConfig, User_Proxy $userProxy, Group_Proxy $groupProxy) {
  46. parent::__construct();
  47. $this->ocConfig = $ocConfig;
  48. $this->userProxy = $userProxy;
  49. $this->groupProxy = $groupProxy;
  50. }
  51. protected function configure() {
  52. $this
  53. ->setName('ldap:search')
  54. ->setDescription('executes a user or group search')
  55. ->addArgument(
  56. 'search',
  57. InputArgument::REQUIRED,
  58. 'the search string (can be empty)'
  59. )
  60. ->addOption(
  61. 'group',
  62. null,
  63. InputOption::VALUE_NONE,
  64. 'searches groups instead of users'
  65. )
  66. ->addOption(
  67. 'offset',
  68. null,
  69. InputOption::VALUE_REQUIRED,
  70. 'The offset of the result set. Needs to be a multiple of limit. defaults to 0.',
  71. '0'
  72. )
  73. ->addOption(
  74. 'limit',
  75. null,
  76. InputOption::VALUE_REQUIRED,
  77. 'limit the results. 0 means no limit, defaults to 15',
  78. '15'
  79. )
  80. ;
  81. }
  82. /**
  83. * Tests whether the offset and limit options are valid
  84. * @param int $offset
  85. * @param int $limit
  86. * @throws \InvalidArgumentException
  87. */
  88. protected function validateOffsetAndLimit($offset, $limit) {
  89. if ($limit < 0) {
  90. throw new \InvalidArgumentException('limit must be 0 or greater');
  91. }
  92. if ($offset < 0) {
  93. throw new \InvalidArgumentException('offset must be 0 or greater');
  94. }
  95. if ($limit === 0 && $offset !== 0) {
  96. throw new \InvalidArgumentException('offset must be 0 if limit is also set to 0');
  97. }
  98. if ($offset > 0 && ($offset % $limit !== 0)) {
  99. throw new \InvalidArgumentException('offset must be a multiple of limit');
  100. }
  101. }
  102. protected function execute(InputInterface $input, OutputInterface $output): int {
  103. $helper = new Helper($this->ocConfig, \OC::$server->getDatabaseConnection());
  104. $configPrefixes = $helper->getServerConfigurationPrefixes(true);
  105. $ldapWrapper = new LDAP();
  106. $offset = (int)$input->getOption('offset');
  107. $limit = (int)$input->getOption('limit');
  108. $this->validateOffsetAndLimit($offset, $limit);
  109. if ($input->getOption('group')) {
  110. $proxy = $this->groupProxy;
  111. $getMethod = 'getGroups';
  112. $printID = false;
  113. // convert the limit of groups to null. This will show all the groups available instead of
  114. // nothing, and will match the same behaviour the search for users has.
  115. if ($limit === 0) {
  116. $limit = null;
  117. }
  118. } else {
  119. $proxy = $this->userProxy;
  120. $getMethod = 'getDisplayNames';
  121. $printID = true;
  122. }
  123. $result = $proxy->$getMethod($input->getArgument('search'), $limit, $offset);
  124. foreach ($result as $id => $name) {
  125. $line = $name . ($printID ? ' ('.$id.')' : '');
  126. $output->writeln($line);
  127. }
  128. return 0;
  129. }
  130. }