Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
52 / 52 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| LdapSettings | |
100.00% |
52 / 52 |
|
100.00% |
7 / 7 |
13 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| buildServers | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
5 | |||
| buildConfig | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| getLdapMapping | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getLdapOptions | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getLdapGroupConfig | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| isActive | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The LDAP settings class |
| 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 2025-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 2025-11-03 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Configuration; |
| 21 | |
| 22 | use phpMyFAQ\Configuration as CoreConfiguration; |
| 23 | |
| 24 | readonly class LdapSettings |
| 25 | { |
| 26 | public function __construct( |
| 27 | private CoreConfiguration $coreConfiguration, |
| 28 | ) { |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Erzeugt das core.ldapServer-Array basierend auf der übergebenen LdapConfiguration. |
| 33 | * @return array<int, array<string, mixed>> |
| 34 | */ |
| 35 | public function buildServers(LdapConfiguration $ldapConfiguration): array |
| 36 | { |
| 37 | $servers = []; |
| 38 | |
| 39 | // Main LDAP server |
| 40 | $servers[0] = [ |
| 41 | 'ldap_server' => $ldapConfiguration->getMainServer(), |
| 42 | 'ldap_port' => $ldapConfiguration->getMainPort(), |
| 43 | 'ldap_user' => $ldapConfiguration->getMainUser(), |
| 44 | 'ldap_password' => $ldapConfiguration->getMainPassword(), |
| 45 | 'ldap_base' => $ldapConfiguration->getMainBase(), |
| 46 | ]; |
| 47 | |
| 48 | // Additional servers if enabled |
| 49 | if (true === $this->coreConfiguration->get(item: 'ldap.ldap_use_multiple_servers')) { |
| 50 | $key = 1; |
| 51 | $configuredServers = $ldapConfiguration->getServers(); |
| 52 | while (array_key_exists($key, $configuredServers)) { |
| 53 | $configuredServer = $configuredServers[$key]; |
| 54 | $normalizedServer = []; |
| 55 | if (is_array($configuredServer)) { |
| 56 | foreach ($configuredServer as $serverKey => $serverValue) { |
| 57 | $normalizedServer[(string) $serverKey] = $serverValue; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | $servers[$key] = $normalizedServer; |
| 62 | ++$key; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return $servers; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Builds the core.ldapConfig array from current configuration values. |
| 71 | * @return array<string, mixed> |
| 72 | */ |
| 73 | public function buildConfig(): array |
| 74 | { |
| 75 | return [ |
| 76 | 'ldap_use_multiple_servers' => $this->coreConfiguration->get(item: 'ldap.ldap_use_multiple_servers'), |
| 77 | 'ldap_mapping' => $this->getLdapMapping(), |
| 78 | 'ldap_use_domain_prefix' => $this->coreConfiguration->get(item: 'ldap.ldap_use_domain_prefix'), |
| 79 | 'ldap_options' => $this->getLdapOptions(), |
| 80 | 'ldap_use_memberOf' => $this->coreConfiguration->get(item: 'ldap.ldap_use_memberOf'), |
| 81 | 'ldap_use_sasl' => $this->coreConfiguration->get(item: 'ldap.ldap_use_sasl'), |
| 82 | 'ldap_use_anonymous_login' => $this->coreConfiguration->get(item: 'ldap.ldap_use_anonymous_login'), |
| 83 | 'ldap_group_config' => $this->getLdapGroupConfig(), |
| 84 | ]; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @return array<string, string> |
| 89 | */ |
| 90 | public function getLdapMapping(): array |
| 91 | { |
| 92 | return [ |
| 93 | 'name' => (string) $this->coreConfiguration->get(item: 'ldap.ldap_mapping.name'), |
| 94 | 'username' => (string) $this->coreConfiguration->get(item: 'ldap.ldap_mapping.username'), |
| 95 | 'mail' => (string) $this->coreConfiguration->get(item: 'ldap.ldap_mapping.mail'), |
| 96 | 'memberOf' => (string) $this->coreConfiguration->get(item: 'ldap.ldap_mapping.memberOf'), |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return array<string, mixed> |
| 102 | */ |
| 103 | public function getLdapOptions(): array |
| 104 | { |
| 105 | return [ |
| 106 | 'LDAP_OPT_PROTOCOL_VERSION' => $this->coreConfiguration->get( |
| 107 | item: 'ldap.ldap_options.LDAP_OPT_PROTOCOL_VERSION', |
| 108 | ), |
| 109 | 'LDAP_OPT_REFERRALS' => $this->coreConfiguration->get(item: 'ldap.ldap_options.LDAP_OPT_REFERRALS'), |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @return array<string, mixed> |
| 115 | */ |
| 116 | public function getLdapGroupConfig(): array |
| 117 | { |
| 118 | $allowedGroups = $this->coreConfiguration->get(item: 'ldap.ldap_group_allowed_groups'); |
| 119 | $groupMapping = $this->coreConfiguration->get(item: 'ldap.ldap_group_mapping'); |
| 120 | |
| 121 | return [ |
| 122 | 'use_group_restriction' => $this->coreConfiguration->get(item: 'ldap.ldap_use_group_restriction'), |
| 123 | 'allowed_groups' => $allowedGroups ? explode(separator: ',', string: (string) $allowedGroups) : [], |
| 124 | 'auto_assign' => $this->coreConfiguration->get(item: 'ldap.ldap_group_auto_assign'), |
| 125 | 'group_mapping' => $groupMapping ? json_decode((string) $groupMapping, associative: true) : [], |
| 126 | ]; |
| 127 | } |
| 128 | |
| 129 | public function isActive(): bool |
| 130 | { |
| 131 | return filter_var($this->coreConfiguration->get(item: 'ldap.ldapSupport'), FILTER_VALIDATE_BOOLEAN); |
| 132 | } |
| 133 | } |