Vor und Nachname separat speichern sowie Profilbearbeitung #18
+48
-10
@@ -1,36 +1,74 @@
|
||||
<?php
|
||||
include_once 'php/controller/profile-controller.php';
|
||||
|
||||
$user = $user ?? null;
|
||||
$isEditMode = isset($_GET["edit"]) && $_GET["edit"] === "1";
|
||||
?>
|
||||
|
||||
<main class="form-page">
|
||||
<div class="flexbox">
|
||||
|
||||
<div class="container">
|
||||
<form>
|
||||
|
||||
<?php if (isset($error) && $error): ?>
|
||||
<p style="color:red;">
|
||||
<?php echo htmlspecialchars($error); ?>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
|
||||
<form method="post" action="index.php?pfad=profile">
|
||||
|
||||
<label class="input-label">Vorname</label>
|
||||
<input type="text"
|
||||
name="vorname"
|
||||
class="login-input"
|
||||
readonly
|
||||
value="<?php echo htmlspecialchars($user["vorname"] ?? ""); ?>">
|
||||
value="<?php echo htmlspecialchars($user["vorname"] ?? ""); ?>"
|
||||
<?php echo $isEditMode ? "" : "readonly"; ?>
|
||||
required>
|
||||
|
||||
<label class="input-label">Nachname</label>
|
||||
<input type="text"
|
||||
name="nachname"
|
||||
class="login-input"
|
||||
readonly
|
||||
value="<?php echo htmlspecialchars($user["nachname"] ?? ""); ?>">
|
||||
value="<?php echo htmlspecialchars($user["nachname"] ?? ""); ?>"
|
||||
<?php echo $isEditMode ? "" : "readonly"; ?>
|
||||
required>
|
||||
|
||||
<label class="input-label">Email-Adresse</label>
|
||||
<input type="email"
|
||||
name="email"
|
||||
class="login-input"
|
||||
readonly
|
||||
value="<?php echo htmlspecialchars($user["email"] ?? ""); ?>">
|
||||
value="<?php echo htmlspecialchars($user["email"] ?? ""); ?>"
|
||||
<?php echo $isEditMode ? "" : "readonly"; ?>
|
||||
required>
|
||||
|
||||
<label class="input-label">Passwort</label>
|
||||
<label class="input-label">
|
||||
<?php echo $isEditMode ? "Neues Passwort" : "Passwort"; ?>
|
||||
</label>
|
||||
<input type="password"
|
||||
name="password"
|
||||
class="login-input"
|
||||
readonly
|
||||
value="********">
|
||||
placeholder="<?php echo $isEditMode ? "Leer lassen, wenn es gleich bleiben soll" : "********"; ?>"
|
||||
<?php echo $isEditMode ? "" : "readonly"; ?>>
|
||||
|
||||
<br><br>
|
||||
|
||||
<?php if ($isEditMode): ?>
|
||||
<button type="submit"
|
||||
name="saveProfile"
|
||||
class="button">
|
||||
Speichern
|
||||
</button>
|
||||
|
||||
<a href="index.php?pfad=profile" class="button">
|
||||
Abbrechen
|
||||
</a>
|
||||
<?php else: ?>
|
||||
<a href="index.php?pfad=profile&edit=1" class="button">
|
||||
Bearbeiten
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
|
||||
</form>
|
||||
|
||||
<br>
|
||||
|
||||
@@ -9,8 +9,52 @@ if (!isset($_SESSION["user"])) {
|
||||
exit();
|
||||
}
|
||||
|
||||
$dao = new LocalUserDAO();
|
||||
|
caroline.slt marked this conversation as resolved
Outdated
|
||||
$error = null;
|
||||
|
||||
try {
|
||||
$dao = new LocalUserDAO();
|
||||
$user = $dao->findUser($_SESSION["user_email"] ?? "");
|
||||
|
||||
if (!$user) {
|
||||
$_SESSION = [];
|
||||
session_destroy();
|
||||
|
||||
header("Location: index.php?pfad=login");
|
||||
exit();
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["saveProfile"])) {
|
||||
$oldEmail = $_SESSION["user_email"];
|
||||
$newEmail = $_POST["email"] ?? "";
|
||||
$vorname = $_POST["vorname"] ?? "";
|
||||
$nachname = $_POST["nachname"] ?? "";
|
||||
$password = $_POST["password"] ?? "";
|
||||
|
||||
$existingUser = $dao->findUser($newEmail);
|
||||
|
||||
if ($existingUser && $newEmail !== $oldEmail) {
|
||||
$error = "Diese E-Mail-Adresse wird bereits verwendet.";
|
||||
} else {
|
||||
$updated = $dao->updateUser(
|
||||
|
caroline.slt marked this conversation as resolved
Outdated
niklas.ortmann
commented
Eingabevalidierung fehlt noch. Eingabevalidierung fehlt noch.
Was darf ein Nutzer überhaupt alles eingeben? Z.B. Buchstaben a-z, A-Z, Sonderzeichen?
(Wenn du das zu dieser Abgabe nicht schaffst, dann bitte ein TODO setzen, dass das später noch erfolgt (wichtig) und einen Hinweis in der ReadMe hinterlassen)!
|
||||
$oldEmail,
|
||||
$newEmail,
|
||||
$vorname,
|
||||
$nachname,
|
||||
$password
|
||||
);
|
||||
|
||||
if ($updated) {
|
||||
$_SESSION["user"] = $vorname . " " . $nachname;
|
||||
$_SESSION["user_email"] = $newEmail;
|
||||
|
||||
header("Location: index.php?pfad=profile");
|
||||
exit();
|
||||
} else {
|
||||
$error = "Die Daten konnten nicht gespeichert werden.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$user = $dao->findUser($_SESSION["user_email"] ?? "");
|
||||
|
||||
$articleManager = ArticleManager::getInstance();
|
||||
@@ -23,12 +67,4 @@ try {
|
||||
} catch (Exception $e) {
|
||||
$_SESSION["message"] = "internal_error";
|
||||
exit();
|
||||
}
|
||||
|
||||
if (!$user) {
|
||||
$_SESSION = [];
|
||||
session_destroy();
|
||||
|
||||
header("Location: index.php?pfad=login");
|
||||
exit();
|
||||
}
|
||||
@@ -56,6 +56,29 @@ class LocalUserDAO implements UserDAOInterface {
|
||||
$this->saveUsers($users);
|
||||
}
|
||||
|
||||
public function updateUser($oldEmail, $newEmail, $vorname, $nachname, $password = null) {
|
||||
$users = $this->loadUsers();
|
||||
|
||||
foreach ($users as $i => $user) {
|
||||
if ($user["email"] === $oldEmail) {
|
||||
$users[$i]["email"] = $newEmail;
|
||||
$users[$i]["vorname"] = $vorname;
|
||||
$users[$i]["nachname"] = $nachname;
|
||||
|
||||
unset($users[$i]["username"]);
|
||||
|
||||
if (!empty($password)) {
|
||||
$users[$i]["password"] = password_hash($password, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
$this->saveUsers($users);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function deleteUser($email) {
|
||||
$users = $this->loadUsers();
|
||||
|
||||
|
||||
@@ -27,6 +27,18 @@ interface UserDAOInterface {
|
||||
*/
|
||||
public function addUser($email, $vorname, $nachname, $password);
|
||||
|
||||
/**
|
||||
* Aktualisiert einen bestehenden Benutzer.
|
||||
*
|
||||
* @param string $oldEmail Alte E-Mail-Adresse
|
||||
* @param string $newEmail Neue E-Mail-Adresse
|
||||
* @param string $vorname Neuer Vorname
|
||||
* @param string $nachname Neuer Nachname
|
||||
* @param string|null $password Neues Passwort oder null
|
||||
* @return bool true, wenn der Benutzer aktualisiert wurde, sonst false
|
||||
|
caroline.slt marked this conversation as resolved
Outdated
niklas.ortmann
commented
Die möglichen Exceptions müssen angeben werden. Die möglichen Exceptions müssen angeben werden.
(Können wir auch noch nachreichen, aber dann würde ich hier zumindest ein TODO schreiben, dass das noch gemacht werden muss).
|
||||
*/
|
||||
public function updateUser($oldEmail, $newEmail, $vorname, $nachname, $password = null);
|
||||
|
||||
/**
|
||||
* Löscht einen Benutzer anhand seiner E-Mail-Adresse.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user
Das sollte zwingend im try-Block bleiben. Wenn beim Instanziieren eines LocalUserDAO ein fehler auftritt, dann wird dieser nicht abgefangen!