Files
Backend/WEBROOT/services/user_control.php
T
2026-02-20 21:48:09 +01:00

162 lines
5.7 KiB
PHP

<?php
// 1. Logik zum Verarbeiten des Formulars (bevor die Liste geladen wird)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_user'])) {
// Nur Admins dürfen Benutzer hinzufügen (Sicherheitscheck)
if ((new getSpecificData())->getIsAdmin()) {
$controller = new UserController();
$controller->setUser(
$_POST['name'],
$_POST['nachname'],
$_POST['email'],
$_POST['passwort'],
$_POST['rolle'],
$_POST['admin'] ?? 0, // Falls Checkbox nicht gesetzt, 0 senden
$_POST['home_ip']
);
// Optional: Seite neu laden, um die Liste zu aktualisieren
header("Location: " . (new getSpecificData())->getReferer());
exit;
}
}
if (isset($_POST['password'])) {
if (isset($superuser['admin']['password']) && $_POST['password'] === $superuser['admin']['password']) {
$_SESSION['logged_in'] = true;
} else {
$error = "Falsches Passwort!";
}
}
// 2. Daten für die Tabelle holen (dein bestehender Code)
$specificData = new getSpecificData();
$isAdmin = $specificData->getIsAdmin();
if ($isAdmin) {
$users = DatabaseShow("Benutzer", "NAME, NACHNAME, EMAIL, ROLLE, ADMIN, HOME_IP");
} else {
$users = DatabaseShow(
"Benutzer",
"NAME, NACHNAME, EMAIL, ROLLE, ADMIN, HOME_IP",
"NAME = ?",
$specificData->getUsername()
);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_user'])) {
if ((new getSpecificData())->getIsAdmin()) {
$controller = new UserController();
$controller->DeleteUser(
$_POST['delete_name'],
$_POST['delete_nachname'],
$_POST['delete_email']
);
header("Location: " . (new getSpecificData())->getReferer());
exit;
}
}
if (isset($_POST['logout'])) {
unset($_SESSION['logged_in']);
header("Location: " . (new getSpecificData())->getReferer());
exit;
}
?>
<div class="content">
<?php if (!($_SESSION['logged_in'] ?? false)): ?>
<div class="login-container">
<div class="login-box">
<form method="post">
<h2>🔐 Login</h2>
<?php if(isset($error)): ?>
<div class="status-msg" style="background:#dc3545; color:#fff;"><?= htmlspecialchars($error) ?>
</div>
<?php endif; ?>
<input type="password" name="password" placeholder="Passwort" required>
<button type="submit">Einloggen</button>
</form>
</div>
</div>
<?php else: ?>
<h2>Benutzerverwaltung</h2>
<form method="post">
<button type="submit" name="logout" id="logoutSiteButton">Logout</button>
</form>
<?php if ($isAdmin): ?>
<div class="form-container">
<h3>Neuen Benutzer anlegen</h3>
<form method="POST" action="">
<div class="user-form-grid">
<input type="text" name="name" placeholder="Vorname" required>
<input type="text" name="nachname" placeholder="Nachname" required>
<input type="text" name="email" placeholder="E-Mail" required>
<input type="text" name="passwort" placeholder="Passwort" required>
<input type="text" name="rolle" placeholder="Rolle (z.B. User)">
<input type="text" name="home_ip" placeholder="Home IP">
<label class="checkbox-group">
<input type="checkbox" name="admin" value="1">
<span>Admin-Rechte</span>
</label>
</div>
<button type="submit" name="add_user" class="btn-submit">
Benutzer speichern
</button>
</form>
</div>
<?php endif; ?>
<div class="table-container">
<table class="user-table">
<thead>
<tr>
<th>Vorname</th>
<th>Nachname</th>
<th>E-Mail</th>
<?php if ($isAdmin): ?>
<th>Rolle</th>
<th>Admin</th>
<th>Home IP</th>
<th></th>
<?php endif; ?>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= htmlspecialchars($user['NAME']) ?></td>
<td><?= htmlspecialchars($user['NACHNAME']) ?></td>
<td><?= htmlspecialchars($user['EMAIL']) ?></td>
<?php if ($isAdmin): ?>
<td><?= htmlspecialchars($user['ROLLE']) ?></td>
<td><?= htmlspecialchars($user['ADMIN']) ?></td>
<td><?= htmlspecialchars($user['HOME_IP']) ?></td>
<td>
<form method="POST" onsubmit="return confirm('Wirklich löschen?');">
<input type="hidden" name="delete_name" value="<?= htmlspecialchars($user['NAME']) ?>">
<input type="hidden" name="delete_nachname"
value="<?= htmlspecialchars($user['NACHNAME']) ?>">
<input type="hidden" name="delete_email" value="<?= htmlspecialchars($user['EMAIL']) ?>">
<button type="submit" name="delete_user" class="btn-delete">
Löschen
</button>
</form>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php endif; ?>
<script>
</script>