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.

TestConfig.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Côme Chilliet <come.chilliet@nextcloud.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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\AccessFactory;
  29. use OCA\User_LDAP\Connection;
  30. use OCA\User_LDAP\Helper;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class TestConfig extends Command {
  36. protected const SUCCESS = 0;
  37. protected const INVALID = 1;
  38. protected const BINDFAILURE = 2;
  39. protected const SEARCHFAILURE = 3;
  40. /** @var AccessFactory */
  41. protected $accessFactory;
  42. public function __construct(AccessFactory $accessFactory) {
  43. $this->accessFactory = $accessFactory;
  44. parent::__construct();
  45. }
  46. protected function configure() {
  47. $this
  48. ->setName('ldap:test-config')
  49. ->setDescription('tests an LDAP configuration')
  50. ->addArgument(
  51. 'configID',
  52. InputArgument::REQUIRED,
  53. 'the configuration ID'
  54. )
  55. ;
  56. }
  57. protected function execute(InputInterface $input, OutputInterface $output): int {
  58. $helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
  59. $availableConfigs = $helper->getServerConfigurationPrefixes();
  60. $configID = $input->getArgument('configID');
  61. if (!in_array($configID, $availableConfigs)) {
  62. $output->writeln('Invalid configID');
  63. return 1;
  64. }
  65. $result = $this->testConfig($configID);
  66. switch ($result) {
  67. case static::SUCCESS:
  68. $output->writeln('The configuration is valid and the connection could be established!');
  69. return 0;
  70. case static::INVALID:
  71. $output->writeln('The configuration is invalid. Please have a look at the logs for further details.');
  72. break;
  73. case static::BINDFAILURE:
  74. $output->writeln('The configuration is valid, but the bind failed. Please check the server settings and credentials.');
  75. break;
  76. case static::SEARCHFAILURE:
  77. $output->writeln('The configuration is valid and the bind passed, but a simple search on the base fails. Please check the server base setting.');
  78. break;
  79. default:
  80. $output->writeln('Your LDAP server was kidnapped by aliens.');
  81. break;
  82. }
  83. return 1;
  84. }
  85. /**
  86. * Tests the specified connection
  87. */
  88. protected function testConfig(string $configID): int {
  89. $lw = new \OCA\User_LDAP\LDAP();
  90. $connection = new Connection($lw, $configID);
  91. // Ensure validation is run before we attempt the bind
  92. $connection->getConfiguration();
  93. if (!$connection->setConfiguration([
  94. 'ldap_configuration_active' => 1,
  95. ])) {
  96. return static::INVALID;
  97. }
  98. if (!$connection->bind()) {
  99. return static::BINDFAILURE;
  100. }
  101. $access = $this->accessFactory->get($connection);
  102. $result = $access->countObjects(1);
  103. if (!is_int($result) || ($result <= 0)) {
  104. return static::SEARCHFAILURE;
  105. }
  106. return static::SUCCESS;
  107. }
  108. }