DatabaseUserManager #25
@@ -5,58 +5,43 @@ require_once "UserManagerDAO.php";
|
||||
class DatabaseUserManager implements UserManagerDAO {
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
try {
|
||||
$db = $this->getConnection();
|
||||
|
||||
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 (
|
||||
$db->exec("
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
email TEXT PRIMARY KEY,
|
||||
vorname TEXT,
|
||||
nachname TEXT,
|
||||
password TEXT
|
||||
vorname TEXT NOT NULL,
|
||||
nachname TEXT NOT NULL,
|
||||
password TEXT NOT NULL
|
||||
);
|
||||
");
|
||||
|
||||
unset($db);
|
||||
unset($db);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
throw new RuntimeException($e->getMessage());
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new RuntimeException("Benutzerdatenbank konnte nicht erstellt werden.");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Baut die Verbindung zur Datenbank auf.
|
||||
*/
|
||||
|
||||
|
caroline.slt marked this conversation as resolved
|
||||
private function getConnection()
|
||||
{
|
||||
try {
|
||||
$user = 'root';
|
||||
$pw = null;
|
||||
$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) {
|
||||
throw new RuntimeException($e->getMessage());
|
||||
throw new RuntimeException("Verbindung zur Benutzerdatenbank fehlgeschlagen.");
|
||||
}
|
||||
}
|
||||
|
||||
|
caroline.slt marked this conversation as resolved
Outdated
niklas.ortmann
commented
Kommentar ergänzen Kommentar ergänzen
|
||||
/**
|
||||
* Singleton-Instanz zurückgeben.
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance == null) {
|
||||
@@ -66,20 +51,14 @@ class DatabaseUserManager implements UserManagerDAO {
|
||||
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
|
||||
]);
|
||||
@@ -89,7 +68,7 @@ class DatabaseUserManager implements UserManagerDAO {
|
||||
return $user ?: null;
|
||||
|
||||
} 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 {
|
||||
$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
|
||||
)
|
||||
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([
|
||||
$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());
|
||||
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(
|
||||
$oldEmail,
|
||||
$newEmail,
|
||||
$vorname,
|
||||
$nachname,
|
||||
$password = null
|
||||
) {
|
||||
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,
|
||||
SET email = :newEmail,
|
||||
vorname = :vorname,
|
||||
nachname = :nachname,
|
||||
password = :password
|
||||
@@ -191,19 +126,13 @@ class DatabaseUserManager implements UserManagerDAO {
|
||||
":newEmail" => $newEmail,
|
||||
":vorname" => $vorname,
|
||||
":nachname" => $nachname,
|
||||
":password" => password_hash(
|
||||
$password,
|
||||
PASSWORD_DEFAULT
|
||||
),
|
||||
":password" => password_hash($password, PASSWORD_DEFAULT),
|
||||
":oldEmail" => $oldEmail
|
||||
];
|
||||
|
||||
} else {
|
||||
|
||||
$sql = "
|
||||
UPDATE users
|
||||
SET
|
||||
email = :newEmail,
|
||||
SET email = :newEmail,
|
||||
vorname = :vorname,
|
||||
nachname = :nachname
|
||||
WHERE email = :oldEmail
|
||||
@@ -218,19 +147,16 @@ class DatabaseUserManager implements UserManagerDAO {
|
||||
}
|
||||
|
||||
$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());
|
||||
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();
|
||||
|
||||
$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
|
||||
]);
|
||||
@@ -256,8 +175,7 @@ class DatabaseUserManager implements UserManagerDAO {
|
||||
return $command->rowCount() > 0;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
throw new RuntimeException($e->getMessage());
|
||||
throw new RuntimeException("Benutzer konnte nicht gelöscht werden.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user
Kommentare ergänzen