Email change on user
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user