Skip to content

Commit

Permalink
Add api to list user
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed May 28, 2024
1 parent 4f2d7a1 commit 5b85472
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserRaw>(`SELECT * FROM user${sql};`, args),
);
}
list_client_configs(uid: number, client: string) {
return this.db.queryEntries<ClientConfig>(
"SELECT * FROM client_config WHERE uid = ? AND client = ?;",
Expand Down
2 changes: 2 additions & 0 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions routes/api/user/list.ts
Original file line number Diff line number Diff line change
@@ -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 = <User | undefined> 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 <BUser> {
id: v.id,
is_admin: v.is_admin,
permissions: v.permissions,
username: v.username,
};
}));
},
};

0 comments on commit 5b85472

Please sign in to comment.