More Facades, User delete
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Khofmann\Api;
|
||||
|
||||
use Khofmann\Response\Response;
|
||||
|
||||
class Api
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
Response::response()->header("Cache-control: no-cache");
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use Exception;
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\Http\Request;
|
||||
use Khofmann\Models\User\User;
|
||||
use Khofmann\Response\Response;
|
||||
|
||||
class AdminAuth implements IMiddleware
|
||||
{
|
||||
@@ -15,17 +16,17 @@ class AdminAuth implements IMiddleware
|
||||
|
||||
// No token
|
||||
if ($token === null) {
|
||||
response()->httpCode(401)->json(["message" => "Not Authorized"]);
|
||||
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
|
||||
}
|
||||
|
||||
try {
|
||||
$user = User::getByToken($token);
|
||||
if (!$user->getIsAdmin()) {
|
||||
response()->httpCode(401)->json(["message" => "Not Authorized"]);
|
||||
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
|
||||
}
|
||||
} catch (Exception $err) {
|
||||
// No user with this token exists
|
||||
response()->httpCode(401)->json(["message" => "Not Authorized"]);
|
||||
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ use Exception;
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\Http\Request;
|
||||
use Khofmann\Models\User\User;
|
||||
use Khofmann\Response\Response;
|
||||
|
||||
class Auth implements IMiddleware
|
||||
{
|
||||
@@ -15,14 +16,14 @@ class Auth implements IMiddleware
|
||||
|
||||
// No token
|
||||
if ($token === null) {
|
||||
response()->httpCode(401)->json(["message" => "Not Authorized"]);
|
||||
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
|
||||
}
|
||||
|
||||
try {
|
||||
User::getByToken($token);
|
||||
} catch (Exception $err) {
|
||||
// No user with this token exists
|
||||
response()->httpCode(401)->json(["message" => "Not Authorized"]);
|
||||
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,25 +2,29 @@
|
||||
|
||||
namespace Khofmann\Input;
|
||||
|
||||
use Khofmann\Request\Request;
|
||||
|
||||
class Input
|
||||
{
|
||||
public static function header($name, $defaultValue = null, $tryParse = true)
|
||||
{
|
||||
return request()->getHeader($name, $defaultValue, $tryParse);
|
||||
}
|
||||
|
||||
|
||||
public static function post($index, $defaultValue = null)
|
||||
{
|
||||
return request()->getInputHandler()->post($index, $defaultValue);
|
||||
return Request::request()->getInputHandler()->post($index, $defaultValue);
|
||||
}
|
||||
|
||||
public static function patch($index, $defaultValue = null)
|
||||
{
|
||||
return Request::request()->getInputHandler()->post($index, $defaultValue);
|
||||
}
|
||||
|
||||
public static function get($index, $defaultValue = null)
|
||||
{
|
||||
return request()->getInputHandler()->get($index, $defaultValue);
|
||||
return Request::request()->getInputHandler()->get($index, $defaultValue);
|
||||
}
|
||||
|
||||
public static function file($index, $defaultValue = null)
|
||||
{
|
||||
return request()->getInputHandler()->file($index, $defaultValue);
|
||||
return Request::request()->getInputHandler()->file($index, $defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,9 @@ class User implements JsonSerializable
|
||||
private ?string $image;
|
||||
private bool $isAdmin;
|
||||
private DateTime $memberSince;
|
||||
private int $postCount;
|
||||
|
||||
protected function __construct(int $id, string $username, int $status, string $email, string $timestamp, string $image, bool $isAdmin)
|
||||
protected function __construct(int $id, string $username, int $status, string $email, string $timestamp, ?string $image, bool $isAdmin)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->username = $username;
|
||||
@@ -171,6 +172,14 @@ class User implements JsonSerializable
|
||||
return true;
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare("DELETE FROM egb_benutzer WHERE id = :ID");
|
||||
$stmt->bindValue(":ID", $this->id);
|
||||
return $stmt->execute();
|
||||
}
|
||||
|
||||
/*
|
||||
* Getters
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Khofmann\Request;
|
||||
|
||||
use Pecee\Http\Request as PRequest;
|
||||
use Pecee\SimpleRouter\SimpleRouter;
|
||||
|
||||
class Request
|
||||
{
|
||||
public static function request(): PRequest
|
||||
{
|
||||
return SimpleRouter::request();
|
||||
}
|
||||
|
||||
public static function header($name, $defaultValue = null, $tryParse = true)
|
||||
{
|
||||
return request()->getHeader($name, $defaultValue, $tryParse);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Khofmann\Response;
|
||||
|
||||
use Pecee\SimpleRouter\SimpleRouter;
|
||||
use Pecee\Http\Response as PResponse;
|
||||
|
||||
class Response
|
||||
{
|
||||
public static function response(): PResponse
|
||||
{
|
||||
return SimpleRouter::response();
|
||||
}
|
||||
|
||||
public static function json($value, int $options = 0, int $dept = 512)
|
||||
{
|
||||
return SimpleRouter::response()->json($value, $options, $dept);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user