LdapConnection.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\User_LDAP\SetupChecks;
  25. use OCA\User_LDAP\AccessFactory;
  26. use OCA\User_LDAP\ConnectionFactory;
  27. use OCA\User_LDAP\Helper;
  28. use OCP\IL10N;
  29. use OCP\SetupCheck\ISetupCheck;
  30. use OCP\SetupCheck\SetupResult;
  31. class LdapConnection implements ISetupCheck {
  32. public function __construct(
  33. private IL10N $l10n,
  34. private Helper $helper,
  35. private ConnectionFactory $connectionFactory,
  36. private AccessFactory $accessFactory,
  37. ) {
  38. }
  39. public function getCategory(): string {
  40. return 'ldap';
  41. }
  42. public function getName(): string {
  43. return $this->l10n->t('LDAP Connection');
  44. }
  45. public function run(): SetupResult {
  46. $availableConfigs = $this->helper->getServerConfigurationPrefixes();
  47. $inactiveConfigurations = [];
  48. $bindFailedConfigurations = [];
  49. $searchFailedConfigurations = [];
  50. foreach ($availableConfigs as $configID) {
  51. $connection = $this->connectionFactory->get($configID);
  52. if (!$connection->ldapConfigurationActive) {
  53. $inactiveConfigurations[] = $configID;
  54. continue;
  55. }
  56. if (!$connection->bind()) {
  57. $bindFailedConfigurations[] = $configID;
  58. continue;
  59. }
  60. $access = $this->accessFactory->get($connection);
  61. $result = $access->countObjects(1);
  62. if (!is_int($result) || ($result <= 0)) {
  63. $searchFailedConfigurations[] = $configID;
  64. }
  65. }
  66. $output = '';
  67. if (!empty($bindFailedConfigurations)) {
  68. $output .= $this->l10n->n(
  69. 'Binding failed for this LDAP configuration: %s',
  70. 'Binding failed for these LDAP configurations: %s',
  71. count($bindFailedConfigurations),
  72. [implode(',', $bindFailedConfigurations)]
  73. )."\n";
  74. }
  75. if (!empty($searchFailedConfigurations)) {
  76. $output .= $this->l10n->n(
  77. 'Searching failed for this LDAP configuration: %s',
  78. 'Searching failed for these LDAP configurations: %s',
  79. count($searchFailedConfigurations),
  80. [implode(',', $searchFailedConfigurations)]
  81. )."\n";
  82. }
  83. if (!empty($inactiveConfigurations)) {
  84. $output .= $this->l10n->n(
  85. 'There is an inactive LDAP configuration: %s',
  86. 'There are inactive LDAP configurations: %s',
  87. count($inactiveConfigurations),
  88. [implode(',', $inactiveConfigurations)]
  89. )."\n";
  90. }
  91. if (!empty($bindFailedConfigurations) || !empty($searchFailedConfigurations)) {
  92. return SetupResult::error($output);
  93. } elseif (!empty($inactiveConfigurations)) {
  94. return SetupResult::warning($output);
  95. }
  96. return SetupResult::success($this->l10n->n(
  97. 'Binding and searching works on the configured LDAP connection (%s)',
  98. 'Binding and searching works on all of the configured LDAP connections (%s)',
  99. count($availableConfigs),
  100. [implode(',', $availableConfigs)]
  101. ));
  102. }
  103. }