Email change on user

This commit is contained in:
2024-07-27 02:52:24 +02:00
parent 683b6020b7
commit 12f7176467
6 changed files with 86 additions and 25 deletions
+2 -1
View File
@@ -51,11 +51,12 @@ class ApiError extends Exception
]), 500);
}
public static function failedUpdate(array $fields)
public static function failedUpdate(array $fields, array $reasons)
{
return new ApiError(json_encode([
"code" => "FailedUpdate",
"fields" => $fields,
"reasons" => $reasons,
]), 500);
}
}
+20 -2
View File
@@ -118,6 +118,10 @@ class Post implements JsonSerializable
{
$db = Database::getInstance();
$db->beginTransaction();
$failed = [];
$reason = [];
if (!empty($content)) {
$content = substr(trim($content), 0, 250);
@@ -125,11 +129,25 @@ class Post implements JsonSerializable
$stmt->bindValue(":CON", nl2br(htmlspecialchars($content)));
$stmt->bindValue(":ID", $this->id);
try {
if (!$stmt->execute()) throw ApiError::failedUpdate(["content"]);
if (!$stmt->execute()) {
$failed = ["content"];
$reason = ["generic"];
}
} catch (Exception $e) {
throw ApiError::failedUpdate(["content"]);
$failed = ["content"];
if ($e->getCode() === "23000") {
$pdoErr = $stmt->errorInfo()[1];
$reason = ["SQL: $pdoErr"];
} else $reason = ["{$e->getCode()}"];
}
}
if ($failed !== null) {
$db->rollBack();
throw ApiError::failedUpdate($failed, $reason);
}
$db->commit();
return Post::getByID($this->id);
}
+44 -11
View File
@@ -270,19 +270,30 @@ class User implements JsonSerializable
return $stmt->execute();
}
public function update(?string $username, ?string $password, $image = null): User
public function update(?string $username, ?string $password, ?string $email): User
{
$db = Database::getInstance();
$db->beginTransaction();
$failed = [];
$reasons = [];
if (!empty($username)) {
$stmt = $db->prepare("UPDATE egb_benutzer SET benutzer = :USR WHERE id = :ID");
$stmt->bindValue(":USR", htmlspecialchars($username));
$stmt->bindValue(":ID", $this->id);
try {
if (!$stmt->execute()) array_push($failed, "username");
if (!$stmt->execute()) {
array_push($failed, "username");
array_push($reasons, "generic");
}
} catch (Exception $e) {
array_push($failed, "username");
if ($e->getCode() === "23000") {
$pdoErr = $stmt->errorInfo()[1];
if ($pdoErr === 1062) array_push($reasons, "Duplicate");
else array_push($reasons, "SQL: $pdoErr");
} else array_push($reasons, "{$e->getCode()}");
}
}
@@ -291,26 +302,48 @@ class User implements JsonSerializable
$stmt->bindValue(":PAS", password_hash($password, PASSWORD_DEFAULT));
$stmt->bindValue(":ID", $this->id);
try {
if (!$stmt->execute()) array_push($failed, "password");
if (!$stmt->execute()) {
array_push($failed, "password");
array_push($reasons, "generic");
}
} catch (Exception $e) {
array_push($failed, "password");
if ($e->getCode() === "23000") {
$pdoErr = $stmt->errorInfo()[1];
if ($pdoErr === 1062) array_push($reasons, "Duplicate");
else array_push($reasons, "SQL: $pdoErr");
} else array_push($reasons, "{$e->getCode()}");
}
}
if (!empty($image)) {
$destinationFilename = sprintf('%s.%s', uniqid(), $image->getExtension());
$image->move(Config::getStoragePath() . "profilbilder/$destinationFilename");
if (!empty($email)) {
// $destinationFilename = sprintf('%s.%s', uniqid(), $image->getExtension());
// $image->move(Config::getStoragePath() . "profilbilder/$destinationFilename");
$stmt = $db->prepare("UPDATE egb_benutzer SET image = :IMG WHERE id = :ID");
$stmt->bindValue(":IMG", $destinationFilename);
$stmt = $db->prepare("UPDATE egb_benutzer SET email = :EMA WHERE id = :ID");
$stmt->bindValue(":EMA", $email);
$stmt->bindValue(":ID", $this->id);
try {
if (!$stmt->execute()) array_push($failed, "image");
if (!$stmt->execute()) {
array_push($failed, "email");
array_push($reasons, "generic");
}
} catch (Exception $e) {
array_push($failed, "image");
array_push($failed, "email");
if ($e->getCode() === "23000") {
$pdoErr = $stmt->errorInfo()[1];
if ($pdoErr === 1062) array_push($reasons, "Duplicate");
else array_push($reasons, "SQL: $pdoErr");
} else array_push($reasons, "{$e->getCode()}");
}
}
if (count($failed) > 0) throw ApiError::failedUpdate($failed);
if (count($failed) > 0) {
$db->rollBack();
throw ApiError::failedUpdate($failed, $reasons);
}
$db->commit();
return User::getByID($this->id);
}