Most User endpoints

This commit is contained in:
2024-07-21 20:22:39 +02:00
parent b3c5841e36
commit 4b89a7e9ca
11 changed files with 102 additions and 28 deletions
+19 -10
View File
@@ -4,6 +4,7 @@ namespace Khofmann\Models\User;
use Exception;
use PDO;
use DateTime;
use Khofmann\Database\Database;
use Config\Config;
use JsonSerializable;
@@ -16,8 +17,9 @@ class User implements JsonSerializable
private string $email;
private ?string $image;
private bool $isAdmin;
private DateTime $memberSince;
protected function __construct(int $id, string $username, int $status, string $email, string $image = null, bool $isAdmin = false)
protected function __construct(int $id, string $username, int $status, string $email, string $timestamp, string $image, bool $isAdmin)
{
$this->id = $id;
$this->username = $username;
@@ -25,6 +27,7 @@ class User implements JsonSerializable
$this->email = $email;
$this->image = $image;
$this->isAdmin = $isAdmin;
$this->memberSince = new DateTime($timestamp);
}
/*
@@ -35,7 +38,7 @@ class User implements JsonSerializable
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT benutzer, status, email, image, isadmin FROM egb_benutzer WHERE id = :ID"
"SELECT benutzer, status, email, image, isadmin, zeitstempel FROM egb_benutzer WHERE id = :ID"
);
$stmt->bindValue(":ID", $id);
$stmt->execute();
@@ -43,14 +46,14 @@ class User implements JsonSerializable
if (!$data) throw new Exception("NotFound");
return new User($id, $data["benutzer"], $data["status"], $data["email"], $data["image"], $data["isadmin"] === 1);
return new User($id, $data["benutzer"], $data["status"], $data["email"], $data["zeitstempel"], $data["image"], $data["isadmin"] === 1);
}
public static function getByEmail(string $email): User
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT id, benutzer, status, image, isadmin FROM egb_benutzer WHERE email = :EMAIL"
"SELECT id, benutzer, status, image, isadmin, zeitstempel FROM egb_benutzer WHERE email = :EMAIL"
);
$stmt->bindValue(":EMAIL", $email);
$stmt->execute();
@@ -58,14 +61,14 @@ class User implements JsonSerializable
if (!$data) throw new Exception("NotFound");
return new User($data["id"], $data["benutzer"], $data["status"], $email, $data["image"], $data["isadmin"] === 1);
return new User($data["id"], $data["benutzer"], $data["status"], $email, $data["zeitstempel"], $data["image"], $data["isadmin"] === 1);
}
public static function getByToken(string $token): User
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT id, benutzer, status, email, image, isadmin FROM egb_benutzer WHERE token = :TOKEN"
"SELECT id, benutzer, status, email, image, isadmin, zeitstempel FROM egb_benutzer WHERE token = :TOKEN"
);
$stmt->bindValue(":TOKEN", $token);
$stmt->execute();
@@ -73,7 +76,7 @@ class User implements JsonSerializable
if (!$data) throw new Exception("NotFound");
return new User($data["id"], $data["benutzer"], $data["status"], $data["email"], $data["image"], $data["isadmin"] === 1);
return new User($data["id"], $data["benutzer"], $data["status"], $data["email"], $data["zeitstempel"], $data["image"], $data["isadmin"] === 1);
}
public static function logIn(string $email, string $password): array
@@ -86,7 +89,7 @@ class User implements JsonSerializable
$data = $stmt->fetch();
if ($data) {
$user = new User($data["id"], $data["benutzer"], $data["status"], $email, $data["image"], $data["isadmin"] === 1);
$user = new User($data["id"], $data["benutzer"], $data["status"], $email, $data["zeitstempel"], $data["image"], $data["isadmin"] === 1);
if (password_verify($password, $data["passwort"])) {
// REHASH for safety should it somehow change
if (password_needs_rehash($data["passwort"], PASSWORD_DEFAULT)) {
@@ -156,7 +159,7 @@ class User implements JsonSerializable
if (!empty($image)) {
$destinationFilename = sprintf('%s.%s', uniqid(), $image->getExtension());
$image->move(Config::getBaseFSPath() . "uploads/profilbilder/$destinationFilename");
$image->move(Config::getStoragePath() . "profilbilder/$destinationFilename");
$stmt = $db->prepare("UPDATE egb_benutzer SET image = :IMG WHERE id = :ID");
$stmt->bindValue(":IMG", $destinationFilename);
@@ -202,6 +205,11 @@ class User implements JsonSerializable
return $this->isAdmin;
}
public function getMemberSince(): DateTime
{
return $this->memberSince;
}
/*
* JSON
*/
@@ -214,7 +222,8 @@ class User implements JsonSerializable
'status' => $this->getStatus(),
'email' => $this->getEmail(),
'image' => $this->getImage(),
'isAdmin' => $this->getIsAdmin()
'isAdmin' => $this->getIsAdmin(),
'memberSince' => $this->getMemberSince(),
];
}
}