1
0

CheckUser.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\User_LDAP\Command;
  25. use Symfony\Component\Console\Command\Command;
  26. use Symfony\Component\Console\Input\InputArgument;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Input\InputOption;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. use OCA\User_LDAP\User\DeletedUsersIndex;
  31. use OCA\User_LDAP\Mapping\UserMapping;
  32. use OCA\User_LDAP\Helper as LDAPHelper;
  33. use OCA\User_LDAP\User_Proxy;
  34. class CheckUser extends Command {
  35. /** @var \OCA\User_LDAP\User_Proxy */
  36. protected $backend;
  37. /** @var \OCA\User_LDAP\Helper */
  38. protected $helper;
  39. /** @var \OCA\User_LDAP\User\DeletedUsersIndex */
  40. protected $dui;
  41. /** @var \OCA\User_LDAP\Mapping\UserMapping */
  42. protected $mapping;
  43. /**
  44. * @param User_Proxy $uBackend
  45. * @param LDAPHelper $helper
  46. * @param DeletedUsersIndex $dui
  47. * @param UserMapping $mapping
  48. */
  49. public function __construct(User_Proxy $uBackend, LDAPHelper $helper, DeletedUsersIndex $dui, UserMapping $mapping) {
  50. $this->backend = $uBackend;
  51. $this->helper = $helper;
  52. $this->dui = $dui;
  53. $this->mapping = $mapping;
  54. parent::__construct();
  55. }
  56. protected function configure() {
  57. $this
  58. ->setName('ldap:check-user')
  59. ->setDescription('checks whether a user exists on LDAP.')
  60. ->addArgument(
  61. 'ocName',
  62. InputArgument::REQUIRED,
  63. 'the user name as used in Nextcloud'
  64. )
  65. ->addOption(
  66. 'force',
  67. null,
  68. InputOption::VALUE_NONE,
  69. 'ignores disabled LDAP configuration'
  70. )
  71. ;
  72. }
  73. protected function execute(InputInterface $input, OutputInterface $output) {
  74. try {
  75. $uid = $input->getArgument('ocName');
  76. $this->isAllowed($input->getOption('force'));
  77. $this->confirmUserIsMapped($uid);
  78. $exists = $this->backend->userExistsOnLDAP($uid);
  79. if($exists === true) {
  80. $output->writeln('The user is still available on LDAP.');
  81. return;
  82. }
  83. $this->dui->markUser($uid);
  84. $output->writeln('The user does not exists on LDAP anymore.');
  85. $output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
  86. . $uid . '"');
  87. } catch (\Exception $e) {
  88. $output->writeln('<error>' . $e->getMessage(). '</error>');
  89. }
  90. }
  91. /**
  92. * checks whether a user is actually mapped
  93. * @param string $ocName the username as used in Nextcloud
  94. * @throws \Exception
  95. * @return true
  96. */
  97. protected function confirmUserIsMapped($ocName) {
  98. $dn = $this->mapping->getDNByName($ocName);
  99. if ($dn === false) {
  100. throw new \Exception('The given user is not a recognized LDAP user.');
  101. }
  102. return true;
  103. }
  104. /**
  105. * checks whether the setup allows reliable checking of LDAP user existence
  106. * @throws \Exception
  107. * @return true
  108. */
  109. protected function isAllowed($force) {
  110. if($this->helper->haveDisabledConfigurations() && !$force) {
  111. throw new \Exception('Cannot check user existence, because '
  112. . 'disabled LDAP configurations are present.');
  113. }
  114. // we don't check ldapUserCleanupInterval from config.php because this
  115. // action is triggered manually, while the setting only controls the
  116. // background job.
  117. return true;
  118. }
  119. }