-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Handlers } from "$fresh/server.ts"; | ||
import { User } from "../../../db.ts"; | ||
import { get_task_manager } from "../../../server.ts"; | ||
import { parse_big_int } from "../../../server/parse_form.ts"; | ||
import { return_data, return_error } from "../../../server/utils.ts"; | ||
|
||
export const handler: Handlers = { | ||
async DELETE(req, ctx) { | ||
const user = <User | undefined> ctx.state.user; | ||
let data: FormData | null = null; | ||
try { | ||
data = await req.formData(); | ||
} catch (_) { | ||
return return_error(1, "Invalid parameters."); | ||
} | ||
const id = await parse_big_int(data.get("id"), null); | ||
if (id === null) { | ||
return return_error(2, "token id not specified."); | ||
} | ||
const m = get_task_manager(); | ||
const token = m.db.get_token_by_id(id); | ||
if (!token) { | ||
return return_error(404, "Token not found."); | ||
} | ||
if (user) { | ||
if (!user.is_admin && user.id != token.uid) { | ||
return return_error(403, "Permission denied."); | ||
} | ||
if (user.is_admin && user.id != token.uid && user.id != 0) { | ||
const u = m.db.get_user(token.uid); | ||
if (!u) { | ||
return return_error(404, "User not found."); | ||
} | ||
if (u.is_admin) { | ||
return return_error(3, "Only root user can delete admin user's token.", 403); | ||
} | ||
} | ||
} | ||
m.db.delete_token(token.token); | ||
return return_data(true); | ||
}, | ||
} |