263 lines
6.6 KiB
PHP
263 lines
6.6 KiB
PHP
<?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);
|
|
|
|
file_put_contents(
|
|
__DIR__ . '/../../db/test.txt',
|
|
'PDO funktioniert'
|
|
);
|
|
|
|
$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)
|
|
{
|
|
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)
|
|
{
|
|
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
|
|
) {
|
|
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)
|
|
{
|
|
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());
|
|
}
|
|
}
|
|
|
|
} |