From 1680f84a4ea26a9611ba81c9fe398dfffb20b8d0 Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 12:01:19 +0100 Subject: [PATCH] bug delete user + /user/info --- app/gateway/user_gateway.php | 21 +++++++++++++++++++-- app/routes.php | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/gateway/user_gateway.php b/app/gateway/user_gateway.php index 807e8e5..f8d4303 100644 --- a/app/gateway/user_gateway.php +++ b/app/gateway/user_gateway.php @@ -39,14 +39,16 @@ class UserGateway { // Delete User: (1:OK, 2:Unauthorize, 3:No User) public function deleteUser(string $uuid) : int { - $query = "DELETE FROM user WHERE id=:uuid;"; + $query = "DELETE FROM user WHERE id=:uuid RETURNING row_count();"; try { $this->con->executeQuery($query, array( ':uuid' => array($uuid, PDO::PARAM_STR) )); + $results = $this->con->getResults(); } catch (PDOException $e) { - return -1; + return -2; } + if(count($results) === 0) return -1; return 0; } @@ -69,6 +71,21 @@ class UserGateway { return json_encode($this->token->getNewJsonToken($results[0]['id'])); } + public function getInfo(string $uuid) { + $query = "SELECT email, username FROM user WHERE id=:uuid;"; + try { + $this->con->executeQuery($query,array( + ':uuid' => array($uuid, PDO::PARAM_STR) + )); + $results = $this->con->getResults(); + } catch(PDOException $e) { + return -2; + } + if(count($results) === 0) return -1; + + return ["email" => $results[0]['email'], "username" => $results[0]['username']]; + } + public function updateMail(string $uuid, string $new_email) { $query = "UPDATE user SET email=:new_email WHERE id=:uuid;"; try { diff --git a/app/routes.php b/app/routes.php index 56cd894..8a20fe4 100644 --- a/app/routes.php +++ b/app/routes.php @@ -50,6 +50,8 @@ return function (App $app) { return $res->withStatus(200); case -1: return $res->withStatus(404); + case -2: + return $res->withStatus(500); } return $res->withStatus(500); }); @@ -73,6 +75,25 @@ return function (App $app) { return $res; }); + $app->get('/user/info', function(Request $req, Response $res) { + $token = $req->getHeader('Authorization')[0]; + if(!(new Token)->verifyToken($token)) { + return $res->withStatus(401); + } + + $uuid = (new Token)->getUuidFromToken($token); + $code = (new UserGateway)->getInfo($uuid); + switch($code) { + case -1: + return $res->withStatus(404); + case -2: + return $res->withStatus(500); + } + + $res->getBody()->write(json_encode($code)); + return $res; + }); + // Update Mail $app->put('/user/email', function(Request $req, Response $res) { $token = $req->getHeader('Authorization')[0];