mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
refactor(nest): inject DatabaseService in access-check services and controllers
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Controller, Get, Param, Req, UseGuards } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import { canAccessTrip } from '../../db/database';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { pluginsEnabled } from './kill-switch';
|
||||
import { PluginRuntimeService } from './plugin-runtime.service';
|
||||
@@ -145,7 +145,10 @@ function normalize(pluginId: string, raw: unknown): MapLayer[] {
|
||||
@Controller('api/map-layers')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class MapLayersController {
|
||||
constructor(private readonly runtime: PluginRuntimeService) {}
|
||||
constructor(
|
||||
private readonly runtime: PluginRuntimeService,
|
||||
private readonly dbs: DatabaseService,
|
||||
) {}
|
||||
|
||||
@Get(':tripId')
|
||||
async get(
|
||||
@@ -155,7 +158,7 @@ export class MapLayersController {
|
||||
if (!pluginsEnabled()) return { layers: [] };
|
||||
const tripId = Number(tripIdRaw);
|
||||
const userId = req.user?.id;
|
||||
if (!Number.isFinite(tripId) || userId == null || !canAccessTrip(tripId, userId)) return { layers: [] };
|
||||
if (!Number.isFinite(tripId) || userId == null || !this.dbs.canAccessTrip(tripId, userId)) return { layers: [] };
|
||||
|
||||
const ids = this.runtime.providersOf('mapLayerProvider');
|
||||
const perProvider = await Promise.all(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Controller, Get, Param, Req, UseGuards } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import { canAccessTrip } from '../../db/database';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { pluginsEnabled } from './kill-switch';
|
||||
import { PluginRuntimeService } from './plugin-runtime.service';
|
||||
@@ -78,7 +78,10 @@ function normalize(pluginId: string, raw: unknown): MapMarker[] {
|
||||
@Controller('api/map-markers')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class MapMarkersController {
|
||||
constructor(private readonly runtime: PluginRuntimeService) {}
|
||||
constructor(
|
||||
private readonly runtime: PluginRuntimeService,
|
||||
private readonly dbs: DatabaseService,
|
||||
) {}
|
||||
|
||||
@Get(':tripId')
|
||||
async get(
|
||||
@@ -88,7 +91,7 @@ export class MapMarkersController {
|
||||
if (!pluginsEnabled()) return { markers: [] };
|
||||
const tripId = Number(tripIdRaw);
|
||||
const userId = req.user?.id;
|
||||
if (!Number.isFinite(tripId) || userId == null || !canAccessTrip(tripId, userId)) return { markers: [] };
|
||||
if (!Number.isFinite(tripId) || userId == null || !this.dbs.canAccessTrip(tripId, userId)) return { markers: [] };
|
||||
|
||||
const ids = this.runtime.providersOf('mapMarkerProvider');
|
||||
const perProvider = await Promise.all(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Controller, Get, Param, Req, UseGuards } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import { canAccessTrip } from '../../db/database';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { pluginsEnabled } from './kill-switch';
|
||||
import { PluginRuntimeService } from './plugin-runtime.service';
|
||||
@@ -74,7 +74,10 @@ function normalize(pluginId: string, raw: unknown): PdfSection[] {
|
||||
@Controller('api/pdf-sections')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class PdfSectionsController {
|
||||
constructor(private readonly runtime: PluginRuntimeService) {}
|
||||
constructor(
|
||||
private readonly runtime: PluginRuntimeService,
|
||||
private readonly dbs: DatabaseService,
|
||||
) {}
|
||||
|
||||
@Get(':tripId')
|
||||
async get(
|
||||
@@ -84,7 +87,7 @@ export class PdfSectionsController {
|
||||
if (!pluginsEnabled()) return { sections: [] };
|
||||
const tripId = Number(tripIdRaw);
|
||||
const userId = req.user?.id;
|
||||
if (!Number.isFinite(tripId) || userId == null || !canAccessTrip(tripId, userId)) return { sections: [] };
|
||||
if (!Number.isFinite(tripId) || userId == null || !this.dbs.canAccessTrip(tripId, userId)) return { sections: [] };
|
||||
|
||||
const ids = this.runtime.providersOf('pdfSectionProvider');
|
||||
const perProvider = await Promise.all(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Controller, Get, Query, Req, UseGuards } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import { canAccessTrip } from '../../db/database';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { pluginsEnabled } from './kill-switch';
|
||||
import { PluginRuntimeService } from './plugin-runtime.service';
|
||||
@@ -82,7 +82,10 @@ function normalize(pluginId: string, raw: unknown, allowed: Set<number>): TripCa
|
||||
@Controller('api/trip-card-contributions')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class TripCardContributionsController {
|
||||
constructor(private readonly runtime: PluginRuntimeService) {}
|
||||
constructor(
|
||||
private readonly runtime: PluginRuntimeService,
|
||||
private readonly dbs: DatabaseService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
async get(
|
||||
@@ -100,7 +103,7 @@ export class TripCardContributionsController {
|
||||
.map((s) => Number(s.trim()))
|
||||
.filter((n) => Number.isInteger(n) && n > 0)
|
||||
.slice(0, MAX_TRIP_IDS);
|
||||
const accessible = [...new Set(requested)].filter((id) => canAccessTrip(id, userId));
|
||||
const accessible = [...new Set(requested)].filter((id) => this.dbs.canAccessTrip(id, userId));
|
||||
if (accessible.length === 0) return { contributions: [] };
|
||||
const allowed = new Set(accessible);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Controller, Get, Param, Req, UseGuards } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import { canAccessTrip } from '../../db/database';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { pluginsEnabled } from './kill-switch';
|
||||
import { PluginRuntimeService } from './plugin-runtime.service';
|
||||
@@ -27,7 +27,10 @@ const MESSAGE_MAX = 300;
|
||||
@Controller('api/trip-warnings')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class TripWarningsController {
|
||||
constructor(private readonly runtime: PluginRuntimeService) {}
|
||||
constructor(
|
||||
private readonly runtime: PluginRuntimeService,
|
||||
private readonly dbs: DatabaseService,
|
||||
) {}
|
||||
|
||||
@Get(':tripId')
|
||||
async get(
|
||||
@@ -37,7 +40,7 @@ export class TripWarningsController {
|
||||
if (!pluginsEnabled()) return { warnings: [] };
|
||||
const tripId = Number(tripIdRaw);
|
||||
const userId = req.user?.id;
|
||||
if (!Number.isFinite(tripId) || userId == null || !canAccessTrip(tripId, userId)) return { warnings: [] };
|
||||
if (!Number.isFinite(tripId) || userId == null || !this.dbs.canAccessTrip(tripId, userId)) return { warnings: [] };
|
||||
|
||||
const ids = this.runtime.providersOf('warningProvider');
|
||||
const perProvider = await Promise.all(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Controller, Get, Param, Req, UseGuards } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import { canAccessTrip } from '../../db/database';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { pluginsEnabled } from './kill-switch';
|
||||
import { PluginRuntimeService } from './plugin-runtime.service';
|
||||
@@ -102,7 +102,10 @@ function normalize(pluginId: string, raw: unknown): Contribution[] {
|
||||
@Controller('api/view-contributions')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class ViewContributionsController {
|
||||
constructor(private readonly runtime: PluginRuntimeService) {}
|
||||
constructor(
|
||||
private readonly runtime: PluginRuntimeService,
|
||||
private readonly dbs: DatabaseService,
|
||||
) {}
|
||||
|
||||
@Get(':view/:tripId')
|
||||
async get(
|
||||
@@ -113,7 +116,7 @@ export class ViewContributionsController {
|
||||
if (!pluginsEnabled() || !VIEWS.has(view)) return { contributions: [] };
|
||||
const tripId = Number(tripIdRaw);
|
||||
const userId = req.user?.id;
|
||||
if (!Number.isFinite(tripId) || userId == null || !canAccessTrip(tripId, userId)) return { contributions: [] };
|
||||
if (!Number.isFinite(tripId) || userId == null || !this.dbs.canAccessTrip(tripId, userId)) return { contributions: [] };
|
||||
|
||||
const ids = this.runtime.providersOf('tableContributor');
|
||||
const perProvider = await Promise.all(
|
||||
|
||||
Reference in New Issue
Block a user