Email change on user
This commit is contained in:
parent
683b6020b7
commit
12f7176467
@ -42,12 +42,12 @@ class Users extends Api
|
|||||||
// Fetch all inputs.
|
// Fetch all inputs.
|
||||||
$username = Input::patch("username");
|
$username = Input::patch("username");
|
||||||
$password = Input::patch("password");
|
$password = Input::patch("password");
|
||||||
$image = Input::file("image");
|
$email = Input::patch("email");
|
||||||
|
|
||||||
// Try and update user.
|
// Try and update user.
|
||||||
// Throw errors according to situation.
|
// Throw errors according to situation.
|
||||||
try {
|
try {
|
||||||
Response::json(User::getByID($id)->update($username, $password, $image));
|
Response::json(User::getByID($id)->update($username, $password, $email));
|
||||||
} catch (Exception $err) {
|
} catch (Exception $err) {
|
||||||
switch ($err->getMessage()) {
|
switch ($err->getMessage()) {
|
||||||
case "NotFound":
|
case "NotFound":
|
||||||
@ -65,12 +65,12 @@ class Users extends Api
|
|||||||
$token = Request::token();
|
$token = Request::token();
|
||||||
$username = Input::patch("username");
|
$username = Input::patch("username");
|
||||||
$password = Input::patch("password");
|
$password = Input::patch("password");
|
||||||
$image = Input::file("image");
|
$email = Input::patch("email");
|
||||||
|
|
||||||
// Try and update user.
|
// Try and update user.
|
||||||
// Throw errors according to situation.
|
// Throw errors according to situation.
|
||||||
try {
|
try {
|
||||||
Response::json(User::getByToken($token)->update($username, $password, $image));
|
Response::json(User::getByToken($token)->update($username, $password, $email));
|
||||||
} catch (Exception $err) {
|
} catch (Exception $err) {
|
||||||
switch ($err->getMessage()) {
|
switch ($err->getMessage()) {
|
||||||
case "NotFound":
|
case "NotFound":
|
||||||
|
|||||||
@ -254,7 +254,12 @@ paths:
|
|||||||
$ref: "#/components/schemas/FailedUpdateResponse"
|
$ref: "#/components/schemas/FailedUpdateResponse"
|
||||||
examples:
|
examples:
|
||||||
Failed:
|
Failed:
|
||||||
value: { "code": "FailedUpdate", "fields": ["content"] }
|
value:
|
||||||
|
{
|
||||||
|
"code": "FailedUpdate",
|
||||||
|
"fields": ["content"],
|
||||||
|
"reasons": ["string"],
|
||||||
|
}
|
||||||
tags:
|
tags:
|
||||||
- Post
|
- Post
|
||||||
delete:
|
delete:
|
||||||
@ -481,7 +486,8 @@ paths:
|
|||||||
value:
|
value:
|
||||||
{
|
{
|
||||||
"code": "FailedUpdate",
|
"code": "FailedUpdate",
|
||||||
"fields": ["username", "password", "image"],
|
"fields": ["username", "password", "email"],
|
||||||
|
"reasons": ["string", "string", "string"],
|
||||||
}
|
}
|
||||||
tags:
|
tags:
|
||||||
- User
|
- User
|
||||||
@ -572,6 +578,10 @@ components:
|
|||||||
type: array
|
type: array
|
||||||
items:
|
items:
|
||||||
type: string
|
type: string
|
||||||
|
reasons:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
ErrorResponse:
|
ErrorResponse:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
@ -629,9 +639,8 @@ components:
|
|||||||
type: string
|
type: string
|
||||||
password:
|
password:
|
||||||
type: string
|
type: string
|
||||||
image:
|
email:
|
||||||
type: string
|
type: string
|
||||||
format: binary
|
|
||||||
RegisterRequest:
|
RegisterRequest:
|
||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -51,11 +51,12 @@ class ApiError extends Exception
|
|||||||
]), 500);
|
]), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function failedUpdate(array $fields)
|
public static function failedUpdate(array $fields, array $reasons)
|
||||||
{
|
{
|
||||||
return new ApiError(json_encode([
|
return new ApiError(json_encode([
|
||||||
"code" => "FailedUpdate",
|
"code" => "FailedUpdate",
|
||||||
"fields" => $fields,
|
"fields" => $fields,
|
||||||
|
"reasons" => $reasons,
|
||||||
]), 500);
|
]), 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -118,6 +118,10 @@ class Post implements JsonSerializable
|
|||||||
{
|
{
|
||||||
$db = Database::getInstance();
|
$db = Database::getInstance();
|
||||||
|
|
||||||
|
$db->beginTransaction();
|
||||||
|
|
||||||
|
$failed = [];
|
||||||
|
$reason = [];
|
||||||
if (!empty($content)) {
|
if (!empty($content)) {
|
||||||
$content = substr(trim($content), 0, 250);
|
$content = substr(trim($content), 0, 250);
|
||||||
|
|
||||||
@ -125,11 +129,25 @@ class Post implements JsonSerializable
|
|||||||
$stmt->bindValue(":CON", nl2br(htmlspecialchars($content)));
|
$stmt->bindValue(":CON", nl2br(htmlspecialchars($content)));
|
||||||
$stmt->bindValue(":ID", $this->id);
|
$stmt->bindValue(":ID", $this->id);
|
||||||
try {
|
try {
|
||||||
if (!$stmt->execute()) throw ApiError::failedUpdate(["content"]);
|
if (!$stmt->execute()) {
|
||||||
|
$failed = ["content"];
|
||||||
|
$reason = ["generic"];
|
||||||
|
}
|
||||||
} catch (Exception $e) {
|
} 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);
|
return Post::getByID($this->id);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -270,19 +270,30 @@ class User implements JsonSerializable
|
|||||||
return $stmt->execute();
|
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 = Database::getInstance();
|
||||||
|
|
||||||
|
$db->beginTransaction();
|
||||||
|
|
||||||
$failed = [];
|
$failed = [];
|
||||||
|
$reasons = [];
|
||||||
if (!empty($username)) {
|
if (!empty($username)) {
|
||||||
$stmt = $db->prepare("UPDATE egb_benutzer SET benutzer = :USR WHERE id = :ID");
|
$stmt = $db->prepare("UPDATE egb_benutzer SET benutzer = :USR WHERE id = :ID");
|
||||||
$stmt->bindValue(":USR", htmlspecialchars($username));
|
$stmt->bindValue(":USR", htmlspecialchars($username));
|
||||||
$stmt->bindValue(":ID", $this->id);
|
$stmt->bindValue(":ID", $this->id);
|
||||||
try {
|
try {
|
||||||
if (!$stmt->execute()) array_push($failed, "username");
|
if (!$stmt->execute()) {
|
||||||
|
array_push($failed, "username");
|
||||||
|
array_push($reasons, "generic");
|
||||||
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
array_push($failed, "username");
|
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(":PAS", password_hash($password, PASSWORD_DEFAULT));
|
||||||
$stmt->bindValue(":ID", $this->id);
|
$stmt->bindValue(":ID", $this->id);
|
||||||
try {
|
try {
|
||||||
if (!$stmt->execute()) array_push($failed, "password");
|
if (!$stmt->execute()) {
|
||||||
|
array_push($failed, "password");
|
||||||
|
array_push($reasons, "generic");
|
||||||
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
array_push($failed, "password");
|
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)) {
|
if (!empty($email)) {
|
||||||
$destinationFilename = sprintf('%s.%s', uniqid(), $image->getExtension());
|
// $destinationFilename = sprintf('%s.%s', uniqid(), $image->getExtension());
|
||||||
$image->move(Config::getStoragePath() . "profilbilder/$destinationFilename");
|
// $image->move(Config::getStoragePath() . "profilbilder/$destinationFilename");
|
||||||
|
|
||||||
$stmt = $db->prepare("UPDATE egb_benutzer SET image = :IMG WHERE id = :ID");
|
$stmt = $db->prepare("UPDATE egb_benutzer SET email = :EMA WHERE id = :ID");
|
||||||
$stmt->bindValue(":IMG", $destinationFilename);
|
$stmt->bindValue(":EMA", $email);
|
||||||
$stmt->bindValue(":ID", $this->id);
|
$stmt->bindValue(":ID", $this->id);
|
||||||
try {
|
try {
|
||||||
if (!$stmt->execute()) array_push($failed, "image");
|
if (!$stmt->execute()) {
|
||||||
|
array_push($failed, "email");
|
||||||
|
array_push($reasons, "generic");
|
||||||
|
}
|
||||||
} catch (Exception $e) {
|
} 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);
|
return User::getByID($this->id);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user