harden admin api authorization and add authz regression tests
This commit is contained in:
1 parent
272c9b36dd
commit
10b58245f5
28 files changed
+1336
-240
No files matched your search
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@clerk/nextjs/server";
|
||||
import { getDatabase } from "@/lib/database";
|
||||
import log from "@/lib/logger";
|
||||
import { ensureUserSynced } from "@/lib/sync-user";
|
||||
|
||||
/**
|
||||
* GET /api/notifications
|
||||
@@ -84,6 +85,39 @@ export async function POST(req: NextRequest) {
|
||||
}
|
||||
|
||||
const db = await getDatabase();
|
||||
const currentUser = await ensureUserSynced(userId, db);
|
||||
|
||||
if (!currentUser) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const canCreateNotifications =
|
||||
currentUser.role === "superAdmin" ||
|
||||
currentUser.role === "admin" ||
|
||||
currentUser.role === "trainer";
|
||||
|
||||
if (!canCreateNotifications) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const targetUser = await db.getUserById(targetUserId);
|
||||
if (!targetUser) {
|
||||
return NextResponse.json(
|
||||
{ error: "Target user not found" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
currentUser.role !== "superAdmin" &&
|
||||
(!currentUser.gymId || targetUser.gymId !== currentUser.gymId)
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{ error: "Forbidden - Cannot notify users from other gyms" },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
const notification = await db.createNotification({
|
||||
id: crypto.randomUUID(),
|
||||
userId: targetUserId,
|
||||
|
||||
Reference in new issue
Block a user