mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
refactor(server): one addon guard, one demo-mode condition (#1829)
Two pieces of copy-paste in the guard layer. Three addon guards — collections, airtrail, journey — did the same thing and differed only in the addon id they read and the word leading the 404. They are one AddonGuard plus a @RequireAddon(addonId, label) decorator now, read through the Reflector. A handler-level decorator overrides the controller's, and a route without the decorator is not gated at all. The demo-mode upload block was copy-pasted into six handlers, each carrying its own copy of the message. All six call isDemoWriteBlocked now, against the same DEMO_WRITE_ERROR body. That one is deliberately NOT a guard, though all six sites are handlers and a guard is the obvious shape — and this PR did build one first. Guards run before the multipart parser, so throwing from one leaves the request body unread and Node resets the connection: the client gets ECONNRESET instead of the 403. PROFILE-015 caught it. The check has to sit inside the handler, after the interceptor has drained the upload, for the response to arrive at all. The function carries that reasoning so the next person does not rediscover it. Two smaller findings on the way there. The DemoWriteGuard attempt broke eight e2e suites that never touch demo mode: AuthModule listed it as a provider, and RuntimeEnvService was not resolvable inside a TestingModule built around one domain module. AppConfigModule is @Global, but @Global only applies to modules that are in the graph. The five modules whose controllers now inject RuntimeEnvService import it explicitly, the same way PermissionsModule is handled. And both new guards get a test asserting the decorators are actually applied. Once a check moves out of a handler, that handler's own tests stop noticing if the decorator is dropped — the assertions on __guards__ and the @RequireAddon metadata are what keeps the four upload endpoints and the four addon route groups gated, and gated on the right addon.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { CanActivate, ExecutionContext, HttpException, Injectable } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { AddonsService } from './addons.service';
|
||||
import { REQUIRE_ADDON, type RequireAddonMeta } from './require-addon.decorator';
|
||||
|
||||
/**
|
||||
* Enforces @RequireAddon. Replaces the three hand-written addon guards
|
||||
* (collections, airtrail, journey), which differed only in which addon id they
|
||||
* read and which word led the 404 body.
|
||||
*
|
||||
* A handler-level @RequireAddon overrides the controller's, so a route group
|
||||
* can carry one addon and a single endpoint another. Without the decorator the
|
||||
* guard is a no-op — it never gates a route that did not ask for it.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AddonGuard implements CanActivate {
|
||||
constructor(
|
||||
private readonly addons: AddonsService,
|
||||
private readonly reflector: Reflector,
|
||||
) {}
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const meta = this.reflector.getAllAndOverride<RequireAddonMeta | undefined>(REQUIRE_ADDON, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
if (!meta) return true;
|
||||
if (!this.addons.isAddonEnabled(meta.addonId)) {
|
||||
throw new HttpException({ error: `${meta.label} addon is not enabled` }, 404);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AddonsController } from './addons.controller';
|
||||
import { AddonsService } from './addons.service';
|
||||
import { AddonGuard } from './addon.guard';
|
||||
|
||||
/**
|
||||
* GET /api/addons — enabled add-ons + photo providers (was an inline handler in
|
||||
@@ -15,7 +16,7 @@ import { AddonsService } from './addons.service';
|
||||
*/
|
||||
@Module({
|
||||
controllers: [AddonsController],
|
||||
providers: [AddonsService],
|
||||
exports: [AddonsService],
|
||||
providers: [AddonsService, AddonGuard],
|
||||
exports: [AddonsService, AddonGuard],
|
||||
})
|
||||
export class AddonsModule {}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
|
||||
export const REQUIRE_ADDON = 'trek:require-addon';
|
||||
|
||||
export interface RequireAddonMeta {
|
||||
addonId: string;
|
||||
/** Leads the 404 body: `${label} addon is not enabled`. */
|
||||
label: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gate a controller (or a single handler) on a global addon toggle. When the
|
||||
* admin has the addon off, the route answers 404 as if it did not exist.
|
||||
*
|
||||
* Pair it with AddonGuard, and list that guard BEFORE JwtAuthGuard:
|
||||
*
|
||||
* @UseGuards(AddonGuard, JwtAuthGuard)
|
||||
* @RequireAddon(ADDON_IDS.JOURNEY, 'Journey')
|
||||
*
|
||||
* The order is the contract, not a preference — a disabled addon has to answer
|
||||
* 404 to anonymous callers too, so the addon check has to run before the 401.
|
||||
*/
|
||||
export const RequireAddon = (addonId: string, label: string) =>
|
||||
SetMetadata<string, RequireAddonMeta>(REQUIRE_ADDON, { addonId, label });
|
||||
@@ -15,12 +15,13 @@ import {
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../common/demo-write';
|
||||
import { RuntimeEnvService } from '../app-config/runtime-env.service';
|
||||
import { diskStorage } from 'multer';
|
||||
import type { Request, Response } from 'express';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import { readEnv } from '../../app-config';
|
||||
import { AuthService } from './auth.service';
|
||||
import { avatarDir } from './auth.helpers';
|
||||
import {
|
||||
@@ -39,7 +40,6 @@ import { JwtAuthGuard } from './jwt-auth.guard';
|
||||
import { CurrentUser } from './current-user.decorator';
|
||||
import { getClientIp } from '../audit/client-ip';
|
||||
import { AuditService } from '../audit/audit.service';
|
||||
import { isDemoEmail } from '../common/demo';
|
||||
import type { User } from '../../types';
|
||||
|
||||
const WINDOW = 15 * 60 * 1000;
|
||||
@@ -72,7 +72,7 @@ const AVATAR_UPLOAD = {
|
||||
@Controller('api/auth')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class AuthController {
|
||||
constructor(private readonly auth: AuthService, private readonly rl: RateLimitService, private readonly audit: AuditService) {}
|
||||
constructor(private readonly auth: AuthService, private readonly rl: RateLimitService, private readonly audit: AuditService, private readonly env: RuntimeEnvService) {}
|
||||
|
||||
private limit(bucket: string, req: Request, max: number): void {
|
||||
if (!this.rl.check(bucket, req.ip || 'unknown', max, WINDOW, Date.now())) {
|
||||
@@ -145,8 +145,8 @@ export class AuthController {
|
||||
@HttpCode(200)
|
||||
@UseInterceptors(FileInterceptor('avatar', AVATAR_UPLOAD))
|
||||
async avatar(@CurrentUser() user: User, @UploadedFile() file: Express.Multer.File | undefined) {
|
||||
if (readEnv().demo.enabled && isDemoEmail(user.email)) {
|
||||
throw new HttpException({ error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.' }, 403);
|
||||
if (isDemoWriteBlocked(this.env, user.email)) {
|
||||
throw new HttpException(DEMO_WRITE_ERROR, 403);
|
||||
}
|
||||
if (!file) {
|
||||
throw new HttpException({ error: 'No image uploaded' }, 400);
|
||||
|
||||
@@ -5,6 +5,7 @@ import { AuthController } from './auth.controller';
|
||||
import { PasskeyController } from './passkey.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { PasskeyService } from './passkey.service';
|
||||
import { AppConfigModule } from '../app-config/app-config.module';
|
||||
import { AuditModule } from '../audit/audit.module';
|
||||
import { PermissionsModule } from '../permissions/permissions.module';
|
||||
import { AtlasModule } from '../atlas/atlas.module';
|
||||
@@ -23,7 +24,7 @@ import { TripMembershipModule } from '../trip-membership/trip-membership.module'
|
||||
* everything outside the container goes through auth.bridge.ts.
|
||||
*/
|
||||
@Module({
|
||||
imports: [RateLimitModule, AuditModule, PermissionsModule, AtlasModule, TripMembershipModule],
|
||||
imports: [RateLimitModule, AuditModule, PermissionsModule, AtlasModule, TripMembershipModule, AppConfigModule],
|
||||
controllers: [AuthPublicController, AuthController, PasskeyController],
|
||||
providers: [AuthService, PasskeyService],
|
||||
exports: [AuthService, PasskeyService],
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import { CanActivate, HttpException, Injectable } from '@nestjs/common';
|
||||
import { AddonsService } from '../addons/addons.service';
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
|
||||
/**
|
||||
* Mirrors the addon mount gate: when the Collections addon is disabled the whole
|
||||
* route group answers 404, regardless of auth. Declared before JwtAuthGuard so
|
||||
* the addon check wins over the 401, exactly as the Journey addon guard does.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CollectionsAddonGuard implements CanActivate {
|
||||
constructor(private readonly addons: AddonsService) {}
|
||||
|
||||
canActivate(): boolean {
|
||||
if (!this.addons.isAddonEnabled(ADDON_IDS.COLLECTIONS)) {
|
||||
throw new HttpException({ error: 'Collections addon is not enabled' }, 404);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -16,20 +16,22 @@ import {
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../common/demo-write';
|
||||
import { RuntimeEnvService } from '../app-config/runtime-env.service';
|
||||
import type { Request } from 'express';
|
||||
import { diskStorage } from 'multer';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { readEnv } from '../../app-config';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import type { User } from '../../types';
|
||||
import { CollectionsService } from './collections.service';
|
||||
import { CollectionsAddonGuard } from './collections-addon.guard';
|
||||
import { AddonGuard } from '../addons/addon.guard';
|
||||
import { RequireAddon } from '../addons/require-addon.decorator';
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { PLACE_IMAGE_UPLOAD } from '../common/place-image-upload';
|
||||
import { placeImageUrl } from '../../services/placeImage';
|
||||
import { isDemoEmail } from '../common/demo';
|
||||
import {
|
||||
CollectionCreateDto,
|
||||
CollectionUpdateDto,
|
||||
@@ -82,9 +84,10 @@ const COVER_UPLOAD = {
|
||||
* excluded from the WS broadcast.
|
||||
*/
|
||||
@Controller('api/addons/collections')
|
||||
@UseGuards(CollectionsAddonGuard, JwtAuthGuard)
|
||||
@UseGuards(AddonGuard, JwtAuthGuard)
|
||||
@RequireAddon(ADDON_IDS.COLLECTIONS, 'Collections')
|
||||
export class CollectionsController {
|
||||
constructor(private readonly collections: CollectionsService) {}
|
||||
constructor(private readonly collections: CollectionsService, private readonly env: RuntimeEnvService) {}
|
||||
|
||||
// ── Lists ─────────────────────────────────────────────────────────────────
|
||||
@Get()
|
||||
@@ -174,8 +177,8 @@ export class CollectionsController {
|
||||
@UploadedFile() file: Express.Multer.File | undefined,
|
||||
@Headers('x-socket-id') socketId?: string,
|
||||
) {
|
||||
if (readEnv().demo.enabled && isDemoEmail(user.email)) {
|
||||
throw new HttpException({ error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.' }, 403);
|
||||
if (isDemoWriteBlocked(this.env, user.email)) {
|
||||
throw new HttpException(DEMO_WRITE_ERROR, 403);
|
||||
}
|
||||
if (!file) throw new HttpException({ error: 'No image uploaded' }, 400);
|
||||
return this.collections.setPlaceImage(user.id, Number(pid), placeImageUrl(file.filename), socketId);
|
||||
@@ -345,8 +348,8 @@ export class CollectionsController {
|
||||
@Post(':id/cover')
|
||||
@UseInterceptors(FileInterceptor('cover', COVER_UPLOAD))
|
||||
uploadCover(@CurrentUser() user: User, @Param('id') id: string, @UploadedFile() file: Express.Multer.File | undefined, @Headers('x-socket-id') socketId?: string) {
|
||||
if (readEnv().demo.enabled && isDemoEmail(user.email)) {
|
||||
throw new HttpException({ error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.' }, 403);
|
||||
if (isDemoWriteBlocked(this.env, user.email)) {
|
||||
throw new HttpException(DEMO_WRITE_ERROR, 403);
|
||||
}
|
||||
if (!file) throw new HttpException({ error: 'No image uploaded' }, 400);
|
||||
const coverUrl = `/uploads/covers/${file.filename}`;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CollectionsController } from './collections.controller';
|
||||
import { CollectionsService } from './collections.service';
|
||||
import { AppConfigModule } from '../app-config/app-config.module';
|
||||
import { CollectionsMcp } from './collections.mcp';
|
||||
import { CollectionsAddonGuard } from './collections-addon.guard';
|
||||
import { AddonsModule } from '../addons/addons.module';
|
||||
import { PermissionsModule } from '../permissions/permissions.module';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
@@ -11,9 +11,9 @@ import { AuthModule } from '../auth/auth.module';
|
||||
* Exports CollectionsService for in-container consumers (PluginsModule's
|
||||
* RPC host deps factory). */
|
||||
@Module({
|
||||
imports: [AddonsModule, PermissionsModule, AuthModule],
|
||||
imports: [AddonsModule, PermissionsModule, AuthModule, AppConfigModule],
|
||||
controllers: [CollectionsController],
|
||||
providers: [CollectionsService, CollectionsMcp, CollectionsAddonGuard],
|
||||
providers: [CollectionsService, CollectionsMcp],
|
||||
exports: [CollectionsService],
|
||||
})
|
||||
export class CollectionsModule {}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { RuntimeEnvService } from '../app-config/runtime-env.service';
|
||||
import { isDemoEmail } from './demo';
|
||||
|
||||
/** The exact 403 body the six upload endpoints have always returned. */
|
||||
export const DEMO_WRITE_ERROR = {
|
||||
error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.',
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Whether this user's writes are blocked because the instance runs in demo mode.
|
||||
*
|
||||
* The condition was copy-pasted into six upload handlers, each with its own copy
|
||||
* of the message. It lives here once now, and every one of them calls it.
|
||||
*
|
||||
* Deliberately a function and not a guard, though all six sites are handlers and
|
||||
* a guard is the obvious shape. Guards run before the multipart parser, so
|
||||
* throwing from one leaves the request body unread and Node resets the
|
||||
* connection: the client sees ECONNRESET instead of the 403. PROFILE-015 catches
|
||||
* exactly that. Checking inside the handler, after the interceptor has drained
|
||||
* the upload, is what makes the response arrive.
|
||||
*/
|
||||
export function isDemoWriteBlocked(env: RuntimeEnvService, email: string | null | undefined): boolean {
|
||||
return env.isDemoMode() && isDemoEmail(email);
|
||||
}
|
||||
@@ -16,11 +16,12 @@ import {
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../common/demo-write';
|
||||
import { RuntimeEnvService } from '../app-config/runtime-env.service';
|
||||
import { diskStorage } from 'multer';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { readEnv } from '../../app-config';
|
||||
import type { User } from '../../types';
|
||||
import { FilesService } from './files.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
@@ -28,7 +29,6 @@ import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { MAX_FILE_SIZE, MAX_VIDEO_SIZE, BLOCKED_EXTENSIONS, filesDir, isVideoExtension } from './files.constants';
|
||||
import { getAllowedExtensions } from './files.bridge';
|
||||
import { FileUploadDto, FileUpdateDto, FileLinkDto } from './files.dto';
|
||||
import { isDemoEmail } from '../common/demo';
|
||||
|
||||
const UPLOAD = {
|
||||
storage: diskStorage({
|
||||
@@ -69,7 +69,10 @@ const UPLOAD = {
|
||||
@Controller('api/trips/:tripId/files')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class FilesController {
|
||||
constructor(private readonly files: FilesService) {}
|
||||
constructor(
|
||||
private readonly files: FilesService,
|
||||
private readonly env: RuntimeEnvService,
|
||||
) {}
|
||||
|
||||
private requireTrip(tripId: string, user: User) {
|
||||
const trip = this.files.verifyTripAccess(tripId, user.id);
|
||||
@@ -109,8 +112,10 @@ export class FilesController {
|
||||
const cleanup = () => { if (file?.path) { try { fs.unlinkSync(file.path); } catch { /* best-effort */ } } };
|
||||
try {
|
||||
const trip = this.requireTrip(tripId, user);
|
||||
if (readEnv().demo.enabled && isDemoEmail(user.email)) {
|
||||
throw new HttpException({ error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.' }, 403);
|
||||
// Inline rather than DemoWriteGuard: requireTrip above answers 404 for a
|
||||
// trip the caller cannot reach, and a guard would run before it.
|
||||
if (isDemoWriteBlocked(this.env, user.email)) {
|
||||
throw new HttpException(DEMO_WRITE_ERROR, 403);
|
||||
}
|
||||
if (!this.files.can('file_upload', trip, user)) {
|
||||
throw new HttpException({ error: 'No permission to upload files' }, 403);
|
||||
|
||||
@@ -3,9 +3,10 @@ import { FilesController } from './files.controller';
|
||||
import { FilesDownloadController } from './files-download.controller';
|
||||
import { FilesService } from './files.service';
|
||||
import { PermissionsModule } from '../permissions/permissions.module';
|
||||
import { AppConfigModule } from '../app-config/app-config.module';
|
||||
|
||||
@Module({
|
||||
imports: [PermissionsModule],
|
||||
imports: [PermissionsModule, AppConfigModule],
|
||||
controllers: [FilesController, FilesDownloadController],
|
||||
providers: [FilesService],
|
||||
exports: [FilesService],
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
import { CanActivate, HttpException, Injectable } from '@nestjs/common';
|
||||
import { AddonsService } from '../addons/addons.service';
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
|
||||
/**
|
||||
* Gates the AirTrail integration routes on the global `airtrail` addon. When the
|
||||
* admin has it disabled the whole group answers 404. Declared before the
|
||||
* JwtAuthGuard so the addon check wins over the 401 (same ordering as the
|
||||
* Journey addon gate).
|
||||
*/
|
||||
@Injectable()
|
||||
export class AirtrailAddonGuard implements CanActivate {
|
||||
constructor(private readonly addons: AddonsService) {}
|
||||
|
||||
canActivate(): boolean {
|
||||
if (!this.addons.isAddonEnabled(ADDON_IDS.AIRTRAIL)) {
|
||||
throw new HttpException({ error: 'AirTrail addon is not enabled' }, 404);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@ import { Body, Controller, Headers, HttpException, Param, Post, UseGuards } from
|
||||
import type { User } from '../../types';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { AirtrailAddonGuard } from './airtrail-addon.guard';
|
||||
import { AddonGuard } from '../addons/addon.guard';
|
||||
import { RequireAddon } from '../addons/require-addon.decorator';
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
import { AirtrailImportDto } from './airtrail.dto';
|
||||
import type { AirtrailImportResult } from '@trek/shared';
|
||||
import { PermissionsService } from '../permissions/permissions.service';
|
||||
@@ -15,7 +17,8 @@ import { importAirtrailFlights } from '../../services/airtrail/airtrailImport';
|
||||
* flights are re-fetched server-side with the caller's own key.
|
||||
*/
|
||||
@Controller('api/trips/:tripId/reservations/import')
|
||||
@UseGuards(AirtrailAddonGuard, JwtAuthGuard)
|
||||
@UseGuards(AddonGuard, JwtAuthGuard)
|
||||
@RequireAddon(ADDON_IDS.AIRTRAIL, 'AirTrail')
|
||||
export class AirtrailImportController {
|
||||
constructor(
|
||||
private readonly permissions: PermissionsService,
|
||||
|
||||
@@ -3,7 +3,9 @@ import type { Request } from 'express';
|
||||
import type { User } from '../../types';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { AirtrailAddonGuard } from './airtrail-addon.guard';
|
||||
import { AddonGuard } from '../addons/addon.guard';
|
||||
import { RequireAddon } from '../addons/require-addon.decorator';
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
import { AirtrailSettingsDto } from './airtrail.dto';
|
||||
import { getClientIp } from '../audit/client-ip';
|
||||
import {
|
||||
@@ -24,7 +26,8 @@ import { runAirtrailSyncForUser } from '../../services/airtrail/airtrailSync';
|
||||
* gated on the `airtrail` addon (404 when disabled).
|
||||
*/
|
||||
@Controller('api/integrations/airtrail')
|
||||
@UseGuards(AirtrailAddonGuard, JwtAuthGuard)
|
||||
@UseGuards(AddonGuard, JwtAuthGuard)
|
||||
@RequireAddon(ADDON_IDS.AIRTRAIL, 'AirTrail')
|
||||
export class AirtrailController {
|
||||
@Get('settings')
|
||||
getSettings(@CurrentUser() user: User) {
|
||||
|
||||
@@ -3,7 +3,6 @@ import { AirtrailController } from './airtrail.controller';
|
||||
import { AirtrailImportController } from './airtrail-import.controller';
|
||||
import { PermissionsModule } from '../permissions/permissions.module';
|
||||
import { AddonsModule } from '../addons/addons.module';
|
||||
import { AirtrailAddonGuard } from './airtrail-addon.guard';
|
||||
|
||||
/**
|
||||
* AirTrail integration domain. The connection lives under
|
||||
@@ -14,6 +13,5 @@ import { AirtrailAddonGuard } from './airtrail-addon.guard';
|
||||
@Module({
|
||||
imports: [PermissionsModule, AddonsModule],
|
||||
controllers: [AirtrailController, AirtrailImportController],
|
||||
providers: [AirtrailAddonGuard],
|
||||
})
|
||||
export class AirtrailModule {}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import { CanActivate, HttpException, Injectable } from '@nestjs/common';
|
||||
import { JourneyService } from './journey.service';
|
||||
|
||||
/**
|
||||
* Mirrors the legacy `/api/journeys` mount gate: when the Journey addon is
|
||||
* disabled the whole route group answers 404, regardless of auth. Declared
|
||||
* before the JwtAuthGuard so the addon check wins over the 401, exactly as the
|
||||
* Express middleware ordering did.
|
||||
*/
|
||||
@Injectable()
|
||||
export class JourneyAddonGuard implements CanActivate {
|
||||
constructor(private readonly journey: JourneyService) {}
|
||||
|
||||
canActivate(): boolean {
|
||||
if (!this.journey.journeyAddonEnabled()) {
|
||||
throw new HttpException({ error: 'Journey addon is not enabled' }, 404);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,9 @@ import fs from 'node:fs';
|
||||
import crypto from 'node:crypto';
|
||||
import type { User } from '../../types';
|
||||
import { JourneyService } from './journey.service';
|
||||
import { JourneyAddonGuard } from './journey-addon.guard';
|
||||
import { AddonGuard } from '../addons/addon.guard';
|
||||
import { RequireAddon } from '../addons/require-addon.decorator';
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { isVideoMime, isVideoExtension, MAX_VIDEO_SIZE } from '../files/files.constants';
|
||||
@@ -99,7 +101,8 @@ const VIDEO_UPLOAD = {
|
||||
* are declared before /:id so they win over the param.
|
||||
*/
|
||||
@Controller('api/journeys')
|
||||
@UseGuards(JourneyAddonGuard, JwtAuthGuard)
|
||||
@UseGuards(AddonGuard, JwtAuthGuard)
|
||||
@RequireAddon(ADDON_IDS.JOURNEY, 'Journey')
|
||||
export class JourneyController {
|
||||
constructor(private readonly journey: JourneyService) {}
|
||||
|
||||
|
||||
@@ -2,12 +2,11 @@ import { Module } from '@nestjs/common';
|
||||
import { JourneyController } from './journey.controller';
|
||||
import { JourneyPublicController } from './journey-public.controller';
|
||||
import { JourneyService } from './journey.service';
|
||||
import { JourneyAddonGuard } from './journey-addon.guard';
|
||||
import { AddonsModule } from '../addons/addons.module';
|
||||
|
||||
@Module({
|
||||
imports: [AddonsModule],
|
||||
controllers: [JourneyController, JourneyPublicController],
|
||||
providers: [JourneyService, JourneyAddonGuard],
|
||||
providers: [JourneyService],
|
||||
})
|
||||
export class JourneyModule {}
|
||||
|
||||
@@ -15,9 +15,10 @@ import {
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../common/demo-write';
|
||||
import { RuntimeEnvService } from '../app-config/runtime-env.service';
|
||||
import { memoryStorage } from 'multer';
|
||||
import { hexColorSchema } from '@trek/shared';
|
||||
import { readEnv } from '../../app-config';
|
||||
import type { User } from '../../types';
|
||||
import { PlacesService } from './places.service';
|
||||
import { isUpdateConflict } from '../common/conflictResult';
|
||||
@@ -25,7 +26,6 @@ import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { PLACE_IMAGE_UPLOAD } from '../common/place-image-upload';
|
||||
import { placeImageUrl } from '../../services/placeImage';
|
||||
import { isDemoEmail } from '../common/demo';
|
||||
import {
|
||||
PlaceBulkDeleteDto,
|
||||
PlaceBulkUpdateDto,
|
||||
@@ -86,7 +86,10 @@ function parseBool(v: unknown, defaultVal: boolean): boolean {
|
||||
@Controller('api/trips/:tripId/places')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class PlacesController {
|
||||
constructor(private readonly places: PlacesService) {}
|
||||
constructor(
|
||||
private readonly places: PlacesService,
|
||||
private readonly env: RuntimeEnvService,
|
||||
) {}
|
||||
|
||||
private requireTrip(tripId: string, user: User) {
|
||||
const trip = this.places.verifyTripAccess(tripId, user.id);
|
||||
@@ -309,8 +312,10 @@ export class PlacesController {
|
||||
) {
|
||||
const trip = this.requireTrip(tripId, user);
|
||||
this.requireEdit(trip, user);
|
||||
if (readEnv().demo.enabled && isDemoEmail(user.email)) {
|
||||
throw new HttpException({ error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.' }, 403);
|
||||
// Inline rather than DemoWriteGuard: requireEdit above answers 404/403 for a
|
||||
// trip the caller cannot reach, and a guard would run before it.
|
||||
if (isDemoWriteBlocked(this.env, user.email)) {
|
||||
throw new HttpException(DEMO_WRITE_ERROR, 403);
|
||||
}
|
||||
if (!file) {
|
||||
throw new HttpException({ error: 'No image uploaded' }, 400);
|
||||
|
||||
@@ -3,6 +3,7 @@ import { PlacesController } from './places.controller';
|
||||
import { PlacesService } from './places.service';
|
||||
import { PlacesMcp } from './places.mcp';
|
||||
import { PermissionsModule } from '../permissions/permissions.module';
|
||||
import { AppConfigModule } from '../app-config/app-config.module';
|
||||
import { QueryHelpersModule } from '../query-helpers/query-helpers.module';
|
||||
import { MapsModule } from '../maps/maps.module';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
@@ -16,7 +17,7 @@ import { AuthModule } from '../auth/auth.module';
|
||||
* is no places.bridge.ts: nothing outside the container consumes this domain.
|
||||
*/
|
||||
@Module({
|
||||
imports: [PermissionsModule, QueryHelpersModule, MapsModule, AuthModule],
|
||||
imports: [PermissionsModule, QueryHelpersModule, MapsModule, AuthModule, AppConfigModule],
|
||||
controllers: [PlacesController],
|
||||
providers: [PlacesService, PlacesMcp],
|
||||
exports: [PlacesService],
|
||||
|
||||
@@ -17,11 +17,12 @@ import {
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../common/demo-write';
|
||||
import { RuntimeEnvService } from '../app-config/runtime-env.service';
|
||||
import type { Request, Response } from 'express';
|
||||
import { diskStorage } from 'multer';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { readEnv } from '../../app-config';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import type { User } from '../../types';
|
||||
import { TripsService } from './trips.service';
|
||||
@@ -30,7 +31,6 @@ import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { getClientIp } from '../audit/client-ip';
|
||||
import { logInfo } from '../audit/audit-log.logger';
|
||||
import { AuditService } from '../audit/audit.service';
|
||||
import { isDemoEmail } from '../common/demo';
|
||||
import { NotFoundError, ValidationError } from './trips.service';
|
||||
import { TripCreateDto, TripUpdateDto, TripCopyDto, TripAddMemberDto, TripTransferOwnershipDto, TripCreateGuestDto, TripRenameGuestDto } from './trips.dto';
|
||||
import { saveUnsplashCover, isUnsplashCoverUrl } from '../../services/unsplashService';
|
||||
@@ -69,7 +69,7 @@ const addDays = (d: Date, n: number) => { const r = new Date(d); r.setDate(r.get
|
||||
@Controller('api/trips')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class TripsController {
|
||||
constructor(private readonly trips: TripsService, private readonly audit: AuditService) {}
|
||||
constructor(private readonly trips: TripsService, private readonly audit: AuditService, private readonly env: RuntimeEnvService) {}
|
||||
|
||||
@Get()
|
||||
list(@CurrentUser() user: User, @Query('archived') archived?: string) {
|
||||
@@ -179,8 +179,8 @@ export class TripsController {
|
||||
@Post(':id/cover')
|
||||
@UseInterceptors(FileInterceptor('cover', COVER_UPLOAD))
|
||||
cover(@CurrentUser() user: User, @Param('id') id: string, @UploadedFile() file: Express.Multer.File | undefined) {
|
||||
if (readEnv().demo.enabled && isDemoEmail(user.email)) {
|
||||
throw new HttpException({ error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.' }, 403);
|
||||
if (isDemoWriteBlocked(this.env, user.email)) {
|
||||
throw new HttpException(DEMO_WRITE_ERROR, 403);
|
||||
}
|
||||
const access = this.trips.canAccessTrip(id, user.id);
|
||||
if (!access?.user_id) {
|
||||
|
||||
@@ -12,13 +12,14 @@ import { AuditModule } from '../audit/audit.module';
|
||||
import { PlacesModule } from '../places/places.module';
|
||||
import { TripsController } from './trips.controller';
|
||||
import { TripsService } from './trips.service';
|
||||
import { AppConfigModule } from '../app-config/app-config.module';
|
||||
import { TripsMcp } from './trips.mcp';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
|
||||
/** Trips aggregate root (C1 — Phase 3). Uses exact strangler prefixes so it does
|
||||
* not capture the nested sub-domain mounts (collab, files, ...). */
|
||||
@Module({
|
||||
imports: [TodoModule, PackingModule, FilesModule, ReservationsModule, DaysModule, PermissionsModule, AuditModule, BudgetModule, CollabModule, VacayModule, PlacesModule, AuthModule],
|
||||
imports: [TodoModule, PackingModule, FilesModule, ReservationsModule, DaysModule, PermissionsModule, AuditModule, BudgetModule, CollabModule, VacayModule, PlacesModule, AuthModule, AppConfigModule],
|
||||
controllers: [TripsController],
|
||||
providers: [TripsService, TripsMcp],
|
||||
// Exported for FeedsModule (ICS feeds) and PluginsModule (RPC host injection).
|
||||
|
||||
Reference in New Issue
Block a user