Email change on user
This commit is contained in:
parent
683b6020b7
commit
12f7176467
@ -42,12 +42,12 @@ class Users extends Api
|
||||
// Fetch all inputs.
|
||||
$username = Input::patch("username");
|
||||
$password = Input::patch("password");
|
||||
$image = Input::file("image");
|
||||
$email = Input::patch("email");
|
||||
|
||||
// Try and update user.
|
||||
// Throw errors according to situation.
|
||||
try {
|
||||
Response::json(User::getByID($id)->update($username, $password, $image));
|
||||
Response::json(User::getByID($id)->update($username, $password, $email));
|
||||
} catch (Exception $err) {
|
||||
switch ($err->getMessage()) {
|
||||
case "NotFound":
|
||||
@ -65,12 +65,12 @@ class Users extends Api
|
||||
$token = Request::token();
|
||||
$username = Input::patch("username");
|
||||
$password = Input::patch("password");
|
||||
$image = Input::file("image");
|
||||
$email = Input::patch("email");
|
||||
|
||||
// Try and update user.
|
||||
// Throw errors according to situation.
|
||||
try {
|
||||
Response::json(User::getByToken($token)->update($username, $password, $image));
|
||||
Response::json(User::getByToken($token)->update($username, $password, $email));
|
||||
} catch (Exception $err) {
|
||||
switch ($err->getMessage()) {
|
||||
case "NotFound":
|
||||
|
||||
@ -254,7 +254,12 @@ paths:
|
||||
$ref: "#/components/schemas/FailedUpdateResponse"
|
||||
examples:
|
||||
Failed:
|
||||
value: { "code": "FailedUpdate", "fields": ["content"] }
|
||||
value:
|
||||
{
|
||||
"code": "FailedUpdate",
|
||||
"fields": ["content"],
|
||||
"reasons": ["string"],
|
||||
}
|
||||
tags:
|
||||
- Post
|
||||
delete:
|
||||
@ -481,7 +486,8 @@ paths:
|
||||
value:
|
||||
{
|
||||
"code": "FailedUpdate",
|
||||
"fields": ["username", "password", "image"],
|
||||
"fields": ["username", "password", "email"],
|
||||
"reasons": ["string", "string", "string"],
|
||||
}
|
||||
tags:
|
||||
- User
|
||||
@ -572,6 +578,10 @@ components:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
reasons:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
ErrorResponse:
|
||||
type: object
|
||||
properties:
|
||||
@ -629,9 +639,8 @@ components:
|
||||
type: string
|
||||
password:
|
||||
type: string
|
||||
image:
|
||||
email:
|
||||
type: string
|
||||
format: binary
|
||||
RegisterRequest:
|
||||
type: object
|
||||
required:
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user