Fehlerbehebung Passwort speichern

This commit is contained in:
2026-06-05 23:18:35 +02:00
parent 9010b68a39
commit b9dead1c8b
+37 -119
View File
@@ -5,58 +5,43 @@ require_once "UserManagerDAO.php";
class DatabaseUserManager implements UserManagerDAO { class DatabaseUserManager implements UserManagerDAO {
private static $instance = null; private static $instance = null;
public function __construct() public function __construct()
{ {
if (!file_exists(__DIR__ . '/../../db/users.db')) {
try { try {
$user = 'root'; $db = $this->getConnection();
$pw = null;
$dsn = 'sqlite:' . __DIR__ . '/../../db/users.db';
$db = new PDO($dsn, $user, $pw);
file_put_contents(
__DIR__ . '/../../db/test.txt',
'PDO funktioniert'
);
$db->exec(" $db->exec("
CREATE TABLE users ( CREATE TABLE IF NOT EXISTS users (
email TEXT PRIMARY KEY, email TEXT PRIMARY KEY,
vorname TEXT, vorname TEXT NOT NULL,
nachname TEXT, nachname TEXT NOT NULL,
password TEXT password TEXT NOT NULL
); );
"); ");
unset($db); unset($db);
} catch (PDOException $e) { } catch (PDOException $e) {
throw new RuntimeException($e->getMessage()); throw new RuntimeException("Benutzerdatenbank konnte nicht erstellt werden.");
} }
} }
}
/**
* Baut die Verbindung zur Datenbank auf.
*/
private function getConnection() private function getConnection()
{ {
try { try {
$user = 'root';
$pw = null;
$dsn = 'sqlite:' . __DIR__ . '/../../db/users.db'; $dsn = 'sqlite:' . __DIR__ . '/../../db/users.db';
return new PDO($dsn, $user, $pw); $db = new PDO($dsn, null, null);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
} catch (PDOException $e) { } catch (PDOException $e) {
throw new RuntimeException($e->getMessage()); throw new RuntimeException("Verbindung zur Benutzerdatenbank fehlgeschlagen.");
} }
} }
/**
* Singleton-Instanz zurückgeben.
*/
public static function getInstance() public static function getInstance()
{ {
if (self::$instance == null) { if (self::$instance == null) {
@@ -66,20 +51,14 @@ class DatabaseUserManager implements UserManagerDAO {
return self::$instance; return self::$instance;
} }
public function findUser($email) public function findUser($email)
{ {
try { try {
$db = $this->getConnection(); $db = $this->getConnection();
$sql = "SELECT * FROM users WHERE email = :email"; $sql = "SELECT * FROM users WHERE email = :email";
$command = $db->prepare($sql); $command = $db->prepare($sql);
if (!$command) {
throw new RuntimeException("Benutzer konnte nicht geladen werden.");
}
$command->execute([ $command->execute([
":email" => $email ":email" => $email
]); ]);
@@ -89,7 +68,7 @@ class DatabaseUserManager implements UserManagerDAO {
return $user ?: null; return $user ?: null;
} catch (PDOException $e) { } catch (PDOException $e) {
throw new RuntimeException($e->getMessage()); throw new RuntimeException("Benutzer konnte nicht geladen werden.");
} }
} }
@@ -97,90 +76,46 @@ class DatabaseUserManager implements UserManagerDAO {
{ {
try { try {
$db = $this->getConnection(); $db = $this->getConnection();
$db->beginTransaction(); $db->beginTransaction();
$existingUser = $this->findUser($email);
if ($existingUser !== null) {
$db->rollBack();
throw new InvalidArgumentException(
"Diese E-Mail-Adresse wird bereits verwendet."
);
}
$sql = " $sql = "
INSERT INTO users ( INSERT INTO users (email, vorname, nachname, password)
email, VALUES (:email, :vorname, :nachname, :password)
vorname,
nachname,
password
)
VALUES (
:email,
:vorname,
:nachname,
:password
)
"; ";
$command = $db->prepare($sql); $command = $db->prepare($sql);
if (!$command) { $command->execute([
$db->rollBack();
throw new RuntimeException(
"Benutzer konnte nicht gespeichert werden."
);
}
$success = $command->execute([
":email" => $email, ":email" => $email,
":vorname" => $vorname, ":vorname" => $vorname,
":nachname" => $nachname, ":nachname" => $nachname,
":password" => $password ":password" => $password
]); ]);
if (!$success) {
$db->rollBack();
throw new RuntimeException(
"Benutzer konnte nicht gespeichert werden."
);
}
$db->commit(); $db->commit();
} catch (PDOException $e) { } catch (PDOException $e) {
throw new RuntimeException($e->getMessage()); if (isset($db) && $db->inTransaction()) {
$db->rollBack();
}
if ($e->getCode() === "23000") {
throw new InvalidArgumentException("Diese E-Mail-Adresse wird bereits verwendet.");
}
throw new RuntimeException("Benutzer konnte nicht gespeichert werden.");
} }
} }
public function updateUser( public function updateUser($oldEmail, $newEmail, $vorname, $nachname, $password = null)
$oldEmail, {
$newEmail,
$vorname,
$nachname,
$password = null
) {
try { try {
$db = $this->getConnection(); $db = $this->getConnection();
if ($oldEmail !== $newEmail) {
$existingUser = $this->findUser($newEmail);
if ($existingUser !== null) {
throw new InvalidArgumentException(
"Diese E-Mail-Adresse wird bereits verwendet."
);
}
}
if (!empty($password)) { if (!empty($password)) {
$sql = " $sql = "
UPDATE users UPDATE users
SET SET email = :newEmail,
email = :newEmail,
vorname = :vorname, vorname = :vorname,
nachname = :nachname, nachname = :nachname,
password = :password password = :password
@@ -191,19 +126,13 @@ class DatabaseUserManager implements UserManagerDAO {
":newEmail" => $newEmail, ":newEmail" => $newEmail,
":vorname" => $vorname, ":vorname" => $vorname,
":nachname" => $nachname, ":nachname" => $nachname,
":password" => password_hash( ":password" => password_hash($password, PASSWORD_DEFAULT),
$password,
PASSWORD_DEFAULT
),
":oldEmail" => $oldEmail ":oldEmail" => $oldEmail
]; ];
} else { } else {
$sql = " $sql = "
UPDATE users UPDATE users
SET SET email = :newEmail,
email = :newEmail,
vorname = :vorname, vorname = :vorname,
nachname = :nachname nachname = :nachname
WHERE email = :oldEmail WHERE email = :oldEmail
@@ -218,19 +147,16 @@ class DatabaseUserManager implements UserManagerDAO {
} }
$command = $db->prepare($sql); $command = $db->prepare($sql);
if (!$command) {
throw new RuntimeException(
"Benutzer konnte nicht aktualisiert werden."
);
}
$command->execute($params); $command->execute($params);
return $command->rowCount() > 0; return $command->rowCount() > 0;
} catch (PDOException $e) { } catch (PDOException $e) {
throw new RuntimeException($e->getMessage()); if ($e->getCode() === "23000") {
throw new InvalidArgumentException("Diese E-Mail-Adresse wird bereits verwendet.");
}
throw new RuntimeException("Benutzer konnte nicht aktualisiert werden.");
} }
} }
@@ -240,15 +166,8 @@ class DatabaseUserManager implements UserManagerDAO {
$db = $this->getConnection(); $db = $this->getConnection();
$sql = "DELETE FROM users WHERE email = :email"; $sql = "DELETE FROM users WHERE email = :email";
$command = $db->prepare($sql); $command = $db->prepare($sql);
if (!$command) {
throw new RuntimeException(
"Benutzer konnte nicht gelöscht werden."
);
}
$command->execute([ $command->execute([
":email" => $email ":email" => $email
]); ]);
@@ -256,8 +175,7 @@ class DatabaseUserManager implements UserManagerDAO {
return $command->rowCount() > 0; return $command->rowCount() > 0;
} catch (PDOException $e) { } catch (PDOException $e) {
throw new RuntimeException($e->getMessage()); throw new RuntimeException("Benutzer konnte nicht gelöscht werden.");
} }
} }
} }