Add role assignment for new users during registration and update admin panel role selection to use a dropdown

This commit is contained in:
Fabian Schieder 2026-03-30 22:25:52 +02:00
parent 7a33971df0
commit f404ca305e
2 changed files with 29 additions and 7 deletions

View File

@ -124,14 +124,14 @@ $usersResult = $conn->query("
<?php if (!$isSelf): ?> <?php if (!$isSelf): ?>
<form method="post" action="admin_users.php" style="margin: 0; display: flex; flex-direction: column; gap: 5px;"> <form method="post" action="admin_users.php" style="margin: 0; display: flex; flex-direction: column; gap: 5px;">
<input type="hidden" name="update_roles_user_id" value="<?= $user['userID'] ?>"> <input type="hidden" name="update_roles_user_id" value="<?= $user['userID'] ?>">
<div style="display: flex; flex-wrap: wrap; gap: 10px;"> <select name="roles[]" multiple size="3" style="background: #0f172a; color: #f8fafc; border: 1px solid #334155; padding: 5px; border-radius: 4px; font-size: 0.85rem; width: 100%;">
<?php foreach ($allRoles as $role): ?> <?php foreach ($allRoles as $role): ?>
<label style="font-size: 0.85rem; cursor: pointer; display: flex; align-items: center; gap: 4px;"> <option value="<?= $role['roleID'] ?>" <?= in_array($role['roleID'], $userRoles) ? 'selected' : '' ?>>
<input type="checkbox" name="roles[]" value="<?= $role['roleID'] ?>" <?= in_array($role['roleID'], $userRoles) ? 'checked' : '' ?>>
<?= htmlspecialchars($role['name']) ?> <?= htmlspecialchars($role['name']) ?>
</label> </option>
<?php endforeach; ?> <?php endforeach; ?>
</div> </select>
<p style="font-size: 0.7rem; color: #94a3b8; margin: 0;">Strg/Cmd für Mehrfachauswahl</p>
<button type="submit" style="background-color: #3b82f6; color: white; border: none; padding: 4px 8px; border-radius: 4px; cursor: pointer; font-size: 0.75rem; width: fit-content; margin-top: 5px;">Speichern</button> <button type="submit" style="background-color: #3b82f6; color: white; border: none; padding: 4px 8px; border-radius: 4px; cursor: pointer; font-size: 0.75rem; width: fit-content; margin-top: 5px;">Speichern</button>
</form> </form>
<?php else: ?> <?php else: ?>

View File

@ -124,15 +124,37 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
{ {
mysqli_stmt_bind_param($stmt, 'ssss', $email, $hash, $displayName, $profilePicture); mysqli_stmt_bind_param($stmt, 'ssss', $email, $hash, $displayName, $profilePicture);
$ok = mysqli_stmt_execute($stmt); $ok = mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
if ($ok) if ($ok)
{ {
$newUserId = mysqli_insert_id($conn);
// Get USER roleID
$roleStmt = mysqli_prepare($conn, "SELECT roleID FROM roles WHERE name = 'USER' LIMIT 1");
if ($roleStmt) {
mysqli_stmt_execute($roleStmt);
$roleResult = mysqli_stmt_get_result($roleStmt);
if ($roleRow = mysqli_fetch_assoc($roleResult)) {
$userRoleId = $roleRow['roleID'];
$insertRoleStmt = mysqli_prepare($conn, "INSERT INTO userRoles (userID, roleID) VALUES (?, ?)");
if ($insertRoleStmt) {
mysqli_stmt_bind_param($insertRoleStmt, 'ii', $newUserId, $userRoleId);
mysqli_stmt_execute($insertRoleStmt);
mysqli_stmt_close($insertRoleStmt);
}
}
mysqli_stmt_close($roleStmt);
}
mysqli_stmt_close($stmt);
mysqli_close($conn); mysqli_close($conn);
header('Location: login.php?registered=1'); header('Location: login.php?registered=1');
exit; exit;
} }
mysqli_stmt_close($stmt);
$errors[] = 'Registrierung fehlgeschlagen.'; $errors[] = 'Registrierung fehlgeschlagen.';
} }
} }