diff --git a/exam/.htaccess b/exam/.htaccess index 767f212..9d3bd7c 100644 --- a/exam/.htaccess +++ b/exam/.htaccess @@ -14,6 +14,11 @@ RewriteRule ^phpCourse/exam/vendor/.* index.php [L,NC] RewriteRule ^phpCourse/exam/routes/.* index.php [L,NC] RewriteRule ^phpCourse/exam/react/.* index.php [L,NC] +RewriteCond %{REQUEST_FILENAME} -f +RewriteCond %{REQUEST_FILENAME} !/uploads/.* +RewriteCond %{REQUEST_FILENAME} !/dist/.* +RewriteRule ^ dist [L,NC] + ## ## API routes ## diff --git a/exam/api/Login/Login.php b/exam/api/Login/Login.php index 894f369..44a5337 100644 --- a/exam/api/Login/Login.php +++ b/exam/api/Login/Login.php @@ -9,8 +9,13 @@ class Login { public function post() { + $email = Input::post("email"); + if (empty($email)) throw new Exception("Missing email", 400); + $password = Input::post("password"); + if (empty($password)) throw new Exception("Missing Password", 400); + try { - $response = \Khofmann\Models\User\User::logIn(Input::post("email"), Input::post("password")); + $response = \Khofmann\Models\User\User::logIn($email, $password); return json_encode($response); } catch (Exception $err) { switch ($err->getMessage()) { @@ -20,6 +25,8 @@ class Login throw new Exception("User not Found", 404); case "Invalid": throw new Exception("Invalid Username or Password", 401); + default: + throw $err; } } } diff --git a/exam/api/Logout/Logout.php b/exam/api/Logout/Logout.php index fb13a51..97ad5aa 100644 --- a/exam/api/Logout/Logout.php +++ b/exam/api/Logout/Logout.php @@ -9,6 +9,6 @@ class Logout public function post() { $token = request()->getHeader("token"); - return json_decode(User::getByToken($token)->logOut($token)); + return json_decode(User::getByToken($token)->logOut()); } } diff --git a/exam/api/User/User.php b/exam/api/User/User.php new file mode 100644 index 0000000..e8ce88c --- /dev/null +++ b/exam/api/User/User.php @@ -0,0 +1,48 @@ +getMessage()) { + case "NotFound": + throw new Exception("User not Found", 404); + default: + throw $err; + } + } + } + + public function post($id) + { + $username = Input::post("username"); + $password = Input::post("password"); + $image = Input::file("image"); + + try { + return json_encode(MUser::getByID($id)->update($username, $password, $image)); + } catch (Exception $err) { + switch ($err->getMessage()) { + case "NotFound": + throw new Exception("User not Found", 404); + case "FailedUsername": + throw new Exception("Failed to update username", 500); + case "FailedPassword": + throw new Exception("Failed to update password", 500); + case "FailedImage": + throw new Exception("Failed to update image", 500); + default: + throw $err; + } + } + } +} diff --git a/exam/api/docs/api.yaml b/exam/api/docs/api.yaml index b147bab..e4205b3 100644 --- a/exam/api/docs/api.yaml +++ b/exam/api/docs/api.yaml @@ -27,6 +27,15 @@ paths: examples: Success: value: true + 400: + description: Missing Fields + content: + application/json: + schema: + $ref: "#/components/schemas/ErrorResponse" + examples: + Missing Fields: + value: { "message": "Missing email" } 401: description: Invalid credentials content: @@ -74,6 +83,97 @@ paths: value: true tags: - Login/Logout + /user{id}: + get: + summary: Get user + description: Get user by ID + 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/UserResponse" + examples: + Success: + value: + { + "id": 1, + "username": "Admin", + "status": 1, + "email": "marvin@zedat.fu-berlin.de", + "image": "profilbilder\\/admin.svg", + "isAdmin": 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 + post: + summary: Update user + description: Update user with ID. Fields are updated in order username,password,image. If one fails, subsequent are not updated + security: + - BasicAuth: [] + parameters: + - name: id + in: path + description: User ID + required: true + schema: + type: integer + format: int14 + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/UserUpdateRequest" + 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" } + 500: + description: Update failed + content: + application/json: + schema: + $ref: "#/components/schemas/ErrorResponse" + examples: + User not Found: + value: { "message": "Failed to update username" } + tags: + - User externalDocs: url: https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/docs/ @@ -103,6 +203,31 @@ components: type: string password: type: string + UserResponse: + type: object + properties: + id: + type: number + username: + type: string + status: + type: number + email: + type: string + image: + type: string + nullable: true + isAdmin: + type: boolean + UserUpdateRequest: + type: object + properties: + username: + type: string + password: + type: string + image: + type: string securitySchemes: BasicAuth: type: apiKey @@ -110,5 +235,4 @@ components: in: header tags: - name: Login/Logout - - name: Users - - name: Posts + - name: User diff --git a/exam/api/docs/index.html b/exam/api/docs/index.html index 4b00ab1..8a3853b 100644 --- a/exam/api/docs/index.html +++ b/exam/api/docs/index.html @@ -309,7 +309,8 @@ data-styled.g111[id="sc-eowDPD"]{content:"jcAXWA,"}/*!sc*/ .gsBSOU:focus{box-shadow:inset 0 2px 2px rgba(0, 0, 0, 0.45),0 2px 0 rgba(128, 128, 128, 0.25);}/*!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*/ -data-styled.g113[id="sc-oeqTF"]{content:"kpMtuJ,"}/*!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*/ .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*/ @@ -363,7 +364,7 @@ data-styled.g137[id="sc-kvXgyf"]{content:"fBvPoH,"}/*!sc*/ -

Log in user

Request Body schema: application/json
required
username
required
string
password
required
string

Responses

Request samples

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

Response samples

Content type
application/json
true

Logout

Request samples

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

Response samples

Content type
application/json
true

Logout

Log out User

Authorizations:
BasicAuth

Responses

Response samples

Content type
application/json
true

Users

Posts

+

Response samples

Content type
application/json
true

User

Get user

Get user by ID

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

User ID

+

Responses

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

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

User ID

+
Request Body schema: application/json
username
string
password
string
image
string

Responses

Request samples

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

Response samples

Content type
application/json
true