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 3.9KB

8 years ago
8 years ago
LDAP User Cleanup: Port from stable7 without further adjustements LDAP User Cleanup background job for user clean up adjust user backend for clean up register background job remove dead code dependency injection make Helper non-static for proper testing check whether it is OK to run clean up job. Do not forget to pass arguments. use correct method to get the config from server methods can be private, proper indirect testing is given no automatic user deletion make limit readable for test purposes make method less complex add first tests let preferences accept limit and offset for getUsersForValue DI via constructor does not work for background jobs after detecting, now we have retrieving deleted users and their details we need this method to be public for now finalize export method, add missing getter clean up namespaces and get rid of unnecessary files helper is not static anymore cleanup according to scrutinizer add cli tool to show deleted users uses are necessary after recent namespace change also remove user from mappings table on deletion add occ command to delete users fix use statement improve output big fixes / improvements PHP doc return true in userExists early for cleaning up deleted users bump version control state and interval with one config.php setting, now ldapUserCleanupInterval. 0 will disable it. enabled by default. improve doc rename cli method to be consistent with others introduce ldapUserCleanupInterval in sample config don't show last login as unix epoche start when no login happend less log output consistent namespace for OfflineUser rename GarbageCollector to DeletedUsersIndex and move it to user subdir fix unit tests add tests for deleteUser more test adjustements Conflicts: apps/user_ldap/ajax/clearMappings.php apps/user_ldap/appinfo/app.php apps/user_ldap/lib/access.php apps/user_ldap/lib/helper.php apps/user_ldap/tests/helper.php core/register_command.php lib/private/preferences.php lib/private/user.php add ldap:check-user to check user existance on the fly Conflicts: apps/user_ldap/lib/helper.php forgotten file PHPdoc fixes, no code change and don't forget to adjust tests
10 years ago
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. }