LdapConnection.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\User_LDAP\SetupChecks;
  8. use OCA\User_LDAP\AccessFactory;
  9. use OCA\User_LDAP\ConnectionFactory;
  10. use OCA\User_LDAP\Helper;
  11. use OCP\IL10N;
  12. use OCP\SetupCheck\ISetupCheck;
  13. use OCP\SetupCheck\SetupResult;
  14. class LdapConnection implements ISetupCheck {
  15. public function __construct(
  16. private IL10N $l10n,
  17. private Helper $helper,
  18. private ConnectionFactory $connectionFactory,
  19. private AccessFactory $accessFactory,
  20. ) {
  21. }
  22. public function getCategory(): string {
  23. return 'ldap';
  24. }
  25. public function getName(): string {
  26. return $this->l10n->t('LDAP Connection');
  27. }
  28. public function run(): SetupResult {
  29. $availableConfigs = $this->helper->getServerConfigurationPrefixes();
  30. $inactiveConfigurations = [];
  31. $bindFailedConfigurations = [];
  32. $searchFailedConfigurations = [];
  33. foreach ($availableConfigs as $configID) {
  34. $connection = $this->connectionFactory->get($configID);
  35. if (!$connection->ldapConfigurationActive) {
  36. $inactiveConfigurations[] = $configID;
  37. continue;
  38. }
  39. if (!$connection->bind()) {
  40. $bindFailedConfigurations[] = $configID;
  41. continue;
  42. }
  43. $access = $this->accessFactory->get($connection);
  44. $result = $access->countObjects(1);
  45. if (!is_int($result) || ($result <= 0)) {
  46. $searchFailedConfigurations[] = $configID;
  47. }
  48. }
  49. $output = '';
  50. if (!empty($bindFailedConfigurations)) {
  51. $output .= $this->l10n->n(
  52. 'Binding failed for this LDAP configuration: %s',
  53. 'Binding failed for these LDAP configurations: %s',
  54. count($bindFailedConfigurations),
  55. [implode(',', $bindFailedConfigurations)]
  56. )."\n";
  57. }
  58. if (!empty($searchFailedConfigurations)) {
  59. $output .= $this->l10n->n(
  60. 'Searching failed for this LDAP configuration: %s',
  61. 'Searching failed for these LDAP configurations: %s',
  62. count($searchFailedConfigurations),
  63. [implode(',', $searchFailedConfigurations)]
  64. )."\n";
  65. }
  66. if (!empty($inactiveConfigurations)) {
  67. $output .= $this->l10n->n(
  68. 'There is an inactive LDAP configuration: %s',
  69. 'There are inactive LDAP configurations: %s',
  70. count($inactiveConfigurations),
  71. [implode(',', $inactiveConfigurations)]
  72. )."\n";
  73. }
  74. if (!empty($bindFailedConfigurations) || !empty($searchFailedConfigurations)) {
  75. return SetupResult::error($output);
  76. } elseif (!empty($inactiveConfigurations)) {
  77. return SetupResult::warning($output);
  78. }
  79. return SetupResult::success($this->l10n->n(
  80. 'Binding and searching works on the configured LDAP connection (%s)',
  81. 'Binding and searching works on all of the configured LDAP connections (%s)',
  82. count($availableConfigs),
  83. [implode(',', $availableConfigs)]
  84. ));
  85. }
  86. }