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.

checkuser.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\lib\user\User;
  29. use OCA\User_LDAP\lib\User\DeletedUsersIndex;
  30. use OCA\User_LDAP\Mapping\UserMapping;
  31. use OCA\user_ldap\lib\Helper as LDAPHelper;
  32. use OCA\user_ldap\User_Proxy;
  33. class CheckUser extends Command {
  34. /** @var \OCA\user_ldap\User_Proxy */
  35. protected $backend;
  36. /** @var \OCA\User_LDAP\lib\Helper */
  37. protected $helper;
  38. /** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */
  39. protected $dui;
  40. /** @var \OCA\User_LDAP\Mapping\UserMapping */
  41. protected $mapping;
  42. /**
  43. * @param OCA\user_ldap\User_Proxy $uBackend
  44. * @param OCA\user_ldap\lib\Helper $helper
  45. * @param OCA\User_LDAP\lib\User\DeletedUsersIndex $dui
  46. * @param OCA\User_LDAP\Mapping\UserMapping $mapping
  47. */
  48. public function __construct(User_Proxy $uBackend, LDAPHelper $helper, DeletedUsersIndex $dui, UserMapping $mapping) {
  49. $this->backend = $uBackend;
  50. $this->helper = $helper;
  51. $this->dui = $dui;
  52. $this->mapping = $mapping;
  53. parent::__construct();
  54. }
  55. protected function configure() {
  56. $this
  57. ->setName('ldap:check-user')
  58. ->setDescription('checks whether a user exists on LDAP.')
  59. ->addArgument(
  60. 'ocName',
  61. InputArgument::REQUIRED,
  62. 'the user name as used in ownCloud'
  63. )
  64. ->addOption(
  65. 'force',
  66. null,
  67. InputOption::VALUE_NONE,
  68. 'ignores disabled LDAP configuration'
  69. )
  70. ;
  71. }
  72. protected function execute(InputInterface $input, OutputInterface $output) {
  73. try {
  74. $uid = $input->getArgument('ocName');
  75. $this->isAllowed($input->getOption('force'));
  76. $this->confirmUserIsMapped($uid);
  77. $exists = $this->backend->userExistsOnLDAP($uid);
  78. if($exists === true) {
  79. $output->writeln('The user is still available on LDAP.');
  80. return;
  81. }
  82. $this->dui->markUser($uid);
  83. $output->writeln('The user does not exists on LDAP anymore.');
  84. $output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
  85. . $uid . '"');
  86. } catch (\Exception $e) {
  87. $output->writeln('<error>' . $e->getMessage(). '</error>');
  88. }
  89. }
  90. /**
  91. * checks whether a user is actually mapped
  92. * @param string $ocName the username as used in ownCloud
  93. * @throws \Exception
  94. * @return true
  95. */
  96. protected function confirmUserIsMapped($ocName) {
  97. $dn = $this->mapping->getDNByName($ocName);
  98. if ($dn === false) {
  99. throw new \Exception('The given user is not a recognized LDAP user.');
  100. }
  101. return true;
  102. }
  103. /**
  104. * checks whether the setup allows reliable checking of LDAP user existence
  105. * @throws \Exception
  106. * @return true
  107. */
  108. protected function isAllowed($force) {
  109. if($this->helper->haveDisabledConfigurations() && !$force) {
  110. throw new \Exception('Cannot check user existence, because '
  111. . 'disabled LDAP configurations are present.');
  112. }
  113. // we don't check ldapUserCleanupInterval from config.php because this
  114. // action is triggered manually, while the setting only controls the
  115. // background job.
  116. return true;
  117. }
  118. }