Create yet another layer of events middleware
This commit is contained in:
parent
9bdaa4afe9
commit
b22f64eb17
26
functions/api/events-team/events/[id]/_middleware.ts
Normal file
26
functions/api/events-team/events/[id]/_middleware.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { jsonError } from "../../../../common.js";
|
||||||
|
|
||||||
|
export async function onRequest(context: RequestContext) {
|
||||||
|
const pathSegments = new URL(context.request.url).pathname.split("/");
|
||||||
|
|
||||||
|
// Skip checks for the by-id endpoint
|
||||||
|
if (pathSegments.length > 5) return await context.next();
|
||||||
|
|
||||||
|
const eventInfo = await context.env.D1.prepare(
|
||||||
|
"SELECT * FROM events WHERE id = ?;",
|
||||||
|
)
|
||||||
|
.bind(context.params.id)
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (!eventInfo) return jsonError("This event does not exist.", 404);
|
||||||
|
|
||||||
|
if (
|
||||||
|
eventInfo.created_by !== context.data.current_user.id &&
|
||||||
|
![1 << 4, 1 << 12].find((p) => context.data.current_user.permissions & p)
|
||||||
|
)
|
||||||
|
return jsonError("You cannot manage this event.", 403);
|
||||||
|
|
||||||
|
context.data.event = eventInfo;
|
||||||
|
|
||||||
|
return await context.next();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user