PHP_AdminTool_Projekt/app/Services/Ldap/LdapConnectionHelper.php

50 lines
1.3 KiB
PHP

<?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;
}
}