TestConfig.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\User_LDAP\Command;
  8. use OCA\User_LDAP\AccessFactory;
  9. use OCA\User_LDAP\Connection;
  10. use OCA\User_LDAP\Helper;
  11. use OCA\User_LDAP\ILDAPWrapper;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class TestConfig extends Command {
  17. protected const ESTABLISHED = 0;
  18. protected const CONF_INVALID = 1;
  19. protected const BINDFAILURE = 2;
  20. protected const SEARCHFAILURE = 3;
  21. public function __construct(
  22. protected AccessFactory $accessFactory,
  23. protected Helper $helper,
  24. protected ILDAPWrapper $ldap,
  25. ) {
  26. parent::__construct();
  27. }
  28. protected function configure(): void {
  29. $this
  30. ->setName('ldap:test-config')
  31. ->setDescription('tests an LDAP configuration')
  32. ->addArgument(
  33. 'configID',
  34. InputArgument::REQUIRED,
  35. 'the configuration ID'
  36. )
  37. ;
  38. }
  39. protected function execute(InputInterface $input, OutputInterface $output): int {
  40. $availableConfigs = $this->helper->getServerConfigurationPrefixes();
  41. $configID = $input->getArgument('configID');
  42. if (!in_array($configID, $availableConfigs)) {
  43. $output->writeln('Invalid configID');
  44. return self::FAILURE;
  45. }
  46. $result = $this->testConfig($configID);
  47. $message = match ($result) {
  48. static::ESTABLISHED => 'The configuration is valid and the connection could be established!',
  49. static::CONF_INVALID => 'The configuration is invalid. Please have a look at the logs for further details.',
  50. static::BINDFAILURE => 'The configuration is valid, but the bind failed. Please check the server settings and credentials.',
  51. static::SEARCHFAILURE => 'The configuration is valid and the bind passed, but a simple search on the base fails. Please check the server base setting.',
  52. default => 'Your LDAP server was kidnapped by aliens.',
  53. };
  54. $output->writeln($message);
  55. return $result === static::ESTABLISHED
  56. ? self::SUCCESS
  57. : self::FAILURE;
  58. }
  59. /**
  60. * Tests the specified connection
  61. */
  62. protected function testConfig(string $configID): int {
  63. $connection = new Connection($this->ldap, $configID);
  64. // Ensure validation is run before we attempt the bind
  65. $connection->getConfiguration();
  66. if (!$connection->setConfiguration([
  67. 'ldap_configuration_active' => 1,
  68. ])) {
  69. return static::CONF_INVALID;
  70. }
  71. if (!$connection->bind()) {
  72. return static::BINDFAILURE;
  73. }
  74. $access = $this->accessFactory->get($connection);
  75. $result = $access->countObjects(1);
  76. if (!is_int($result) || ($result <= 0)) {
  77. return static::SEARCHFAILURE;
  78. }
  79. return static::ESTABLISHED;
  80. }
  81. }