Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions apps/sim/lib/workspaces/permissions/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ describe('Permission Utils', () => {
canWrite: false,
canAdmin: false,
workspace: null,
permission: null,
})
})

Expand All @@ -793,6 +794,7 @@ describe('Permission Utils', () => {
canWrite: true,
canAdmin: true,
workspace: { id: 'workspace123', ownerId: 'user123' },
permission: 'admin',
})
})

Expand Down
27 changes: 13 additions & 14 deletions apps/sim/lib/workspaces/permissions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface WorkspaceAccess {
canWrite: boolean
canAdmin: boolean
workspace: WorkspaceWithOwner | null
/** The viewer's raw effective permission, or `null` when the workspace doesn't exist or they have none. */
permission: PermissionType | null
}

/**
Expand Down Expand Up @@ -143,15 +145,22 @@ export async function checkWorkspaceAccess(
const ws = await getWorkspaceWithOwner(workspaceId)

if (!ws) {
return { exists: false, hasAccess: false, canWrite: false, canAdmin: false, workspace: null }
return {
exists: false,
hasAccess: false,
canWrite: false,
canAdmin: false,
workspace: null,
permission: null,
}
}

const permission = await getEffectiveWorkspacePermission(userId, ws)
const hasAccess = permission !== null
const canWrite = permissionSatisfies(permission, 'write')
const canAdmin = permissionSatisfies(permission, 'admin')

return { exists: true, hasAccess, canWrite, canAdmin, workspace: ws }
return { exists: true, hasAccess, canWrite, canAdmin, workspace: ws, permission }
}

/**
Expand Down Expand Up @@ -200,11 +209,7 @@ export async function getUserEntityPermissions(
entityId: string
): Promise<PermissionType | null> {
if (entityType === 'workspace') {
const ws = await getWorkspaceWithOwner(entityId)
if (!ws) {
return null
}
return getEffectiveWorkspacePermission(userId, ws)
return (await checkWorkspaceAccess(entityId, userId)).permission
}

const result = await db
Expand Down Expand Up @@ -387,13 +392,7 @@ export async function hasWorkspaceAdminAccess(
userId: string,
workspaceId: string
): Promise<boolean> {
const ws = await getWorkspaceWithOwner(workspaceId)

if (!ws) {
return false
}

return (await getEffectiveWorkspacePermission(userId, ws)) === 'admin'
return (await checkWorkspaceAccess(workspaceId, userId)).canAdmin
}

/**
Expand Down
Loading