search.php 3.6 KB

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