From 7b897071f58b0fa7527e39407e05c40c8ec09f7e Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Mon, 22 Jul 2024 00:56:04 +0200 Subject: [PATCH] More Facades, User delete --- exam/api/Login/Login.php | 8 +++--- exam/api/Logout/Logout.php | 9 ++++--- exam/api/User/User.php | 39 +++++++++++++++++++-------- exam/api/docs/api.yaml | 43 +++++++++++++++++++++++++----- exam/api/docs/index.html | 22 ++++++++++----- exam/classes/Api/Api.php | 13 +++++++++ exam/classes/Auth/AdminAuth.php | 7 ++--- exam/classes/Auth/Auth.php | 5 ++-- exam/classes/Input/Input.php | 18 ++++++++----- exam/classes/Models/User/User.php | 11 +++++++- exam/classes/Request/Reques.php | 19 +++++++++++++ exam/classes/Response/Response.php | 19 +++++++++++++ exam/routes/routes.php | 9 ++++--- exam/utils/helpers.php | 18 ------------- 14 files changed, 177 insertions(+), 63 deletions(-) create mode 100644 exam/classes/Api/Api.php create mode 100644 exam/classes/Request/Reques.php create mode 100644 exam/classes/Response/Response.php diff --git a/exam/api/Login/Login.php b/exam/api/Login/Login.php index 44a5337..a98a5a5 100644 --- a/exam/api/Login/Login.php +++ b/exam/api/Login/Login.php @@ -3,9 +3,12 @@ namespace Api\Login; use Exception; +use Khofmann\Api\Api; use Khofmann\Input\Input; +use Khofmann\Response\Response; +use Khofmann\Models\User\User; -class Login +class Login extends Api { public function post() { @@ -15,8 +18,7 @@ class Login if (empty($password)) throw new Exception("Missing Password", 400); try { - $response = \Khofmann\Models\User\User::logIn($email, $password); - return json_encode($response); + return Response::json(User::logIn($email, $password)); } catch (Exception $err) { switch ($err->getMessage()) { case "Failed": diff --git a/exam/api/Logout/Logout.php b/exam/api/Logout/Logout.php index 97ad5aa..1cbdcb5 100644 --- a/exam/api/Logout/Logout.php +++ b/exam/api/Logout/Logout.php @@ -2,13 +2,16 @@ namespace Api\Logout; +use Khofmann\Api\Api; use \Khofmann\Models\User\User; +use Khofmann\Request\Request; +use Khofmann\Response\Response; -class Logout +class Logout extends Api { public function post() { - $token = request()->getHeader("token"); - return json_decode(User::getByToken($token)->logOut()); + $token = Request::header("token"); + return Response::json(User::getByToken($token)->logOut()); } } diff --git a/exam/api/User/User.php b/exam/api/User/User.php index aa65bb0..b2b1868 100644 --- a/exam/api/User/User.php +++ b/exam/api/User/User.php @@ -5,13 +5,16 @@ namespace Api\User; use Exception; use Khofmann\Models\User\User as MUser; use Khofmann\Input\Input; +use Khofmann\Response\Response; +use Khofmann\Api\Api; +use Khofmann\Request\Request; -class User +class User extends Api { public function get($id) { try { - return json_encode(MUser::getByID($id)); + return Response::json(MUser::getByID($id)); } catch (Exception $err) { switch ($err->getMessage()) { case "NotFound": @@ -22,14 +25,14 @@ class User } } - public function post($id) + public function patch($id) { - $username = Input::post("username"); - $password = Input::post("password"); + $username = Input::patch("username"); + $password = Input::patch("password"); $image = Input::file("image"); try { - return json_encode(MUser::getByID($id)->update($username, $password, $image)); + return Response::json(MUser::getByID($id)->update($username, $password, $image)); } catch (Exception $err) { switch ($err->getMessage()) { case "NotFound": @@ -46,15 +49,15 @@ class User } } - public function postSelf() + public function patchSelf() { - $token = Input::header("token"); - $username = Input::post("username"); - $password = Input::post("password"); + $token = Request::header("token"); + $username = Input::patch("username"); + $password = Input::patch("password"); $image = Input::file("image"); try { - return json_encode(MUser::getByToken($token)->update($username, $password, $image)); + return Response::json(MUser::getByToken($token)->update($username, $password, $image)); } catch (Exception $err) { switch ($err->getMessage()) { case "NotFound": @@ -70,4 +73,18 @@ class User } } } + + public function delete($id) + { + try { + return Response::json(MUser::getByID($id)->delete()); + } catch (Exception $err) { + switch ($err->getMessage()) { + case "NotFound": + throw new Exception("User not Found", 404); + default: + throw $err; + } + } + } } diff --git a/exam/api/docs/api.yaml b/exam/api/docs/api.yaml index dc48c0c..e02ccca 100644 --- a/exam/api/docs/api.yaml +++ b/exam/api/docs/api.yaml @@ -126,12 +126,12 @@ paths: value: { "message": "User not Found" } tags: - User - post: + patch: summary: Update user description: Update user with ID. Fields are updated in order username, password, image. If one fails, subsequent are not updated.
Use special ID self to update logged in user.
- Requires logged in user to have admin permissions for any ID other than self.
+ Requires logged in user to have admin permissions for any ID other than self. security: - BasicAuth: [] parameters: @@ -177,17 +177,48 @@ paths: value: { "message": "Failed to update username" } tags: - User + delete: + summary: Delete user + description: Delete user with ID.
+ Requires logged in user to have admin permissions. + security: + - BasicAuth: [] + parameters: + - name: id + in: path + description: User ID + required: true + schema: + type: integer + format: int14 + responses: + 200: + description: Success + content: + application/json: + schema: + $ref: "#/components/schemas/BooleanResponse" + examples: + Success: + value: true + 404: + description: User not Found + content: + application/json: + schema: + $ref: "#/components/schemas/ErrorResponse" + examples: + User not Found: + value: { "message": "User not Found" } + tags: + - User externalDocs: url: https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/docs/ security: [] servers: - url: https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/ - description: "" - variables: {} components: - links: {} - callbacks: {} schemas: BooleanResponse: type: boolean diff --git a/exam/api/docs/index.html b/exam/api/docs/index.html index c519119..40e5c7d 100644 --- a/exam/api/docs/index.html +++ b/exam/api/docs/index.html @@ -310,7 +310,9 @@ data-styled.g111[id="sc-eowDPD"]{content:"jcAXWA,"}/*!sc*/ data-styled.g112[id="sc-iAlELC"]{content:"gsBSOU,"}/*!sc*/ .kpMtuJ{font-size:0.929em;line-height:20px;background-color:#186FAF;color:#ffffff;padding:3px 10px;text-transform:uppercase;font-family:Montserrat,sans-serif;margin:0;}/*!sc*/ .ffmPnn{font-size:0.929em;line-height:20px;background-color:#2F8132;color:#ffffff;padding:3px 10px;text-transform:uppercase;font-family:Montserrat,sans-serif;margin:0;}/*!sc*/ -data-styled.g113[id="sc-oeqTF"]{content:"kpMtuJ,ffmPnn,"}/*!sc*/ +.eUIOSr{font-size:0.929em;line-height:20px;background-color:#bf581d;color:#ffffff;padding:3px 10px;text-transform:uppercase;font-family:Montserrat,sans-serif;margin:0;}/*!sc*/ +.ByFMv{font-size:0.929em;line-height:20px;background-color:#cc3333;color:#ffffff;padding:3px 10px;text-transform:uppercase;font-family:Montserrat,sans-serif;margin:0;}/*!sc*/ +data-styled.g113[id="sc-oeqTF"]{content:"kpMtuJ,ffmPnn,eUIOSr,ByFMv,"}/*!sc*/ .bFiOkX{position:absolute;width:100%;z-index:100;background:#fafafa;color:#263238;box-sizing:border-box;box-shadow:0 0 6px rgba(0, 0, 0, 0.33);overflow:hidden;border-bottom-left-radius:4px;border-bottom-right-radius:4px;transition:all 0.25s ease;visibility:hidden;transform:translateY(-50%) scaleY(0);}/*!sc*/ data-styled.g114[id="sc-ezTrPE"]{content:"bFiOkX,"}/*!sc*/ .hdRKqQ{padding:10px;}/*!sc*/ @@ -364,7 +366,7 @@ data-styled.g137[id="sc-kvXgyf"]{content:"fBvPoH,"}/*!sc*/ -

Success

Response samples

Content type
application/json
{
  • "id": 1,
  • "username": "Admin",
  • "status": 1,
  • "email": "marvin@zedat.fu-berlin.de",
  • "image": "profilbilder\\/admin.svg",
  • "isAdmin": true
}

Update user

Update user with ID. Fields are updated in order username, password, image. If one fails, subsequent are not updated.
Use special ID self to update logged in user.
Requires logged in user to have admin permissions for any ID other than self.

+

Response samples

Content type
application/json
{
  • "id": 1,
  • "username": "Admin",
  • "status": 1,
  • "email": "marvin@zedat.fu-berlin.de",
  • "image": "profilbilder\\/admin.svg",
  • "isAdmin": true
}

Update user

Update user with ID. Fields are updated in order username, password, image. If one fails, subsequent are not updated.
Use special ID self to update logged in user.
Requires logged in user to have admin permissions for any ID other than self.

Authorizations:
BasicAuth
path Parameters
id
required
integer <int14>

User ID

Request Body schema: application/json
username
string
password
string
image
string <binary>

Responses

Request samples

Content type
application/json
{
  • "username": "string",
  • "password": "string",
  • "image": "string"
}

Response samples

Content type
application/json
true
+

Request samples

Content type
application/json
{
  • "username": "string",
  • "password": "string",
  • "image": "string"
}

Response samples

Content type
application/json
true

Delete user

Delete user with ID.
Requires logged in user to have admin permissions.

+
Authorizations:
BasicAuth
path Parameters
id
required
integer <int14>

User ID

+

Responses

Response samples

Content type
application/json
true