11import { OpenAPIHono } from "@hono/zod-openapi" ;
22import type { Context } from "hono" ;
3+ import { HTTPException } from "hono/http-exception" ;
34import {
45 generateProblemText ,
56 getProblemText ,
@@ -67,6 +68,7 @@ const problems = new OpenAPIHono<{
6768 Bindings : Env ;
6869 Variables : {
6970 userId : string ;
71+ isAdmin : boolean ;
7072 db : Database ;
7173 } ;
7274} > ( ) ;
@@ -123,7 +125,10 @@ async function shouldReturnIdempotentError(
123125
124126// Helper to start workflow if autoGenerate is enabled (for problem creation)
125127async function startWorkflowIfAuto (
126- c : Context < { Bindings : Env ; Variables : { userId : string ; db : Database } } > ,
128+ c : Context < {
129+ Bindings : Env ;
130+ Variables : { userId : string ; isAdmin : boolean ; db : Database } ;
131+ } > ,
127132 problemId : string ,
128133 model ?: string ,
129134 autoGenerate : boolean = true ,
@@ -158,7 +163,10 @@ async function startWorkflowIfAuto(
158163
159164// Helper to start workflow from a specific step if enqueueNextStep is enabled
160165async function startWorkflowFromStepIfEnabled (
161- c : Context < { Bindings : Env ; Variables : { userId : string ; db : Database } } > ,
166+ c : Context < {
167+ Bindings : Env ;
168+ Variables : { userId : string ; isAdmin : boolean ; db : Database } ;
169+ } > ,
162170 problemId : string ,
163171 currentStep : GenerationStep ,
164172 model ?: string ,
@@ -718,6 +726,15 @@ problems.openapi(generateInputCodeRoute, async (c) => {
718726problems . openapi ( getInputCodeRoute , async ( c ) => {
719727 const db = c . get ( "db" ) ;
720728 const { problemId } = c . req . valid ( "param" ) ;
729+ const isAdmin = c . get ( "isAdmin" ) ;
730+
731+ // Check admin access
732+ if ( ! isAdmin ) {
733+ throw new HTTPException ( 403 , {
734+ message :
735+ "Admin access required. This endpoint is only accessible to administrators." ,
736+ } ) ;
737+ }
721738
722739 // Validate prerequisite: test cases must exist
723740 const problem = await getProblem ( problemId , db ) ;
@@ -814,6 +831,7 @@ problems.openapi(generateInputsRoute, async (c) => {
814831problems . openapi ( getInputsRoute , async ( c ) => {
815832 const db = c . get ( "db" ) ;
816833 const { problemId } = c . req . valid ( "param" ) ;
834+ const isAdmin = c . get ( "isAdmin" ) ;
817835
818836 // Validate prerequisite: all test cases must have inputCode
819837 const problem = await getProblem ( problemId , db ) ;
@@ -835,7 +853,20 @@ problems.openapi(getInputsRoute, async (c) => {
835853 ) ;
836854 }
837855
838- const result = await getTestCaseInputs ( problemId , db ) ;
856+ const allInputs = await getTestCaseInputs ( problemId , db ) ;
857+
858+ // If admin, return all inputs; if not admin, only return inputs for sample test cases
859+ let result : unknown [ ] ;
860+ if ( isAdmin ) {
861+ result = allInputs ;
862+ } else {
863+ // Filter to only include inputs for sample test cases
864+ result = problem . testCases
865+ . filter ( ( tc ) => tc . isSampleCase )
866+ . map ( ( tc ) => tc . input )
867+ . filter ( ( input ) => input !== null && input !== undefined ) ;
868+ }
869+
839870 return c . json ( { success : true as const , data : result } , 200 ) ;
840871} ) ;
841872
@@ -947,6 +978,15 @@ problems.openapi(generateSolutionRoute, async (c) => {
947978problems . openapi ( getSolutionRoute , async ( c ) => {
948979 const db = c . get ( "db" ) ;
949980 const { problemId } = c . req . valid ( "param" ) ;
981+ const isAdmin = c . get ( "isAdmin" ) ;
982+
983+ // Check admin access
984+ if ( ! isAdmin ) {
985+ throw new HTTPException ( 403 , {
986+ message :
987+ "Admin access required. This endpoint is only accessible to administrators." ,
988+ } ) ;
989+ }
950990
951991 // Validate prerequisite: all test cases must have input field populated
952992 const problem = await getProblem ( problemId , db ) ;
@@ -1085,6 +1125,15 @@ problems.openapi(generateOutputsRoute, async (c) => {
10851125problems . openapi ( getOutputsRoute , async ( c ) => {
10861126 const db = c . get ( "db" ) ;
10871127 const { problemId } = c . req . valid ( "param" ) ;
1128+ const isAdmin = c . get ( "isAdmin" ) ;
1129+
1130+ // Check admin access
1131+ if ( ! isAdmin ) {
1132+ throw new HTTPException ( 403 , {
1133+ message :
1134+ "Admin access required. This endpoint is only accessible to administrators." ,
1135+ } ) ;
1136+ }
10881137
10891138 // Validate prerequisite: solution must exist
10901139 const problem = await getProblem ( problemId , db ) ;
0 commit comments