11import { NextRequest , NextResponse } from 'next/server'
2- import { eq } from 'drizzle-orm'
2+ import { and , eq } from 'drizzle-orm'
33import { z } from 'zod'
44import { getSession } from '@/lib/auth'
55import { createLogger } from '@/lib/logs/console-logger'
66import { Variable } from '@/stores/panel/variables/types'
77import { db } from '@/db'
8- import { workflow } from '@/db/schema'
8+ import { workflow , workspaceMember } from '@/db/schema'
99
1010const logger = createLogger ( 'WorkflowVariablesAPI' )
1111
@@ -33,7 +33,7 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
3333 return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
3434 }
3535
36- // Check if the workflow belongs to the user
36+ // Get the workflow record
3737 const workflowRecord = await db
3838 . select ( )
3939 . from ( workflow )
@@ -45,9 +45,31 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
4545 return NextResponse . json ( { error : 'Workflow not found' } , { status : 404 } )
4646 }
4747
48- if ( workflowRecord [ 0 ] . userId !== session . user . id ) {
48+ const workflowData = workflowRecord [ 0 ]
49+ const workspaceId = workflowData . workspaceId
50+
51+ // Check authorization - either the user owns the workflow or is a member of the workspace
52+ let isAuthorized = workflowData . userId === session . user . id
53+
54+ // If not authorized by ownership and the workflow belongs to a workspace, check workspace membership
55+ if ( ! isAuthorized && workspaceId ) {
56+ const membership = await db
57+ . select ( )
58+ . from ( workspaceMember )
59+ . where (
60+ and (
61+ eq ( workspaceMember . workspaceId , workspaceId ) ,
62+ eq ( workspaceMember . userId , session . user . id )
63+ )
64+ )
65+ . limit ( 1 )
66+
67+ isAuthorized = membership . length > 0
68+ }
69+
70+ if ( ! isAuthorized ) {
4971 logger . warn (
50- `[${ requestId } ] User ${ session . user . id } attempted to update variables for workflow ${ workflowId } owned by ${ workflowRecord [ 0 ] . userId } `
72+ `[${ requestId } ] User ${ session . user . id } attempted to update variables for workflow ${ workflowId } without permission `
5173 )
5274 return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
5375 }
@@ -115,7 +137,7 @@ export async function GET(req: NextRequest, { params }: { params: Promise<{ id:
115137 return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
116138 }
117139
118- // Check if the workflow belongs to the user
140+ // Get the workflow record
119141 const workflowRecord = await db
120142 . select ( )
121143 . from ( workflow )
@@ -127,15 +149,37 @@ export async function GET(req: NextRequest, { params }: { params: Promise<{ id:
127149 return NextResponse . json ( { error : 'Workflow not found' } , { status : 404 } )
128150 }
129151
130- if ( workflowRecord [ 0 ] . userId !== session . user . id ) {
152+ const workflowData = workflowRecord [ 0 ]
153+ const workspaceId = workflowData . workspaceId
154+
155+ // Check authorization - either the user owns the workflow or is a member of the workspace
156+ let isAuthorized = workflowData . userId === session . user . id
157+
158+ // If not authorized by ownership and the workflow belongs to a workspace, check workspace membership
159+ if ( ! isAuthorized && workspaceId ) {
160+ const membership = await db
161+ . select ( )
162+ . from ( workspaceMember )
163+ . where (
164+ and (
165+ eq ( workspaceMember . workspaceId , workspaceId ) ,
166+ eq ( workspaceMember . userId , session . user . id )
167+ )
168+ )
169+ . limit ( 1 )
170+
171+ isAuthorized = membership . length > 0
172+ }
173+
174+ if ( ! isAuthorized ) {
131175 logger . warn (
132- `[${ requestId } ] User ${ session . user . id } attempted to access variables for workflow ${ workflowId } owned by ${ workflowRecord [ 0 ] . userId } `
176+ `[${ requestId } ] User ${ session . user . id } attempted to access variables for workflow ${ workflowId } without permission `
133177 )
134178 return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
135179 }
136180
137181 // Return variables if they exist
138- const variables = ( workflowRecord [ 0 ] . variables as Record < string , Variable > ) || { }
182+ const variables = ( workflowData . variables as Record < string , Variable > ) || { }
139183
140184 // Add cache headers to prevent frequent reloading
141185 const headers = new Headers ( {
0 commit comments