Skip to content

Commit eaa3315

Browse files
committed
improvement(audit-log): add resource names and specific invitation actions
1 parent 6d406c1 commit eaa3315

File tree

12 files changed

+55
-20
lines changed

12 files changed

+55
-20
lines changed

apps/sim/app/api/credential-sets/[id]/members/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ async function getCredentialSetWithAccess(credentialSetId: string, userId: strin
1414
const [set] = await db
1515
.select({
1616
id: credentialSet.id,
17+
name: credentialSet.name,
1718
organizationId: credentialSet.organizationId,
1819
providerId: credentialSet.providerId,
1920
})
@@ -186,7 +187,8 @@ export async function DELETE(req: NextRequest, { params }: { params: Promise<{ i
186187
resourceId: id,
187188
actorName: session.user.name ?? undefined,
188189
actorEmail: session.user.email ?? undefined,
189-
description: `Removed member "${memberId}" from credential set "${id}"`,
190+
resourceName: result.set.name,
191+
description: `Removed member from credential set "${result.set.name}"`,
190192
request: req,
191193
})
192194

apps/sim/app/api/knowledge/[id]/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ export async function DELETE(
215215
action: AuditAction.KNOWLEDGE_BASE_DELETED,
216216
resourceType: AuditResourceType.KNOWLEDGE_BASE,
217217
resourceId: id,
218-
description: `Deleted knowledge base "${id}"`,
218+
resourceName: accessCheck.knowledgeBase.name,
219+
description: `Deleted knowledge base "${accessCheck.knowledgeBase.name || id}"`,
219220
request: _request,
220221
})
221222

apps/sim/app/api/knowledge/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export interface EmbeddingData {
9999

100100
export interface KnowledgeBaseAccessResult {
101101
hasAccess: true
102-
knowledgeBase: Pick<KnowledgeBaseData, 'id' | 'userId' | 'workspaceId'>
102+
knowledgeBase: Pick<KnowledgeBaseData, 'id' | 'userId' | 'workspaceId' | 'name'>
103103
}
104104

105105
export interface KnowledgeBaseAccessDenied {
@@ -113,7 +113,7 @@ export type KnowledgeBaseAccessCheck = KnowledgeBaseAccessResult | KnowledgeBase
113113
export interface DocumentAccessResult {
114114
hasAccess: true
115115
document: DocumentData
116-
knowledgeBase: Pick<KnowledgeBaseData, 'id' | 'userId' | 'workspaceId'>
116+
knowledgeBase: Pick<KnowledgeBaseData, 'id' | 'userId' | 'workspaceId' | 'name'>
117117
}
118118

119119
export interface DocumentAccessDenied {
@@ -128,7 +128,7 @@ export interface ChunkAccessResult {
128128
hasAccess: true
129129
chunk: EmbeddingData
130130
document: DocumentData
131-
knowledgeBase: Pick<KnowledgeBaseData, 'id' | 'userId' | 'workspaceId'>
131+
knowledgeBase: Pick<KnowledgeBaseData, 'id' | 'userId' | 'workspaceId' | 'name'>
132132
}
133133

134134
export interface ChunkAccessDenied {
@@ -151,6 +151,7 @@ export async function checkKnowledgeBaseAccess(
151151
id: knowledgeBase.id,
152152
userId: knowledgeBase.userId,
153153
workspaceId: knowledgeBase.workspaceId,
154+
name: knowledgeBase.name,
154155
})
155156
.from(knowledgeBase)
156157
.where(and(eq(knowledgeBase.id, knowledgeBaseId), isNull(knowledgeBase.deletedAt)))
@@ -193,6 +194,7 @@ export async function checkKnowledgeBaseWriteAccess(
193194
id: knowledgeBase.id,
194195
userId: knowledgeBase.userId,
195196
workspaceId: knowledgeBase.workspaceId,
197+
name: knowledgeBase.name,
196198
})
197199
.from(knowledgeBase)
198200
.where(and(eq(knowledgeBase.id, knowledgeBaseId), isNull(knowledgeBase.deletedAt)))

apps/sim/app/api/organizations/[id]/invitations/[invitationId]/route.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,16 @@ export async function PUT(
553553
email: orgInvitation.email,
554554
})
555555

556+
const auditActionMap = {
557+
accepted: AuditAction.ORG_INVITATION_ACCEPTED,
558+
rejected: AuditAction.ORG_INVITATION_REJECTED,
559+
cancelled: AuditAction.ORG_INVITATION_CANCELLED,
560+
} as const
561+
556562
recordAudit({
557563
workspaceId: null,
558564
actorId: session.user.id,
559-
action:
560-
status === 'accepted'
561-
? AuditAction.ORG_INVITATION_ACCEPTED
562-
: AuditAction.ORG_INVITATION_UPDATED,
565+
action: auditActionMap[status],
563566
resourceType: AuditResourceType.ORGANIZATION,
564567
resourceId: organizationId,
565568
actorName: session.user.name ?? undefined,

apps/sim/app/api/permission-groups/[id]/members/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ async function getPermissionGroupWithAccess(groupId: string, userId: string) {
1414
const [group] = await db
1515
.select({
1616
id: permissionGroup.id,
17+
name: permissionGroup.name,
1718
organizationId: permissionGroup.organizationId,
1819
})
1920
.from(permissionGroup)
@@ -158,9 +159,10 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
158159
action: AuditAction.PERMISSION_GROUP_MEMBER_ADDED,
159160
resourceType: AuditResourceType.PERMISSION_GROUP,
160161
resourceId: id,
162+
resourceName: result.group.name,
161163
actorName: session.user.name ?? undefined,
162164
actorEmail: session.user.email ?? undefined,
163-
description: `Added member ${userId} to permission group`,
165+
description: `Added member ${userId} to permission group "${result.group.name}"`,
164166
metadata: { targetUserId: userId, permissionGroupId: id },
165167
request: req,
166168
})
@@ -241,9 +243,10 @@ export async function DELETE(req: NextRequest, { params }: { params: Promise<{ i
241243
action: AuditAction.PERMISSION_GROUP_MEMBER_REMOVED,
242244
resourceType: AuditResourceType.PERMISSION_GROUP,
243245
resourceId: id,
246+
resourceName: result.group.name,
244247
actorName: session.user.name ?? undefined,
245248
actorEmail: session.user.email ?? undefined,
246-
description: `Removed member ${memberToRemove.userId} from permission group`,
249+
description: `Removed member ${memberToRemove.userId} from permission group "${result.group.name}"`,
247250
metadata: { targetUserId: memberToRemove.userId, memberId, permissionGroupId: id },
248251
request: req,
249252
})

apps/sim/app/api/users/me/api-keys/[id]/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ export async function DELETE(
3535
const result = await db
3636
.delete(apiKey)
3737
.where(and(eq(apiKey.id, keyId), eq(apiKey.userId, userId)))
38-
.returning({ id: apiKey.id })
38+
.returning({ id: apiKey.id, name: apiKey.name })
3939

4040
if (!result.length) {
4141
return NextResponse.json({ error: 'API key not found' }, { status: 404 })
4242
}
4343

44+
const deletedKey = result[0]
45+
4446
recordAudit({
4547
workspaceId: null,
4648
actorId: userId,
@@ -49,7 +51,8 @@ export async function DELETE(
4951
resourceId: keyId,
5052
actorName: session.user.name ?? undefined,
5153
actorEmail: session.user.email ?? undefined,
52-
description: `Revoked personal API key: ${keyId}`,
54+
resourceName: deletedKey.name,
55+
description: `Revoked personal API key: ${deletedKey.name}`,
5356
request,
5457
})
5558

apps/sim/app/api/workspaces/[id]/api-keys/[keyId]/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,14 @@ export async function DELETE(
137137
.where(
138138
and(eq(apiKey.workspaceId, workspaceId), eq(apiKey.id, keyId), eq(apiKey.type, 'workspace'))
139139
)
140-
.returning({ id: apiKey.id })
140+
.returning({ id: apiKey.id, name: apiKey.name })
141141

142142
if (deletedRows.length === 0) {
143143
return NextResponse.json({ error: 'API key not found' }, { status: 404 })
144144
}
145145

146+
const deletedKey = deletedRows[0]
147+
146148
recordAudit({
147149
workspaceId,
148150
actorId: userId,
@@ -151,7 +153,8 @@ export async function DELETE(
151153
resourceId: keyId,
152154
actorName: session.user.name ?? undefined,
153155
actorEmail: session.user.email ?? undefined,
154-
description: `Revoked workspace API key: ${keyId}`,
156+
resourceName: deletedKey.name,
157+
description: `Revoked workspace API key: ${deletedKey.name}`,
155158
request,
156159
})
157160

apps/sim/app/api/workspaces/[id]/notifications/[notificationId]/route.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ export async function PUT(request: NextRequest, { params }: RouteParams) {
258258
action: AuditAction.NOTIFICATION_UPDATED,
259259
resourceType: AuditResourceType.NOTIFICATION,
260260
resourceId: notificationId,
261+
resourceName: subscription.notificationType,
261262
actorName: session.user.name ?? undefined,
262263
actorEmail: session.user.email ?? undefined,
263264
description: `Updated ${subscription.notificationType} notification subscription`,
@@ -313,12 +314,17 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
313314
eq(workspaceNotificationSubscription.workspaceId, workspaceId)
314315
)
315316
)
316-
.returning({ id: workspaceNotificationSubscription.id })
317+
.returning({
318+
id: workspaceNotificationSubscription.id,
319+
notificationType: workspaceNotificationSubscription.notificationType,
320+
})
317321

318322
if (deleted.length === 0) {
319323
return NextResponse.json({ error: 'Notification not found' }, { status: 404 })
320324
}
321325

326+
const deletedSubscription = deleted[0]
327+
322328
logger.info('Deleted notification subscription', {
323329
workspaceId,
324330
subscriptionId: notificationId,
@@ -332,7 +338,8 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
332338
resourceId: notificationId,
333339
actorName: session.user.name ?? undefined,
334340
actorEmail: session.user.email ?? undefined,
335-
description: `Deleted notification subscription`,
341+
resourceName: deletedSubscription.notificationType,
342+
description: `Deleted ${deletedSubscription.notificationType} notification subscription`,
336343
request,
337344
})
338345

apps/sim/app/api/workspaces/[id]/notifications/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
263263
action: AuditAction.NOTIFICATION_CREATED,
264264
resourceType: AuditResourceType.NOTIFICATION,
265265
resourceId: subscription.id,
266+
resourceName: data.notificationType,
266267
actorName: session.user.name ?? undefined,
267268
actorEmail: session.user.email ?? undefined,
268269
description: `Created ${data.notificationType} notification subscription`,

apps/sim/app/api/workspaces/[id]/route.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ export async function DELETE(
229229
`Deleting workspace ${workspaceId} for user ${session.user.id}, deleteTemplates: ${deleteTemplates}`
230230
)
231231

232+
// Fetch workspace name before deletion for audit logging
233+
const [workspaceRecord] = await db
234+
.select({ name: workspace.name })
235+
.from(workspace)
236+
.where(eq(workspace.id, workspaceId))
237+
.limit(1)
238+
232239
// Delete workspace and all related data in a transaction
233240
await db.transaction(async (tx) => {
234241
// Get all workflows in this workspace before deletion
@@ -290,7 +297,8 @@ export async function DELETE(
290297
action: AuditAction.WORKSPACE_DELETED,
291298
resourceType: AuditResourceType.WORKSPACE,
292299
resourceId: workspaceId,
293-
description: 'Deleted workspace',
300+
resourceName: workspaceRecord?.name,
301+
description: `Deleted workspace "${workspaceRecord?.name || workspaceId}"`,
294302
request,
295303
})
296304

0 commit comments

Comments
 (0)