Compare commits

..

3 Commits

Author SHA1 Message Date
d268a8bf6a Verbindung auf LdapConnectionHelper umgestellt. 2025-11-19 07:54:12 +01:00
a6c6e8d67a Connection Helper-Klasse erstellt 2025-11-19 07:53:44 +01:00
a7bb3cac6e Datei gelöscht 2025-11-19 07:53:10 +01:00
3 changed files with 55 additions and 16 deletions

View File

@ -19,6 +19,8 @@ class LdapAuthService
/** @var array<string, mixed> LDAP-spezifische Konfiguration (Server, Port, Domain-Suffix, Timeout, etc.) */
private array $config;
private LdapConnectionHelper $connectionHelper;
/**
* Erwartet den Teilbereich "ldap" aus der allgemeinen Konfiguration (config.php).
*
@ -28,6 +30,7 @@ class LdapAuthService
{
// LDAP-Konfiguration in der Instanz speichern.
$this->config = $ldapConfig;
$this->connectionHelper = new LdapConnectionHelper($ldapConfig);
}
/**
@ -50,30 +53,17 @@ class LdapAuthService
// Benötigte Werte aus der LDAP-Konfiguration auslesen.
// Falls einzelne Werte fehlen, werden sinnvolle Standardwerte gesetzt.
$server = (string)($this->config['server'] ?? '');
$port = (int)($this->config['port'] ?? 389);
$domainSuffix = (string)($this->config['domain_suffix'] ?? '');
$timeout = (int)($this->config['timeout'] ?? 5);
// Ohne Server-Adresse oder Domain-Suffix ist eine sinnvolle Anmeldung nicht möglich.
// Das ist ein Konfigurationsfehler und wird als technischer Fehler behandelt.
if ($server === '' || $domainSuffix === '') {
throw new RuntimeException('LDAP-Konfiguration ist unvollständig.');
if ($domainSuffix === '') {
throw new RuntimeException('LDAP-Konfiguration ist unvollständig (domain_suffix fehlt).');
}
// Verbindung zum LDAP/AD-Server herstellen.
// Rückgabewert ist eine Ressource (Verbindungshandle) oder false bei Fehlern.
$connection = ldap_connect($server, $port);
if ($connection === false) {
throw new RuntimeException('LDAP-Verbindung konnte nicht aufgebaut werden.');
}
// LDAP-Optionen setzen:
// - Protokollversion 3 (Standard in aktuellen Umgebungen)
// - Netzwerk-Timeout, damit die Anfrage nicht unendlich hängt
ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connection, LDAP_OPT_NETWORK_TIMEOUT, $timeout);
$connection = $this->connectionHelper->createConnection();
// UPN (User Principal Name) aus Benutzername und Domain-Suffix bilden.
// Beispiel: "administrator" + "@ITFA-PROJ-DOM.local" -> "administrator@ITFA-PROJ-DOM.local"

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace App\Services\Ldap;
use RuntimeException;
class LdapConnectionHelper
{
/** @var array<string, mixed> */
private array $config;
/**
* @param array<string, mixed> $ldapConfig
*/
public function __construct(array $ldapConfig)
{
$this->config = $ldapConfig;
}
/**
* Erstellt eine LDAP-Verbindung mit gesetzten Optionen (Protokollversion, Timeout),
* aber ohne Bind. Den Bind führen die aufrufenden Services durch.
*
* @return resource LDAP-Verbindungs-Handle
*/
public function createConnection()
{
$server = (string)($this->config['server'] ?? '');
$port = (int)($this->config['port'] ?? 636);
$timeout = (int)($this->config['timeout'] ?? 5);
if ($server === '') {
throw new RuntimeException('LDAP-Konfiguration ist unvollständig (server fehlt).');
}
$connection = ldap_connect($server, $port);
if ($connection === false) {
throw new RuntimeException('LDAP-Verbindung konnte nicht aufgebaut werden.');
}
ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connection, LDAP_OPT_NETWORK_TIMEOUT, $timeout);
return $connection;
}
}