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
@@ -106,7 +106,7 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
const db = await getDatabase();
|
||||
const currentUser = await db.getUserById(currentUserId);
|
||||
const currentUser = await ensureUserSynced(currentUserId, db);
|
||||
const isStaff =
|
||||
currentUser?.role === "admin" ||
|
||||
currentUser?.role === "superAdmin" ||
|
||||
@@ -140,6 +140,18 @@ export async function POST(request: NextRequest) {
|
||||
content,
|
||||
} = validation.data;
|
||||
|
||||
const targetUser = await db.getUserById(userId);
|
||||
if (!targetUser) {
|
||||
return badRequestResponse("Target user not found");
|
||||
}
|
||||
|
||||
if (
|
||||
currentUser?.role !== "superAdmin" &&
|
||||
(!currentUser?.gymId || targetUser.gymId !== currentUser.gymId)
|
||||
) {
|
||||
return forbiddenResponse("Cannot create recommendations for other gyms");
|
||||
}
|
||||
|
||||
// Handle AI Plan (Legacy/Specific)
|
||||
if (recommendationText && activityPlan && dietPlan && fitnessProfileId) {
|
||||
const recommendation = await db.createRecommendation({
|
||||
@@ -198,6 +210,41 @@ export async function PUT(request: NextRequest) {
|
||||
validation.data;
|
||||
|
||||
const db = await getDatabase();
|
||||
const currentUser = await ensureUserSynced(currentUserId, db);
|
||||
|
||||
if (!currentUser) {
|
||||
return forbiddenResponse("User not found");
|
||||
}
|
||||
|
||||
const isStaff =
|
||||
currentUser.role === "admin" ||
|
||||
currentUser.role === "superAdmin" ||
|
||||
currentUser.role === "trainer";
|
||||
|
||||
if (!isStaff) {
|
||||
return forbiddenResponse();
|
||||
}
|
||||
|
||||
const existingRecommendation = (await db.getAllRecommendations()).find(
|
||||
(recommendation) => recommendation.id === id,
|
||||
);
|
||||
|
||||
if (!existingRecommendation) {
|
||||
return badRequestResponse("Recommendation not found");
|
||||
}
|
||||
|
||||
if (currentUser.role !== "superAdmin") {
|
||||
const targetUser = await db.getUserById(existingRecommendation.userId);
|
||||
if (
|
||||
!currentUser.gymId ||
|
||||
!targetUser ||
|
||||
targetUser.gymId !== currentUser.gymId
|
||||
) {
|
||||
return forbiddenResponse(
|
||||
"Cannot modify recommendations for other gyms",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await db.updateRecommendation(id, {
|
||||
...(status && { status }),
|
||||
|
||||
Reference in new issue
Block a user