diff --git a/source/server/projectAccess.ts b/source/server/projectAccess.ts index e0136b1..c3d2e9e 100644 --- a/source/server/projectAccess.ts +++ b/source/server/projectAccess.ts @@ -31,16 +31,14 @@ export function canEdit( project: Project, members: ProjectMember[], user: JwtUs return role === 'editor' || role === 'admin'; } -export type AccessResult = - | { ok: true } - | { ok: false; status: number; error: string }; +export interface AccessDenial { status: number; error: string; } -export function checkAccess( projectId: string, user: JwtUser, mode: 'view' | 'edit' ): AccessResult +export function checkAccess( projectId: string, user: JwtUser, mode: 'view' | 'edit' ): AccessDenial | null { const project = projects.findById( projectId ); - if ( !project ) return { ok: false, status: 404, error: 'Not found' }; + if ( !project ) return { status: 404, error: 'Not found' }; const members = projectMembers.forProject( projectId ); const allowed = mode === 'view' ? canView( project, members, user ) : canEdit( project, members, user ); - if ( !allowed ) return { ok: false, status: 403, error: 'Forbidden' }; - return { ok: true }; + if ( !allowed ) return { status: 403, error: 'Forbidden' }; + return null; } diff --git a/source/server/routes/files.ts b/source/server/routes/files.ts index 76f4fe2..7134c5a 100644 --- a/source/server/routes/files.ts +++ b/source/server/routes/files.ts @@ -8,15 +8,15 @@ router.use( requireAuth ); router.get( '/:projectId/tree', ( req, res ) => { - const access = checkAccess( req.params.projectId, req.user!, 'view' ); - if ( !access.ok ) { res.status( access.status ).json( { error: access.error } ); return; } + const denied = checkAccess( req.params.projectId, req.user!, 'view' ); + if ( denied ) { res.status( denied.status ).json( { error: denied.error } ); return; } res.json( getFileTree( req.params.projectId ) ); } ); router.get( '/:projectId/*', ( req, res ) => { - const access = checkAccess( req.params.projectId, req.user!, 'view' ); - if ( !access.ok ) { res.status( access.status ).json( { error: access.error } ); return; } + const denied = checkAccess( req.params.projectId, req.user!, 'view' ); + if ( denied ) { res.status( denied.status ).json( { error: denied.error } ); return; } const filePath = ( req.params as Record )[ 0 ]; const content = readProjectFile( req.params.projectId, filePath ); if ( content === null ) { res.status( 404 ).json( { error: 'Not found' } ); return; } @@ -25,8 +25,8 @@ router.get( '/:projectId/*', ( req, res ) => router.post( '/:projectId/create-file', ( req, res ) => { - const access = checkAccess( req.params.projectId, req.user!, 'edit' ); - if ( !access.ok ) { res.status( access.status ).json( { error: access.error } ); return; } + const denied = checkAccess( req.params.projectId, req.user!, 'edit' ); + if ( denied ) { res.status( denied.status ).json( { error: denied.error } ); return; } const { path: filePath } = req.body as { path: string }; if ( !filePath ) { res.status( 400 ).json( { error: 'path required' } ); return; } const ok = createProjectFile( req.params.projectId, filePath ); @@ -36,8 +36,8 @@ router.post( '/:projectId/create-file', ( req, res ) => router.post( '/:projectId/create-directory', ( req, res ) => { - const access = checkAccess( req.params.projectId, req.user!, 'edit' ); - if ( !access.ok ) { res.status( access.status ).json( { error: access.error } ); return; } + const denied = checkAccess( req.params.projectId, req.user!, 'edit' ); + if ( denied ) { res.status( denied.status ).json( { error: denied.error } ); return; } const { path: dirPath } = req.body as { path: string }; if ( !dirPath ) { res.status( 400 ).json( { error: 'path required' } ); return; } const ok = createProjectDirectory( req.params.projectId, dirPath ); @@ -47,8 +47,8 @@ router.post( '/:projectId/create-directory', ( req, res ) => router.post( '/:projectId/rename', ( req, res ) => { - const access = checkAccess( req.params.projectId, req.user!, 'edit' ); - if ( !access.ok ) { res.status( access.status ).json( { error: access.error } ); return; } + const denied = checkAccess( req.params.projectId, req.user!, 'edit' ); + if ( denied ) { res.status( denied.status ).json( { error: denied.error } ); return; } const { path: oldPath, newName } = req.body as { path: string; newName: string }; if ( !oldPath || !newName ) { res.status( 400 ).json( { error: 'path and newName required' } ); return; } const ok = renameProjectEntry( req.params.projectId, oldPath, newName ); @@ -58,8 +58,8 @@ router.post( '/:projectId/rename', ( req, res ) => router.post( '/:projectId/delete', ( req, res ) => { - const access = checkAccess( req.params.projectId, req.user!, 'edit' ); - if ( !access.ok ) { res.status( access.status ).json( { error: access.error } ); return; } + const denied = checkAccess( req.params.projectId, req.user!, 'edit' ); + if ( denied ) { res.status( denied.status ).json( { error: denied.error } ); return; } const { path: targetPath } = req.body as { path: string }; if ( !targetPath ) { res.status( 400 ).json( { error: 'path required' } ); return; } const ok = deleteProjectEntry( req.params.projectId, targetPath ); @@ -69,8 +69,8 @@ router.post( '/:projectId/delete', ( req, res ) => router.put( '/:projectId/*', ( req, res ) => { - const access = checkAccess( req.params.projectId, req.user!, 'edit' ); - if ( !access.ok ) { res.status( access.status ).json( { error: access.error } ); return; } + const denied = checkAccess( req.params.projectId, req.user!, 'edit' ); + if ( denied ) { res.status( denied.status ).json( { error: denied.error } ); return; } const filePath = ( req.params as Record )[ 0 ]; if ( typeof req.body !== 'string' ) { res.status( 400 ).json( { error: 'Content must be text' } ); return; } const ok = writeProjectFile( req.params.projectId, filePath, req.body );