auth.php aktualisiert

This commit is contained in:
M_Viper 2025-03-13 20:58:23 +00:00
parent b193d1c5ce
commit 2368c71da0

140
auth.php
View File

@ -1,70 +1,70 @@
<?php <?php
session_start(); session_start();
// Benutzerdatenbank // Benutzerdatenbank
$valid_users = array( $valid_users = array(
'admin' => array( 'admin' => array(
'password' => 'Jennifer@1996+', 'password' => 'password',
'access_level' => 'all' 'access_level' => 'all'
), ),
'Lager' => array( 'Lager' => array(
'password' => 'MediaMarkt', 'password' => 'Logistik',
'access_level' => 'limited' 'access_level' => 'limited'
) )
); );
function authenticate($username, $password) { function authenticate($username, $password) {
global $valid_users; global $valid_users;
if (array_key_exists($username, $valid_users) && $valid_users[$username]['password'] === $password) { if (array_key_exists($username, $valid_users) && $valid_users[$username]['password'] === $password) {
$_SESSION['authenticated'] = true; $_SESSION['authenticated'] = true;
$_SESSION['username'] = $username; $_SESSION['username'] = $username;
$_SESSION['access_level'] = $valid_users[$username]['access_level']; $_SESSION['access_level'] = $valid_users[$username]['access_level'];
$_SESSION['last_activity'] = time(); // Zeitstempel für die letzte Aktion setzen $_SESSION['last_activity'] = time(); // Zeitstempel für die letzte Aktion setzen
// Weiterleitung basierend auf der Benutzerrolle // Weiterleitung basierend auf der Benutzerrolle
if ($_SESSION['access_level'] === 'all') { if ($_SESSION['access_level'] === 'all') {
header('Location: public/admin.php'); header('Location: public/admin.php');
exit; exit;
} elseif ($_SESSION['access_level'] === 'limited') { } elseif ($_SESSION['access_level'] === 'limited') {
header('Location: public/search.php'); header('Location: public/search.php');
exit; exit;
} }
return true; return true;
} else { } else {
return false; return false;
} }
} }
function is_authenticated() { function is_authenticated() {
return isset($_SESSION['authenticated']) && $_SESSION['authenticated'] === true; return isset($_SESSION['authenticated']) && $_SESSION['authenticated'] === true;
} }
function require_login() { function require_login() {
if (!is_authenticated()) { if (!is_authenticated()) {
header('Location: public/search.php'); header('Location: public/search.php');
exit; exit;
} }
} }
function require_admin_login() { function require_admin_login() {
require_login(); require_login();
if ($_SESSION['access_level'] !== 'all') { if ($_SESSION['access_level'] !== 'all') {
header('Location: unauthorized.php'); header('Location: unauthorized.php');
exit; exit;
} }
} }
function require_limited_access_login() { function require_limited_access_login() {
require_login(); require_login();
if ($_SESSION['access_level'] !== 'limited') { if ($_SESSION['access_level'] !== 'limited') {
header('Location: unauthorized.php'); header('Location: unauthorized.php');
exit; exit;
} }
} }
function get_authenticated_username() { function get_authenticated_username() {
return $_SESSION['username'] ?? null; return $_SESSION['username'] ?? null;
} }
?> ?>