Implement database-based user management

This commit is contained in:
2026-06-05 22:34:12 +02:00
parent 11b55008ea
commit c23a10eb6e
3 changed files with 266 additions and 13 deletions
+237 -6
View File
@@ -1,26 +1,257 @@
<?php
require_once "UserManagerDAO.php";
class DatabaseUserManager implements UserManagerDAO {
private static $instance = null;
public function __construct()
{
if (!file_exists(__DIR__ . '/../../db/users.db')) {
try {
$user = 'root';
$pw = null;
$dsn = 'sqlite:' . __DIR__ . '/../../db/users.db';
$db = new PDO($dsn, $user, $pw);
$db->exec("
CREATE TABLE users (
email TEXT PRIMARY KEY,
vorname TEXT,
nachname TEXT,
password TEXT
);
");
unset($db);
} catch (PDOException $e) {
throw new RuntimeException($e->getMessage());
}
}
}
/**
* Baut die Verbindung zur Datenbank auf.
*/
private function getConnection()
{
try {
$user = 'root';
$pw = null;
$dsn = 'sqlite:' . __DIR__ . '/../../db/users.db';
return new PDO($dsn, $user, $pw);
} catch (PDOException $e) {
throw new RuntimeException($e->getMessage());
}
}
/**
* Singleton-Instanz zurückgeben.
*/
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new DatabaseUserManager();
}
return self::$instance;
}
public function findUser($email)
{
// TODO: Implement findUser() method.
try {
$db = $this->getConnection();
$sql = "SELECT * FROM users WHERE email = :email";
$command = $db->prepare($sql);
if (!$command) {
throw new RuntimeException("Benutzer konnte nicht geladen werden.");
}
$command->execute([
":email" => $email
]);
$user = $command->fetch(PDO::FETCH_ASSOC);
return $user ?: null;
} catch (PDOException $e) {
throw new RuntimeException($e->getMessage());
}
}
public function addUser($email, $vorname, $nachname, $password)
{
// TODO: Implement addUser() method.
try {
$db = $this->getConnection();
$db->beginTransaction();
$existingUser = $this->findUser($email);
if ($existingUser !== null) {
$db->rollBack();
throw new InvalidArgumentException(
"Diese E-Mail-Adresse wird bereits verwendet."
);
}
$sql = "
INSERT INTO users (
email,
vorname,
nachname,
password
)
VALUES (
:email,
:vorname,
:nachname,
:password
)
";
$command = $db->prepare($sql);
if (!$command) {
$db->rollBack();
throw new RuntimeException(
"Benutzer konnte nicht gespeichert werden."
);
}
$success = $command->execute([
":email" => $email,
":vorname" => $vorname,
":nachname" => $nachname,
":password" => $password
]);
if (!$success) {
$db->rollBack();
throw new RuntimeException(
"Benutzer konnte nicht gespeichert werden."
);
}
$db->commit();
} catch (PDOException $e) {
throw new RuntimeException($e->getMessage());
}
}
public function updateUser($oldEmail, $newEmail, $vorname, $nachname, $password = null)
{
// TODO: Implement updateUser() method.
public function updateUser(
$oldEmail,
$newEmail,
$vorname,
$nachname,
$password = null
) {
try {
$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)) {
$sql = "
UPDATE users
SET
email = :newEmail,
vorname = :vorname,
nachname = :nachname,
password = :password
WHERE email = :oldEmail
";
$params = [
":newEmail" => $newEmail,
":vorname" => $vorname,
":nachname" => $nachname,
":password" => password_hash(
$password,
PASSWORD_DEFAULT
),
":oldEmail" => $oldEmail
];
} else {
$sql = "
UPDATE users
SET
email = :newEmail,
vorname = :vorname,
nachname = :nachname
WHERE email = :oldEmail
";
$params = [
":newEmail" => $newEmail,
":vorname" => $vorname,
":nachname" => $nachname,
":oldEmail" => $oldEmail
];
}
$command = $db->prepare($sql);
if (!$command) {
throw new RuntimeException(
"Benutzer konnte nicht aktualisiert werden."
);
}
$command->execute($params);
return $command->rowCount() > 0;
} catch (PDOException $e) {
throw new RuntimeException($e->getMessage());
}
}
public function deleteUser($email)
{
// TODO: Implement deleteUser() method.
try {
$db = $this->getConnection();
$sql = "DELETE FROM users WHERE email = :email";
$command = $db->prepare($sql);
if (!$command) {
throw new RuntimeException(
"Benutzer konnte nicht gelöscht werden."
);
}
$command->execute([
":email" => $email
]);
return $command->rowCount() > 0;
} catch (PDOException $e) {
throw new RuntimeException($e->getMessage());
}
}
}