forked from thomas/Newwebshop
100 lines
3.0 KiB
PHP
100 lines
3.0 KiB
PHP
<?php
|
|
/**
|
|
* Copyright seit 2024 Webshop System
|
|
*
|
|
* Admin-Login-Controller für das Webshop-System
|
|
*
|
|
* @author Webshop System
|
|
* @license GPL v3
|
|
*/
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use Doctrine\DBAL\DriverManager;
|
|
use Doctrine\DBAL\Exception;
|
|
|
|
class AdminLoginController
|
|
{
|
|
public function index()
|
|
{
|
|
// Login-Formular anzeigen
|
|
$this->render('admin/login.html.twig', [
|
|
'title' => 'Webshop Admin - Login'
|
|
]);
|
|
}
|
|
|
|
public function login()
|
|
{
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: /admin/login');
|
|
exit;
|
|
}
|
|
|
|
$email = $_POST['email'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($email) || empty($password)) {
|
|
$this->render('admin/login.html.twig', [
|
|
'error' => 'Bitte füllen Sie alle Felder aus.',
|
|
'title' => 'Webshop Admin - Login'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// DB-Verbindung herstellen
|
|
$connectionParams = [
|
|
'dbname' => getenv('DB_DATABASE') ?: 'freeshop',
|
|
'user' => getenv('DB_USERNAME') ?: 'freeshop_user',
|
|
'password' => getenv('DB_PASSWORD') ?: 'freeshop_password',
|
|
'host' => getenv('DB_HOST') ?: 'db',
|
|
'driver' => 'pdo_mysql',
|
|
'port' => getenv('DB_PORT') ?: 3306,
|
|
'charset' => 'utf8mb4',
|
|
];
|
|
|
|
try {
|
|
$conn = DriverManager::getConnection($connectionParams);
|
|
|
|
// User in DB suchen
|
|
$stmt = $conn->prepare('SELECT * FROM ws_user WHERE email = ? AND is_admin = 1');
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetchAssociative();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
// Login erfolgreich - Session starten
|
|
session_start();
|
|
$_SESSION['admin_user_id'] = $user['id'];
|
|
$_SESSION['admin_user_email'] = $user['email'];
|
|
$_SESSION['admin_user_name'] = $user['firstname'] . ' ' . $user['lastname'];
|
|
|
|
header('Location: /admin/dashboard');
|
|
exit;
|
|
} else {
|
|
$this->render('admin/login.html.twig', [
|
|
'error' => 'Ungültige E-Mail oder Passwort.',
|
|
'title' => 'Webshop Admin - Login'
|
|
]);
|
|
}
|
|
} catch (Exception $e) {
|
|
$this->render('admin/login.html.twig', [
|
|
'error' => 'Datenbankfehler: ' . $e->getMessage(),
|
|
'title' => 'Webshop Admin - Login'
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function logout()
|
|
{
|
|
session_start();
|
|
session_destroy();
|
|
header('Location: /admin/login');
|
|
exit;
|
|
}
|
|
|
|
protected function render($template, $data = [])
|
|
{
|
|
// Einfache Template-Engine (später durch Twig ersetzen)
|
|
extract($data);
|
|
include __DIR__ . '/../../templates/' . $template;
|
|
}
|
|
}
|