Files
domili/admin/users.php
2025-08-11 17:02:16 +02:00

127 lines
5.3 KiB
PHP
Executable File

<?php
include('../inc/check_login.php');
require_once('../inc/db.php');
// Zugriff nur für eingeloggte Admins
if ($_SESSION['role'] !== 'admin') {
die("Zugriff nur für Admins");
}
$message = '';
$message_type = '';
$edit_mode = false;
$edit_user = null;
// Benutzer hinzufügen
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['username'], $_POST['password'], $_POST['role'])) {
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$role = $_POST['role'] === 'admin' ? 'admin' : 'member';
$sql = "INSERT INTO users (username, password, role) VALUES ('$username', '$password', '$role')";
if (mysqli_query($conn, $sql)) {
$message = "Benutzer erfolgreich hinzugefügt.";
$message_type = 'success';
} else {
$message = "Fehler beim Hinzufügen: " . mysqli_error($conn);
$message_type = 'danger';
}
}
// Benutzerübersicht abrufen
$users = [];
$result = mysqli_query($conn, "SELECT id, username, role FROM users ORDER BY username ASC");
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$users[] = $row;
}
}
require_once('../inc/header.php');
?>
<div class="container mt-5">
<h2 class="mb-4">Benutzerverwaltung</h2>
<?php if ($message) : ?>
<div id="status-message" class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
<?php echo htmlspecialchars($message); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="card shadow mb-4">
<div class="card-header bg-primary text-white">
<h4 class="mb-0">Neuen Benutzer hinzufügen</h4>
</div>
<div class="card-body">
<form method="post">
<div class="row g-3 align-items-end">
<div class="col-md-4">
<label for="username" class="form-label">Benutzername</label>
<input type="text" class="form-control" id="username" name="username" required>
<div class="form-text" style="visibility: hidden;">&nbsp;</div>
</div>
<div class="col-md-4">
<label for="password" class="form-label">Passwort</label>
<input type="password" class="form-control" id="password" name="password" required>
<div class="form-text" style="visibility: hidden;">&nbsp;</div>
</div>
<div class="col-md-4 d-flex flex-column justify-content-end">
<label for="role" class="form-label">Rolle</label>
<div class="d-flex w-100">
<select class="form-select w-100" id="role" name="role">
<option value="member">Mitglied</option>
<option value="admin">Admin</option>
</select>
</div>
</div><button type="submit" class="btn btn-primary ms-2">Hinzufügen</button>
</div>
</form>
</div>
</div>
<div class="card shadow">
<div class="card-header bg-secondary text-white">
<h4 class="mb-0">Benutzerübersicht</h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Benutzername</th>
<th>Rolle</th>
<th>Aktionen</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= htmlspecialchars($user['id']) ?></td>
<td><?= htmlspecialchars($user['username']) ?></td>
<td>
<span class="badge rounded-pill bg-<?= $user['role'] === 'admin' ? 'info' : 'secondary' ?>">
<?= htmlspecialchars($user['role']) ?>
</span>
</td>
<td>
<a href="#" class="text-dark me-1 text-decoration-none" data-bs-toggle="tooltip" data-bs-placement="top" title="Bearbeiten">
<span class="material-icons">mode_edit_outline</span>
</a>
<a href="#" class="text-danger text-decoration-none" data-bs-toggle="tooltip" data-bs-placement="top" title="Löschen">
<span class="material-icons">delete_outline</span>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php include('../inc/footer.php'); ?>