diff --git a/db.ts b/db.ts index 56abd2f..4e80d0c 100644 --- a/db.ts +++ b/db.ts @@ -1315,6 +1315,21 @@ export class EhDb { get_user_count() { return this.db.query<[number]>("SELECT COUNT(*) FROM user;")[0][0]; } + get_users(limit?: number, offset?: number) { + let sql = ""; + const args = []; + if (limit !== undefined) { + sql += " LIMIT ?"; + args.push(limit); + } + if (offset !== undefined) { + sql += " OFFSET ?"; + args.push(offset); + } + return this.convert_user( + this.db.queryEntries(`SELECT * FROM user${sql};`, args), + ); + } list_client_configs(uid: number, client: string) { return this.db.queryEntries( "SELECT * FROM client_config WHERE uid = ? AND client = ?;", diff --git a/fresh.gen.ts b/fresh.gen.ts index 6e8a294..52a4977 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -29,6 +29,7 @@ import * as $api_task_export_zip_cfg from "./routes/api/task/export_zip_cfg.ts"; import * as $api_thumbnail_id_ from "./routes/api/thumbnail/[id].ts"; import * as $api_token from "./routes/api/token.ts"; import * as $api_user from "./routes/api/user.ts"; +import * as $api_user_list from "./routes/api/user/list.ts"; import * as $file_id_ from "./routes/file/[id].ts"; import * as $file_verify_id_ from "./routes/file/[verify]/[id].ts"; import * as $file_middleware from "./routes/file/_middleware.ts"; @@ -69,6 +70,7 @@ const manifest = { "./routes/api/thumbnail/[id].ts": $api_thumbnail_id_, "./routes/api/token.ts": $api_token, "./routes/api/user.ts": $api_user, + "./routes/api/user/list.ts": $api_user_list, "./routes/file/[id].ts": $file_id_, "./routes/file/[verify]/[id].ts": $file_verify_id_, "./routes/file/_middleware.ts": $file_middleware, diff --git a/routes/api/user/list.ts b/routes/api/user/list.ts new file mode 100644 index 0000000..deb7e57 --- /dev/null +++ b/routes/api/user/list.ts @@ -0,0 +1,29 @@ +import { Handlers } from "$fresh/server.ts"; +import { User } from "../../../db.ts"; +import { get_task_manager } from "../../../server.ts"; +import { parse_bool, parse_int } from "../../../server/parse_form.ts"; +import { BUser } from "../../../server/user.ts"; +import { return_data, return_error } from "../../../server/utils.ts"; + +export const handler: Handlers = { + async GET(req, ctx) { + const user = ctx.state.user; + if (user && !user.is_admin) { + return return_error(403, "Permission denied."); + } + const u = new URL(req.url); + const all = await parse_bool(u.searchParams.get("all"), false); + const offset = await parse_int(u.searchParams.get("offset"), 0); + const limit = await parse_int(u.searchParams.get("limit"), 20); + const m = get_task_manager(); + const users = all ? m.db.get_users() : m.db.get_users(limit, offset); + return return_data(users.map((v) => { + return { + id: v.id, + is_admin: v.is_admin, + permissions: v.permissions, + username: v.username, + }; + })); + }, +};