1
0

TestConfig.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 OCA\User_LDAP\ILDAPWrapper;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Input\InputArgument;
  34. use Symfony\Component\Console\Input\InputInterface;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. class TestConfig extends Command {
  37. protected const ESTABLISHED = 0;
  38. protected const CONF_INVALID = 1;
  39. protected const BINDFAILURE = 2;
  40. protected const SEARCHFAILURE = 3;
  41. public function __construct(
  42. protected AccessFactory $accessFactory,
  43. protected Helper $helper,
  44. protected ILDAPWrapper $ldap,
  45. ) {
  46. parent::__construct();
  47. }
  48. protected function configure(): void {
  49. $this
  50. ->setName('ldap:test-config')
  51. ->setDescription('tests an LDAP configuration')
  52. ->addArgument(
  53. 'configID',
  54. InputArgument::REQUIRED,
  55. 'the configuration ID'
  56. )
  57. ;
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. $availableConfigs = $this->helper->getServerConfigurationPrefixes();
  61. $configID = $input->getArgument('configID');
  62. if (!in_array($configID, $availableConfigs)) {
  63. $output->writeln('Invalid configID');
  64. return self::FAILURE;
  65. }
  66. $result = $this->testConfig($configID);
  67. $message = match ($result) {
  68. static::ESTABLISHED => 'The configuration is valid and the connection could be established!',
  69. static::CONF_INVALID => 'The configuration is invalid. Please have a look at the logs for further details.',
  70. static::BINDFAILURE => 'The configuration is valid, but the bind failed. Please check the server settings and credentials.',
  71. static::SEARCHFAILURE => 'The configuration is valid and the bind passed, but a simple search on the base fails. Please check the server base setting.',
  72. default => 'Your LDAP server was kidnapped by aliens.',
  73. };
  74. $output->writeln($message);
  75. return $result === static::ESTABLISHED
  76. ? self::SUCCESS
  77. : self::FAILURE;
  78. }
  79. /**
  80. * Tests the specified connection
  81. */
  82. protected function testConfig(string $configID): int {
  83. $connection = new Connection($this->ldap, $configID);
  84. // Ensure validation is run before we attempt the bind
  85. $connection->getConfiguration();
  86. if (!$connection->setConfiguration([
  87. 'ldap_configuration_active' => 1,
  88. ])) {
  89. return static::CONF_INVALID;
  90. }
  91. if (!$connection->bind()) {
  92. return static::BINDFAILURE;
  93. }
  94. $access = $this->accessFactory->get($connection);
  95. $result = $access->countObjects(1);
  96. if (!is_int($result) || ($result <= 0)) {
  97. return static::SEARCHFAILURE;
  98. }
  99. return static::ESTABLISHED;
  100. }
  101. }