154 lines
5.5 KiB
PHP
154 lines
5.5 KiB
PHP
<?php
|
||
|
||
// Strenge Typprüfung für Parameter- und Rückgabetypen aktivieren.
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Controllers;
|
||
|
||
use App\Services\Ldap\LdapDirectoryService;
|
||
|
||
/**
|
||
* Controller für die Benutzer- und Gruppenanzeige.
|
||
*
|
||
* Aufgaben:
|
||
* - holt über den LdapDirectoryService die Listen von Benutzern und Gruppen
|
||
* - behandelt technische Fehler und bereitet eine Fehlermeldung für die View auf
|
||
* - gibt die Daten an eine View-Datei (public/views/users.php) weiter
|
||
*
|
||
* WICHTIG:
|
||
* - Es werden aktuell nur Daten angezeigt (Read-only).
|
||
* - Es findet keine Änderung im Active Directory statt.
|
||
*/
|
||
class UserManagementController
|
||
{
|
||
/** @var array<string, mixed> Vollständige Anwendungskonfiguration (aus config.php) */
|
||
private array $config;
|
||
|
||
/** @var LdapDirectoryService Service für das Lesen von Benutzern und Gruppen aus dem LDAP/AD */
|
||
private LdapDirectoryService $directoryService;
|
||
|
||
/**
|
||
* @param array<string, mixed> $config Vollständige Konfiguration aus config.php
|
||
*/
|
||
public function __construct(array $config)
|
||
{
|
||
// Komplette Konfiguration speichern (falls später weitere Werte benötigt werden).
|
||
$this->config = $config;
|
||
|
||
// LDAP-Konfiguration aus der Gesamt-Konfiguration herausziehen.
|
||
$ldapConfig = $config['ldap'] ?? [];
|
||
|
||
// Directory-Service initialisieren, der die eigentliche LDAP-Arbeit übernimmt.
|
||
$this->directoryService = new LdapDirectoryService($ldapConfig);
|
||
}
|
||
|
||
/**
|
||
* Zeigt Benutzer- und Gruppenliste an.
|
||
* Wird typischerweise über die Route "users" (index.php?route=users) aufgerufen.
|
||
*/
|
||
public function show(): void
|
||
{
|
||
// Standardwerte für die View-Variablen vorbereiten.
|
||
$error = null;
|
||
$users = [];
|
||
$groups = [];
|
||
|
||
try {
|
||
// Benutzer- und Gruppenlisten aus dem AD laden.
|
||
$users = $this->directoryService->getUsers();
|
||
$groups = $this->directoryService->getGroups();
|
||
} catch (\Throwable $exception) {
|
||
// Sämtliche technischen Fehler (z. B. Verbindungs- oder Konfigurationsprobleme)
|
||
// werden hier in eine für den Benutzer lesbare Fehlermeldung übersetzt.
|
||
$error = 'Fehler beim Laden von Benutzern/Gruppen: ' . $exception->getMessage();
|
||
}
|
||
|
||
// Pfad zur eigentlichen View-Datei bestimmen.
|
||
$viewPath = __DIR__ . '/../../public/views/users.php';
|
||
|
||
// Falls die View-Datei (noch) nicht existiert, Fallback-Ausgabe verwenden.
|
||
if (file_exists($viewPath) === false) {
|
||
$this->renderInline($users, $groups, $error);
|
||
return;
|
||
}
|
||
|
||
// Variablen $users, $groups, $error stehen in der View zur Verfügung,
|
||
// weil sie im aktuellen Scope definiert sind.
|
||
require $viewPath;
|
||
}
|
||
|
||
/**
|
||
* Fallback-Ausgabe, falls noch keine View-Datei existiert.
|
||
*
|
||
* @param array<int, array<string, string>> $users Liste der Benutzer-Datensätze
|
||
* @param array<int, array<string, string>> $groups Liste der Gruppen-Datensätze
|
||
* @param string|null $error Fehlermeldung (falls vorhanden)
|
||
*/
|
||
private function renderInline(array $users, array $groups, ?string $error): void
|
||
{
|
||
?>
|
||
<!doctype html>
|
||
<html lang="de">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<title>AD Admin Tool – Benutzer & Gruppen</title>
|
||
</head>
|
||
<body>
|
||
<h1>Benutzer & Gruppen</h1>
|
||
|
||
<?php if ($error !== null): ?>
|
||
<!-- Fehlermeldung ausgeben, HTML-sicher maskiert -->
|
||
<p style="color: red;"><?php echo htmlspecialchars($error, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?></p>
|
||
<?php endif; ?>
|
||
|
||
<h2>Benutzer</h2>
|
||
<table border="1" cellpadding="4" cellspacing="0">
|
||
<thead>
|
||
<tr>
|
||
<th>Benutzername (sAMAccountName)</th>
|
||
<th>Anzeigename</th>
|
||
<th>E-Mail</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php foreach ($users as $user): ?>
|
||
<tr>
|
||
<td><?php echo htmlspecialchars($user['samaccountname'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?></td>
|
||
<td><?php echo htmlspecialchars($user['displayname'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?></td>
|
||
<td><?php echo htmlspecialchars($user['mail'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?></td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</tbody>
|
||
</table>
|
||
|
||
<h2>Gruppen</h2>
|
||
<table border="1" cellpadding="4" cellspacing="0">
|
||
<thead>
|
||
<tr>
|
||
<th>Gruppenname (sAMAccountName)</th>
|
||
<th>CN</th>
|
||
<th>Beschreibung</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php foreach ($groups as $group): ?>
|
||
<tr>
|
||
<td><?php echo htmlspecialchars($group['samaccountname'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?></td>
|
||
<td><?php echo htmlspecialchars($group['cn'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?></td>
|
||
<td><?php echo htmlspecialchars($group['description'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?></td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p>
|
||
<a href="index.php?route=dashboard">Zurück zum Dashboard</a> |
|
||
<a href="index.php?route=logout">Logout</a>
|
||
</p>
|
||
|
||
</body>
|
||
</html>
|
||
<?php
|
||
}
|
||
}
|