User List

This commit is contained in:
Kilian Hofmann 2024-07-22 14:18:48 +02:00
parent 2fde820942
commit 7ee04b0a4b
5 changed files with 121 additions and 8 deletions

15
exam/api/Users/Users.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace Api\Users;
use Khofmann\Api\Api;
use Khofmann\Models\User\User;
use Khofmann\Response\Response;
class Users extends Api
{
public function get()
{
Response::json(User::list());
}
}

View File

@ -158,6 +158,57 @@ paths:
value: { "message": "User not found" } value: { "message": "User not found" }
tags: tags:
- Register - Register
/users:
get:
summary: List users
description: List all users. <br>
Requires logged in user to have admin permissions.
security:
- BasicAuth: []
responses:
200:
description: Success
content:
application/json:
schema:
$ref: "#/components/schemas/UserListResponse"
examples:
Success:
value:
[
{
"id": 1,
"username": "Admin",
"status": 1,
"email": "marvin@zedat.fu-berlin.de",
"image": "669d41fbdb56b.png",
"isAdmin": true,
"memberSince":
{
"date": "2024-07-22 14:02:49.000000",
"timezone_type": 3,
"timezone": "Europe/Berlin",
},
"postCount": 3,
},
{
"id": 2,
"username": "Max",
"status": 1,
"email": "max@moritz.net",
"image": "profilbilder/max.svg",
"isAdmin": false,
"memberSince":
{
"date": "2024-07-22 03:07:41.000000",
"timezone_type": 3,
"timezone": "Europe/Berlin",
},
"postCount": 2,
},
]
tags:
- User
/user{id}: /user{id}:
get: get:
summary: Get user summary: Get user
@ -187,8 +238,15 @@ paths:
"username": "Admin", "username": "Admin",
"status": 1, "status": 1,
"email": "marvin@zedat.fu-berlin.de", "email": "marvin@zedat.fu-berlin.de",
"image": "profilbilder\\/admin.svg", "image": "669d41fbdb56b.png",
"isAdmin": true, "isAdmin": true,
"memberSince":
{
"date": "2024-07-22 14:02:49.000000",
"timezone_type": 3,
"timezone": "Europe/Berlin",
},
"postCount": 3,
} }
404: 404:
description: User not found description: User not found
@ -328,6 +386,18 @@ components:
nullable: true nullable: true
isAdmin: isAdmin:
type: boolean type: boolean
memberSince:
type: object
properties:
date:
type: string
format: date-time
timezone_type:
type: number
timezone:
type: string
postCount:
type: number
UserUpdateRequest: UserUpdateRequest:
type: object type: object
properties: properties:
@ -359,12 +429,15 @@ components:
code: code:
type: string type: string
format: uuid4 format: uuid4
UserListResponse:
type: array
items:
$ref: "#/components/schemas/UserResponse"
securitySchemes: securitySchemes:
BasicAuth: BasicAuth:
type: apiKey type: apiKey
name: token name: token
in: header in: header
format: uuid4
tags: tags:
- name: Login/Logout - name: Login/Logout
- name: Register - name: Register

File diff suppressed because one or more lines are too long

View File

@ -226,6 +226,25 @@ class User implements JsonSerializable
return $stmt->execute(); return $stmt->execute();
} }
public static function list()
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT
b.id, b.benutzer, b.status, b.email, b.image, b.isadmin, b.zeitstempel,
(SELECT COUNT(*) FROM egb_gaestebuch WHERE benutzer_id = b.id) as postCount
FROM
egb_benutzer AS b"
);
$stmt->execute();
$data = $stmt->fetchAll();
return array_map(
fn ($item) => new User($item["id"], $item["benutzer"], $item["status"], $item["email"], $item["zeitstempel"], $item["image"], $item["isadmin"] === 1, $item["postCount"]),
$data
);
}
/* /*
* Members * Members
*/ */

View File

@ -35,6 +35,8 @@ SimpleRouter::group(["middleware" => Khofmann\Auth\Auth::class], function () {
* Admin Auth routes * Admin Auth routes
*/ */
SimpleRouter::group(["middleware" => Khofmann\Auth\AdminAuth::class], function () { SimpleRouter::group(["middleware" => Khofmann\Auth\AdminAuth::class], function () {
// List users
SimpleRouter::get("/users", [Api\Users\Users::class, "get"]);
// Update any user // Update any user
SimpleRouter::patch("/user/{id}", [Api\User\User::class, "patch"]); SimpleRouter::patch("/user/{id}", [Api\User\User::class, "patch"]);
// Delete any user // Delete any user