Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
56.06% |
37 / 66 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| LdapController | |
56.06% |
37 / 66 |
|
0.00% |
0 / 2 |
15.87 | |
0.00% |
0 / 1 |
| configuration | |
91.30% |
21 / 23 |
|
0.00% |
0 / 1 |
1.00 | |||
| healthcheck | |
37.21% |
16 / 43 |
|
0.00% |
0 / 1 |
23.84 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The Admin LDAP API Controller |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | * @copyright 2026 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2026-03-05 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Controller\Administration\Api; |
| 21 | |
| 22 | use phpMyFAQ\Controller\AbstractController; |
| 23 | use phpMyFAQ\Enums\PermissionType; |
| 24 | use phpMyFAQ\Ldap; |
| 25 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 26 | use Symfony\Component\HttpFoundation\Response; |
| 27 | use Symfony\Component\Routing\Attribute\Route; |
| 28 | |
| 29 | final class LdapController extends AbstractController |
| 30 | { |
| 31 | #[Route(path: 'ldap/configuration', name: 'admin.api.ldap.configuration', methods: ['GET'])] |
| 32 | public function configuration(): JsonResponse |
| 33 | { |
| 34 | $this->userHasPermission(PermissionType::CONFIGURATION_EDIT); |
| 35 | |
| 36 | $servers = $this->configuration->getLdapServer(); |
| 37 | $strippedServers = array_map( |
| 38 | static function (array $server): array { |
| 39 | /* @mago-expect lint:no-literal-password - masking placeholder, not a credential */ |
| 40 | $server['ldap_password'] = '********'; |
| 41 | return $server; |
| 42 | }, |
| 43 | $servers, |
| 44 | ); |
| 45 | |
| 46 | return $this->json([ |
| 47 | 'servers' => $strippedServers, |
| 48 | 'mapping' => $this->configuration->getLdapMapping(), |
| 49 | 'options' => $this->configuration->getLdapOptions(), |
| 50 | 'groupConfig' => $this->configuration->getLdapGroupConfig(), |
| 51 | 'generalSettings' => [ |
| 52 | 'domainPrefix' => (bool) $this->configuration->get(item: 'ldap.ldap_use_domain_prefix'), |
| 53 | 'sasl' => (bool) $this->configuration->get(item: 'ldap.ldap_use_sasl'), |
| 54 | 'anonymousLogin' => (bool) $this->configuration->get(item: 'ldap.ldap_use_anonymous_login'), |
| 55 | 'dynamicLogin' => (bool) $this->configuration->get(item: 'ldap.ldap_use_dynamic_login'), |
| 56 | 'dynamicLoginAttribute' => $this->configuration->get(item: 'ldap.ldap_dynamic_login_attribute') ?? '', |
| 57 | 'multipleServers' => (bool) $this->configuration->get(item: 'ldap.ldap_use_multiple_servers'), |
| 58 | ], |
| 59 | ], Response::HTTP_OK); |
| 60 | } |
| 61 | |
| 62 | #[Route(path: 'ldap/healthcheck', name: 'admin.api.ldap.healthcheck', methods: ['GET'])] |
| 63 | public function healthcheck(): JsonResponse |
| 64 | { |
| 65 | $this->userHasPermission(PermissionType::CONFIGURATION_EDIT); |
| 66 | |
| 67 | if (!extension_loaded('ldap')) { |
| 68 | return $this->json([ |
| 69 | 'available' => false, |
| 70 | 'status' => 'unavailable', |
| 71 | 'error' => 'PHP LDAP extension is not loaded.', |
| 72 | 'servers' => [], |
| 73 | ], Response::HTTP_SERVICE_UNAVAILABLE); |
| 74 | } |
| 75 | |
| 76 | $servers = $this->configuration->getLdapServer(); |
| 77 | |
| 78 | if ($servers === []) { |
| 79 | return $this->json([ |
| 80 | 'available' => false, |
| 81 | 'status' => 'unavailable', |
| 82 | 'error' => 'No LDAP server configured.', |
| 83 | 'servers' => [], |
| 84 | ], Response::HTTP_SERVICE_UNAVAILABLE); |
| 85 | } |
| 86 | |
| 87 | $allHealthy = true; |
| 88 | $serverResults = []; |
| 89 | |
| 90 | foreach ($servers as $index => $server) { |
| 91 | $ldap = new Ldap($this->configuration); |
| 92 | $connected = $ldap->connect( |
| 93 | (string) ($server['ldap_server'] ?? ''), |
| 94 | (int) ($server['ldap_port'] ?? 389), |
| 95 | (string) ($server['ldap_base'] ?? ''), |
| 96 | (string) ($server['ldap_user'] ?? ''), |
| 97 | (string) ($server['ldap_password'] ?? ''), |
| 98 | ); |
| 99 | |
| 100 | $serverResults[] = [ |
| 101 | 'index' => $index, |
| 102 | 'host' => $server['ldap_server'] ?? '', |
| 103 | 'available' => $connected, |
| 104 | 'error' => $connected ? null : $ldap->error ?? 'Unable to connect.', |
| 105 | ]; |
| 106 | |
| 107 | if (!$connected) { |
| 108 | $allHealthy = false; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $this->json( |
| 113 | [ |
| 114 | 'available' => $allHealthy, |
| 115 | 'status' => $allHealthy ? 'healthy' : 'degraded', |
| 116 | 'servers' => $serverResults, |
| 117 | ], |
| 118 | $allHealthy ? Response::HTTP_OK : Response::HTTP_SERVICE_UNAVAILABLE, |
| 119 | ); |
| 120 | } |
| 121 | } |