Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/app/api/scim/v2/Groups/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { writeAuditLog } from "@/server/services/audit";
import { authenticateScim } from "../auth";
import {
loadGroupMappings,
getMappingsForGroup,
applyMappedMemberships,
} from "@/server/services/group-mappings";

interface ScimGroupResponse {
schemas: string[];
Expand Down Expand Up @@ -33,6 +38,30 @@ function scimError(detail: string, status: number) {
);
}

/**
* Process members from a SCIM group request through the mapping table.
*/
async function applyGroupMembers(
groupName: string,
members: unknown,
): Promise<void> {
if (!Array.isArray(members) || members.length === 0) return;

const allMappings = await loadGroupMappings();
const groupMappings = getMappingsForGroup(allMappings, groupName);
if (groupMappings.length === 0) return;

await prisma.$transaction(async (tx) => {
for (const member of members) {
const userId = (member as { value?: unknown }).value;
if (typeof userId !== "string") continue;
const user = await tx.user.findUnique({ where: { id: userId } });
if (!user) continue;
await applyMappedMemberships(tx, userId, groupMappings);
}
});
}

export async function GET(req: NextRequest) {
if (!(await authenticateScim(req))) {
return scimError("Unauthorized", 401);
Expand Down Expand Up @@ -100,6 +129,8 @@ export async function POST(req: NextRequest) {
});
}

await applyGroupMembers(displayName, body.members);

await writeAuditLog({
userId: null,
action: "scim.group_adopted",
Expand All @@ -118,6 +149,8 @@ export async function POST(req: NextRequest) {
},
});

await applyGroupMembers(displayName, body.members);

await writeAuditLog({
userId: null,
action: "scim.group_created",
Expand Down
Loading