@@ -2,6 +2,7 @@ import { eq } from 'drizzle-orm'
22import { type NextRequest , NextResponse } from 'next/server'
33import { getSession } from '@/lib/auth'
44import { createLogger } from '@/lib/logs/console/logger'
5+ import { getUserEntityPermissions } from '@/lib/permissions/utils'
56
67export const dynamic = 'force-dynamic'
78
@@ -42,7 +43,7 @@ export async function GET(req: NextRequest, { params }: { params: Promise<{ id:
4243 }
4344
4445 const [ workflowRecord ] = await db
45- . select ( { userId : workflow . userId } )
46+ . select ( { userId : workflow . userId , workspaceId : workflow . workspaceId } )
4647 . from ( workflow )
4748 . where ( eq ( workflow . id , schedule . workflowId ) )
4849 . limit ( 1 )
@@ -52,7 +53,20 @@ export async function GET(req: NextRequest, { params }: { params: Promise<{ id:
5253 return NextResponse . json ( { error : 'Workflow not found' } , { status : 404 } )
5354 }
5455
55- if ( workflowRecord . userId !== session . user . id ) {
56+ // Check authorization - either the user owns the workflow or has workspace permissions
57+ let isAuthorized = workflowRecord . userId === session . user . id
58+
59+ // If not authorized by ownership and the workflow belongs to a workspace, check workspace permissions
60+ if ( ! isAuthorized && workflowRecord . workspaceId ) {
61+ const userPermission = await getUserEntityPermissions (
62+ session . user . id ,
63+ 'workspace' ,
64+ workflowRecord . workspaceId
65+ )
66+ isAuthorized = userPermission !== null
67+ }
68+
69+ if ( ! isAuthorized ) {
5670 logger . warn ( `[${ requestId } ] User not authorized to view this schedule: ${ scheduleId } ` )
5771 return NextResponse . json ( { error : 'Not authorized to view this schedule' } , { status : 403 } )
5872 }
0 commit comments