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*/
{- "id": 1,
- "username": "Admin",
- "status": 1,
- "email": "marvin@zedat.fu-berlin.de",
- "image": "profilbilder\\/admin.svg",
- "isAdmin": true
}{- "id": 1,
- "username": "Admin",
- "status": 1,
- "email": "marvin@zedat.fu-berlin.de",
- "image": "profilbilder\\/admin.svg",
- "isAdmin": true
}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.
| id required | integer <int14> User ID |
| username | string |
| password | string |
| image | string <binary> |
{- "username": "string",
- "password": "string",
- "image": "string"
}true{- "username": "string",
- "password": "string",
- "image": "string"
}true