Pagination on the list, optional auth

This commit is contained in:
2024-07-22 16:19:01 +02:00
parent 7ee04b0a4b
commit 5251c43a6b
11 changed files with 499 additions and 119 deletions
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace Khofmann\Auth;
use Exception;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
use Khofmann\Models\User\User;
use Khofmann\Response\Response;
class OptAuth implements IMiddleware
{
public function handle(Request $request): void
{
$token = $request->getHeader("token");
// No token
if ($token === null) {
return;
}
try {
User::getByToken($token);
} catch (Exception $err) {
// No user with this token exists
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
}
}
}
+4 -4
View File
@@ -9,24 +9,24 @@ class Input
public static function post(string $index, $defaultValue = null)
{
$value = Request::request()->getInputHandler()->post($index, $defaultValue);
return empty($value) ? null : $value->getValue();
return !is_object($value) ? $value : $value->getValue();
}
public static function patch(string $index, $defaultValue = null)
{
$value = Request::request()->getInputHandler()->post($index, $defaultValue);
return empty($value) ? null : $value->getValue();
return !is_object($value) ? $value : $value->getValue();
}
public static function get(string $index, $defaultValue = null)
{
$value = Request::request()->getInputHandler()->get($index, $defaultValue);
return empty($value) ? null : $value->getValue();
return !is_object($value) ? $value : $value->getValue();
}
public static function file(string $index, $defaultValue = null)
{
$value = Request::request()->getInputHandler()->file($index, $defaultValue);
return empty($value) ? null : $value->getValue();
return !is_object($value) ? $value : $value->getValue();
}
}
+107
View File
@@ -0,0 +1,107 @@
<?php
namespace Khofmann\Models\Post;
use DateTime;
use Khofmann\Models\User\User;
use JsonSerializable;
use Khofmann\Database\Database;
use PDO;
class Post implements JsonSerializable
{
private int $id;
// User is set if the post was fetched by an authenticated user
private ?User $user;
private ?string $name;
private string $content;
private DateTime $postedAt;
public function __construct(int $id, ?User $user, ?string $name, string $content, string $postedAt)
{
$this->id = $id;
$this->user = $user;
$this->name = $name;
$this->content = $content;
$this->postedAt = new DateTime($postedAt);
}
/*
* Statics
*/
public static function list(int $page, int $limit, bool $authed = false)
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT
COUNT(*)
FROM
egb_gaestebuch"
);
$stmt->execute();
$count = $stmt->fetch(PDO::FETCH_COLUMN, 0);
$stmt = $db->prepare(
"SELECT
*
FROM
egb_gaestebuch
LIMIT $limit
OFFSET " . ($page * $limit)
);
$stmt->execute();
$data = $stmt->fetchAll();
$list = array_map(
function ($item) use ($authed) {
$user = User::getByID($item["benutzer_id"]);
return new Post($item["id"], $authed ? $user : null, !$authed ? $user->getUsername() : null, $item["beitrag"], $item["zeitstempel"]);
},
$data
);
return ["pages" => intdiv($count, $limit) + 1, "data" => $list];
}
/*
* Getters
*/
public function getId(): int
{
return $this->id;
}
public function getUser(): User
{
return $this->user;
}
public function getContent(): string
{
return $this->content;
}
public function getPostedAt(): DateTime
{
return $this->postedAt;
}
/*
* JSON
*/
public function jsonSerialize(): array
{
$user = $this->user ? $this->user : [
"username" => $this->name,
];
return [
'id' => $this->id,
'user' => $user,
'content' => $this->content,
'postedAt' => $this->postedAt,
];
}
}
+12 -2
View File
@@ -226,9 +226,17 @@ class User implements JsonSerializable
return $stmt->execute();
}
public static function list()
public static function list(int $page, int $limit)
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT
COUNT(*)
FROM
egb_gaestebuch"
);
$stmt->execute();
$count = $stmt->fetch(PDO::FETCH_COLUMN, 0);
$stmt = $db->prepare(
"SELECT
b.id, b.benutzer, b.status, b.email, b.image, b.isadmin, b.zeitstempel,
@@ -239,10 +247,12 @@ class User implements JsonSerializable
$stmt->execute();
$data = $stmt->fetchAll();
return array_map(
$list = array_map(
fn ($item) => new User($item["id"], $item["benutzer"], $item["status"], $item["email"], $item["zeitstempel"], $item["image"], $item["isadmin"] === 1, $item["postCount"]),
$data
);
return ["pages" => intdiv($count, $limit) + 1, "data" => $list];
}
/*
+9
View File
@@ -21,4 +21,13 @@ class Response
}
SimpleRouter::response()->json($value, $options, $dept);
}
public static function redirect(string $url, ?int $code = null): void
{
if ($code !== null) {
Response::response()->httpCode($code);
}
Response::response()->redirect($url);
}
}