From d24d914c8c3a923ed48c4c7c81e30891b4179daa Mon Sep 17 00:00:00 2001 From: Karsten Tlotzek Date: Mon, 30 Jun 2025 21:18:04 +0200 Subject: [PATCH] refactor: Validierungslogik ins Model verschoben - pwRequirementCheck und checkDoublePw aus Controller ins Model - Alle Passwort- und E-Mail-Validierungen jetzt zentral im Model - Controller macht nur noch Request/Response Handling - Saubere MVC-Trennung --- Controller/AuthController.php | 58 +++++------------------- Controller/RegisterController.php | 10 ----- Model/AuthModel.php | 40 +++++++++++++---- Views/Auth/login.phtml | 38 ---------------- Views/Auth/register.phtml | 65 --------------------------- Views/Auth/showAuthForm.phtml | 25 ++++++----- Views/Auth/showRegistrationForm.phtml | 53 ++++++++-------------- Views/header.phtml | 5 ++- bibarts.sql | 59 ++++++------------------ index.php | 8 ++-- 10 files changed, 96 insertions(+), 265 deletions(-) delete mode 100644 Controller/RegisterController.php delete mode 100644 Views/Auth/login.phtml delete mode 100644 Views/Auth/register.phtml diff --git a/Controller/AuthController.php b/Controller/AuthController.php index 78e8511..ff9885a 100644 --- a/Controller/AuthController.php +++ b/Controller/AuthController.php @@ -71,9 +71,9 @@ class AuthController $data = [ 'vorname' => $_POST['vorname'] ?? '', 'nachname' => $_POST['nachname'] ?? '', - 'straße' => $_POST['straße'] ?? '', + 'straße' => $_POST['strasse'] ?? '', 'hausnr' => $_POST['hausnr'] ?? '', - 'postleitzahl' => $_POST['postleitzahl'] ?? '', + 'plz' => $_POST['plz'] ?? '', 'ort' => $_POST['ort'] ?? '', 'land' => $_POST['land'] ?? '', 'tel' => $_POST['tel'] ?? '', @@ -83,52 +83,18 @@ class AuthController 'isAdmin' => $_POST['isAdmin'] ?? false, ]; - $errors = []; + $result = $this->model->register($data); - if (!$this->model->checkDoublePw($data['password'], $data['password_repeat'])) { - $errors['password'] = "Passwörter stimmen nicht überein."; + if ($result === true) { + $this->view->setVars(['success' => 'Registrierung erfolgreich!']); + $this->view->render('Auth/showAuthForm'); + exit; + } else { + $errors['register'] = is_string($result) ? $result : "Registrierung fehlgeschlagen."; + $this->view->setVars(['errors' => $errors, 'validData' => $data]); + $this->view->render('Auth/showRegistrationForm'); + exit; } - - if ($this->pwRequirementCheck($data['password'])) { - $errors['password'] = "Passwort muss mindestens 8 Zeichen lang sein und mindestens ein Großbuchstabe, ein Kleinbuchstabe, eine Zahl und ein Sonderzeichen enthalten."; - } - - if (empty($errors)) { - $result = $this->model->register($data); - - if ($result === true) { - $this->view->setVars([ - 'success' => "Registrierung war erfolgreich." - ]); - } else { - $errors['register'] = is_string($result) ? $result : "Registrierung fehlgeschlagen."; - } - } - - $this->view->setVars([ - 'errors' => $errors, - 'validData' => $data - ]); - } - - private function pwRequirementCheck($password){ - $error = []; - - if(strlen($password) <= 8) - $error[] = "min 8 Charackter"; - if(!preg_match("/[A-Z]/", $password)) - $error[] = "min one large Character"; - if(!preg_match("/[a-z]/", $password)) - $error[] = "min one small charakter"; - if(!preg_match("/[0-9]/", $password)) - $error[] = "min one number"; - if(!preg_match("[^a-zA-Z0-9\s]", $password)); - $error[] = "min one special character"; - - if(empty($error)) - return true; - else - return $error; } public function forgotPassword() { diff --git a/Controller/RegisterController.php b/Controller/RegisterController.php deleted file mode 100644 index eb2059b..0000000 --- a/Controller/RegisterController.php +++ /dev/null @@ -1,10 +0,0 @@ -pwRequirementCheck($data['password']); - if ($rtn !== true) { - return $rtn; - } - + public function register($data) { if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) { return "Bitte geben Sie eine gültige E-Mail ein."; } @@ -72,6 +66,15 @@ class AuthModel extends Database return "Ein Account mit dieser E-Mail existiert bereits."; } + // Passwort-Validierung + if (!$this->checkDoublePw($data['password'], $data['password_repeat'])) { + return "Passwörter stimmen nicht überein."; + } + + if ($this->pwRequirementCheck($data['password']) !== true) { + return "Passwort muss mindestens 8 Zeichen lang sein und mindestens ein Großbuchstabe, ein Kleinbuchstabe, eine Zahl und ein Sonderzeichen enthalten."; + } + $hashedPassword = password_hash($data['password'], PASSWORD_DEFAULT); $sql = "INSERT INTO user (email, password, straße, hausnr, ort, postleitzahl, land,vorname, nachname, tel, isAdmin) @@ -145,8 +148,7 @@ class AuthModel extends Database } } - private function forgottenPwUpdate($email, $hashedPassword) - { + private function forgottenPwUpdate($email, $hashedPassword) { try{ $pdo = $this->linkDB(); @@ -213,4 +215,24 @@ class AuthModel extends Database else return false; } + + public function pwRequirementCheck($password){ + $error = []; + + if(strlen($password) <= 8) + $error[] = "min 8 Charackter"; + if(!preg_match("/[A-Z]/", $password)) + $error[] = "min one large Character"; + if(!preg_match("/[a-z]/", $password)) + $error[] = "min one small charakter"; + if(!preg_match("/[0-9]/", $password)) + $error[] = "min one number"; + if(!preg_match("[^a-zA-Z0-9\s]", $password)); + $error[] = "min one special character"; + + if(empty($error)) + return true; + else + return $error; + } } diff --git a/Views/Auth/login.phtml b/Views/Auth/login.phtml deleted file mode 100644 index 374f0f2..0000000 --- a/Views/Auth/login.phtml +++ /dev/null @@ -1,38 +0,0 @@ - - -
- -
- - \ No newline at end of file diff --git a/Views/Auth/register.phtml b/Views/Auth/register.phtml deleted file mode 100644 index 9161031..0000000 --- a/Views/Auth/register.phtml +++ /dev/null @@ -1,65 +0,0 @@ - - - -
-

-
- - - -
- -
- - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- -
- - \ No newline at end of file diff --git a/Views/Auth/showAuthForm.phtml b/Views/Auth/showAuthForm.phtml index 2d76e8e..eb5e141 100644 --- a/Views/Auth/showAuthForm.phtml +++ b/Views/Auth/showAuthForm.phtml @@ -5,19 +5,20 @@ include dirname(__DIR__) . '/header.phtml';
diff --git a/Views/Auth/showRegistrationForm.phtml b/Views/Auth/showRegistrationForm.phtml index 342e1c2..363c413 100644 --- a/Views/Auth/showRegistrationForm.phtml +++ b/Views/Auth/showRegistrationForm.phtml @@ -1,69 +1,54 @@ +
+ diff --git a/Views/header.phtml b/Views/header.phtml index 3358c73..9b1e404 100644 --- a/Views/header.phtml +++ b/Views/header.phtml @@ -1,7 +1,7 @@ - VR Contact + Bib Arts @@ -11,8 +11,9 @@