refactor session management and access control for improved security and maintainability
This commit is contained in:
parent
2039b7ba0f
commit
9e9c18cbaa
@ -1,10 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
ini_set('display_errors', 1);
|
require_once __DIR__ . '/lib/bootstrap.php';
|
||||||
ini_set('display_startup_errors', 1);
|
|
||||||
error_reporting(E_ALL);
|
|
||||||
|
|
||||||
session_start();
|
|
||||||
|
|
||||||
if (empty($_SESSION['user_id']))
|
if (empty($_SESSION['user_id']))
|
||||||
{
|
{
|
||||||
@ -123,9 +119,11 @@ include 'header.php';
|
|||||||
Schnellaktionen
|
Schnellaktionen
|
||||||
</h2>
|
</h2>
|
||||||
<div class="account__quick-actions">
|
<div class="account__quick-actions">
|
||||||
|
<?php if (!empty($_SESSION['user_roles']) && in_array('ADMIN', $_SESSION['user_roles'], true)): ?>
|
||||||
<a href="productAdder.php" class="auth__submit account__action-link">
|
<a href="productAdder.php" class="auth__submit account__action-link">
|
||||||
Produkt hinzufügen
|
Produkt hinzufügen
|
||||||
</a>
|
</a>
|
||||||
|
<?php endif; ?>
|
||||||
<a href="wunschliste.php" class="auth__submit account__action-link account__action-link--secondary">
|
<a href="wunschliste.php" class="auth__submit account__action-link account__action-link--secondary">
|
||||||
Meine Wunschliste
|
Meine Wunschliste
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
// Zentraler Bootstrap: muss vor jeglicher HTML-Ausgabe inkludiert werden.
|
// Zentraler Bootstrap: muss vor jeglicher HTML-Ausgabe inkludiert werden.
|
||||||
// - startet die Session genau einmal
|
// - startet die Session genau einmal
|
||||||
// - setzt sinnvolle PHP-Error-Settings für die Entwicklung
|
// - setzt sinnvolle PHP-Error-Settings für die Entwicklung
|
||||||
|
// - lädt die Rollen des eingeloggten Users bei jedem Request
|
||||||
|
|
||||||
ini_set('display_errors', '1');
|
ini_set('display_errors', '1');
|
||||||
ini_set('display_startup_errors', '1');
|
ini_set('display_startup_errors', '1');
|
||||||
@ -10,3 +11,28 @@ error_reporting(E_ALL);
|
|||||||
if (session_status() !== PHP_SESSION_ACTIVE) {
|
if (session_status() !== PHP_SESSION_ACTIVE) {
|
||||||
session_start();
|
session_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rollen bei jedem Request aus der DB aktualisieren
|
||||||
|
if (!empty($_SESSION['user_id'])) {
|
||||||
|
$__bsConn = new mysqli('localhost', 'FSST', 'L9wUNZZ9Qkbt', 'FSST', 3306);
|
||||||
|
if (!$__bsConn->connect_error) {
|
||||||
|
$__bsStmt = $__bsConn->prepare(
|
||||||
|
'SELECT r.name FROM userRoles ur JOIN roles r ON r.roleID = ur.roleID WHERE ur.userID = ?'
|
||||||
|
);
|
||||||
|
if ($__bsStmt) {
|
||||||
|
$__bsUid = (int)$_SESSION['user_id'];
|
||||||
|
$__bsStmt->bind_param('i', $__bsUid);
|
||||||
|
$__bsStmt->execute();
|
||||||
|
$__bsResult = $__bsStmt->get_result();
|
||||||
|
$_SESSION['user_roles'] = [];
|
||||||
|
while ($__bsRow = $__bsResult->fetch_assoc()) {
|
||||||
|
$_SESSION['user_roles'][] = $__bsRow['name'];
|
||||||
|
}
|
||||||
|
$__bsStmt->close();
|
||||||
|
}
|
||||||
|
$__bsConn->close();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$_SESSION['user_roles'] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
// login.php
|
// login.php
|
||||||
|
|
||||||
ini_set('display_errors', 1);
|
require_once __DIR__ . '/lib/bootstrap.php';
|
||||||
ini_set('display_startup_errors', 1);
|
|
||||||
error_reporting(E_ALL);
|
|
||||||
|
|
||||||
session_start();
|
|
||||||
|
|
||||||
// 1) DB-Verbindung (einmal)
|
// 1) DB-Verbindung (einmal)
|
||||||
$servername = "localhost";
|
$servername = "localhost";
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
require_once __DIR__ . '/lib/bootstrap.php';
|
||||||
|
|
||||||
/* Alle Session-Variablen löschen */
|
/* Alle Session-Variablen löschen */
|
||||||
$_SESSION = [];
|
$_SESSION = [];
|
||||||
|
|||||||
@ -1,10 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
// product_add.php
|
// product_add.php
|
||||||
|
|
||||||
ini_set('display_errors', 1);
|
require_once __DIR__ . '/lib/bootstrap.php';
|
||||||
error_reporting(E_ALL);
|
|
||||||
|
|
||||||
session_start();
|
/* =======================
|
||||||
|
0) Zugriffskontrolle – nur ADMIN
|
||||||
|
======================= */
|
||||||
|
if (empty($_SESSION['user_id']) || empty($_SESSION['user_roles']) || !in_array('ADMIN', $_SESSION['user_roles'], true)) {
|
||||||
|
http_response_code(403);
|
||||||
|
include 'header.php';
|
||||||
|
echo '<main class="auth"><section class="auth__grid"><div class="auth__card">';
|
||||||
|
echo '<h2 class="auth__title">Zugriff verweigert</h2>';
|
||||||
|
echo '<p>Du hast keine Berechtigung, Produkte hinzuzufügen.</p>';
|
||||||
|
echo '</div></section></main>';
|
||||||
|
include 'footer.php';
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
/* =======================
|
/* =======================
|
||||||
1) Kategorie aus GET
|
1) Kategorie aus GET
|
||||||
|
|||||||
@ -1,11 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
// productpage.php
|
// productpage.php
|
||||||
|
|
||||||
ini_set('display_errors', 1);
|
require_once __DIR__ . '/lib/bootstrap.php';
|
||||||
ini_set('display_startup_errors', 1);
|
|
||||||
error_reporting(E_ALL);
|
|
||||||
|
|
||||||
session_start();
|
|
||||||
|
|
||||||
// 1) DB-Verbindung (einmal)
|
// 1) DB-Verbindung (einmal)
|
||||||
$servername = "localhost";
|
$servername = "localhost";
|
||||||
|
|||||||
@ -1,11 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
// register.php
|
// register.php
|
||||||
|
|
||||||
ini_set('display_errors', 1);
|
require_once __DIR__ . '/lib/bootstrap.php';
|
||||||
ini_set('display_startup_errors', 1);
|
|
||||||
error_reporting(E_ALL);
|
|
||||||
|
|
||||||
session_start();
|
|
||||||
|
|
||||||
require_once __DIR__ . '/lib/strings.php';
|
require_once __DIR__ . '/lib/strings.php';
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// Produktion: keine PHP-Fehler im Browser ausgeben (Logs bleiben serverseitig)
|
require_once __DIR__ . '/lib/bootstrap.php';
|
||||||
ini_set('display_errors', 0);
|
|
||||||
ini_set('display_startup_errors', 0);
|
|
||||||
error_reporting(E_ALL);
|
|
||||||
|
|
||||||
session_start();
|
|
||||||
|
|
||||||
if (empty($_SESSION['user_id']))
|
if (empty($_SESSION['user_id']))
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,11 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
// wunschliste.php
|
// wunschliste.php
|
||||||
|
|
||||||
ini_set('display_errors', 1);
|
require_once __DIR__ . '/lib/bootstrap.php';
|
||||||
ini_set('display_startup_errors', 1);
|
|
||||||
error_reporting(E_ALL);
|
|
||||||
|
|
||||||
session_start();
|
|
||||||
|
|
||||||
// 1) DB-Verbindung (einmal)
|
// 1) DB-Verbindung (einmal)
|
||||||
$servername = "localhost";
|
$servername = "localhost";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user