Skip to content

Commit 305e02c

Browse files
committed
fix(credentials): grant admin role to credential creator
Revert admin-only restriction — write users can create secrets. Ensure the acting user (creator) always gets admin role on the credential via actingUserId parameter in ensureWorkspaceCredentialMemberships and session.user.id check in route.ts POST. Role mapping: - workspace owner → admin - credential creator (actingUserId/session.user.id) → admin - workspace admin permission → admin - write/read → member
1 parent 80c7061 commit 305e02c

2 files changed

Lines changed: 15 additions & 16 deletions

File tree

apps/sim/app/api/credentials/route.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
ATLASSIAN_SERVICE_ACCOUNT_SECRET_TYPE,
3030
} from '@/lib/oauth/types'
3131
import { captureServerEvent } from '@/lib/posthog/server'
32-
import { checkWorkspaceAccess, hasWorkspaceAdminAccess } from '@/lib/workspaces/permissions/utils'
32+
import { checkWorkspaceAccess } from '@/lib/workspaces/permissions/utils'
3333

3434
const logger = createLogger('CredentialsAPI')
3535

@@ -296,16 +296,8 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
296296
} = parsed.data.body
297297

298298
const workspaceAccess = await checkWorkspaceAccess(workspaceId, session.user.id)
299-
if (!workspaceAccess.hasAccess) {
300-
return NextResponse.json({ error: 'Workspace access required' }, { status: 403 })
301-
}
302-
303-
const isAdmin = await hasWorkspaceAdminAccess(session.user.id, workspaceId)
304-
if (!isAdmin) {
305-
return NextResponse.json(
306-
{ error: 'Admin permission required to manage credentials' },
307-
{ status: 403 }
308-
)
299+
if (!workspaceAccess.canWrite) {
300+
return NextResponse.json({ error: 'Write permission required' }, { status: 403 })
309301
}
310302

311303
let resolvedDisplayName = displayName?.trim() ?? ''
@@ -557,7 +549,10 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
557549
if (workspaceUserIds.length > 0) {
558550
for (const memberUserId of workspaceUserIds) {
559551
const wsPermission = wsPermissionByUser.get(memberUserId)
560-
const isAdmin = memberUserId === workspaceRow.ownerId || wsPermission === 'admin'
552+
const isAdmin =
553+
memberUserId === workspaceRow.ownerId ||
554+
memberUserId === session.user.id ||
555+
wsPermission === 'admin'
561556
await tx.insert(credentialMember).values({
562557
id: generateId(),
563558
credentialId,

apps/sim/lib/credentials/environment.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ async function ensureWorkspaceCredentialMemberships(
3838
credentialId: string,
3939
memberUserIds: string[],
4040
ownerUserId: string,
41-
wsPermissionByUser: Map<string, string>
41+
wsPermissionByUser: Map<string, string>,
42+
actingUserId?: string
4243
) {
4344
if (!memberUserIds.length) return
4445

@@ -65,7 +66,8 @@ async function ensureWorkspaceCredentialMemberships(
6566
const now = new Date()
6667
const values = targetUserIds.map((memberUserId) => {
6768
const wsPermission = wsPermissionByUser.get(memberUserId)
68-
const isAdmin = memberUserId === ownerUserId || wsPermission === 'admin'
69+
const isAdmin =
70+
memberUserId === ownerUserId || memberUserId === actingUserId || wsPermission === 'admin'
6971
return {
7072
id: generateId(),
7173
credentialId,
@@ -173,7 +175,8 @@ export async function syncWorkspaceEnvCredentials(params: {
173175
credentialId,
174176
memberUserIds,
175177
workspaceRow.ownerId,
176-
wsPermissionByUser
178+
wsPermissionByUser,
179+
actingUserId
177180
)
178181
}
179182

@@ -255,7 +258,8 @@ export async function createWorkspaceEnvCredentials(params: {
255258
const membershipValues = createdIds.flatMap((credentialId) =>
256259
memberUserIds.map((memberUserId) => {
257260
const wsPermission = wsPermissionByUser.get(memberUserId)
258-
const isAdmin = memberUserId === ownerUserId || wsPermission === 'admin'
261+
const isAdmin =
262+
memberUserId === ownerUserId || memberUserId === actingUserId || wsPermission === 'admin'
259263
return {
260264
id: generateId(),
261265
credentialId,

0 commit comments

Comments
 (0)