1
0

TestConfig.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. protected AccessFactory $accessFactory;
  42. protected Helper $helper;
  43. protected ILDAPWrapper $ldap;
  44. public function __construct(
  45. AccessFactory $accessFactory,
  46. Helper $helper,
  47. ILDAPWrapper $ldap
  48. ) {
  49. $this->accessFactory = $accessFactory;
  50. $this->helper = $helper;
  51. $this->ldap = $ldap;
  52. parent::__construct();
  53. }
  54. protected function configure(): void {
  55. $this
  56. ->setName('ldap:test-config')
  57. ->setDescription('tests an LDAP configuration')
  58. ->addArgument(
  59. 'configID',
  60. InputArgument::REQUIRED,
  61. 'the configuration ID'
  62. )
  63. ;
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. $availableConfigs = $this->helper->getServerConfigurationPrefixes();
  67. $configID = $input->getArgument('configID');
  68. if (!in_array($configID, $availableConfigs)) {
  69. $output->writeln('Invalid configID');
  70. return 1;
  71. }
  72. $result = $this->testConfig($configID);
  73. switch ($result) {
  74. case static::ESTABLISHED:
  75. $output->writeln('The configuration is valid and the connection could be established!');
  76. return 0;
  77. case static::CONF_INVALID:
  78. $output->writeln('The configuration is invalid. Please have a look at the logs for further details.');
  79. break;
  80. case static::BINDFAILURE:
  81. $output->writeln('The configuration is valid, but the bind failed. Please check the server settings and credentials.');
  82. break;
  83. case static::SEARCHFAILURE:
  84. $output->writeln('The configuration is valid and the bind passed, but a simple search on the base fails. Please check the server base setting.');
  85. break;
  86. default:
  87. $output->writeln('Your LDAP server was kidnapped by aliens.');
  88. break;
  89. }
  90. return 1;
  91. }
  92. /**
  93. * Tests the specified connection
  94. */
  95. protected function testConfig(string $configID): int {
  96. $connection = new Connection($this->ldap, $configID);
  97. // Ensure validation is run before we attempt the bind
  98. $connection->getConfiguration();
  99. if (!$connection->setConfiguration([
  100. 'ldap_configuration_active' => 1,
  101. ])) {
  102. return static::CONF_INVALID;
  103. }
  104. if (!$connection->bind()) {
  105. return static::BINDFAILURE;
  106. }
  107. $access = $this->accessFactory->get($connection);
  108. $result = $access->countObjects(1);
  109. if (!is_int($result) || ($result <= 0)) {
  110. return static::SEARCHFAILURE;
  111. }
  112. return static::ESTABLISHED;
  113. }
  114. }