Skip to content

Commit

Permalink
feat(server): apply ValidationPipe on controllers (immich-app#2137)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelheusschen authored Mar 31, 2023
1 parent 49f66be commit 51785a1
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 45 deletions.
8 changes: 3 additions & 5 deletions server/apps/immich/src/controllers/album.controller.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { AlbumService, AuthUserDto } from '@app/domain';
import { GetAlbumsDto } from '@app/domain/album/dto/get-albums.dto';
import { Controller, Get, Query, ValidationPipe } from '@nestjs/common';
import { Controller, Get, Query, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { GetAuthUser } from '../decorators/auth-user.decorator';
import { Authenticated } from '../decorators/authenticated.decorator';

@ApiTags('Album')
@Controller('album')
@Authenticated()
@UsePipes(new ValidationPipe({ transform: true }))
export class AlbumController {
constructor(private service: AlbumService) {}

@Get()
async getAllAlbums(
@GetAuthUser() authUser: AuthUserDto,
@Query(new ValidationPipe({ transform: true })) query: GetAlbumsDto,
) {
async getAllAlbums(@GetAuthUser() authUser: AuthUserDto, @Query() query: GetAlbumsDto) {
return this.service.getAllAlbums(authUser, query);
}
}
10 changes: 4 additions & 6 deletions server/apps/immich/src/controllers/api-key.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import {
APIKeyUpdateDto,
AuthUserDto,
} from '@app/domain';
import { Body, Controller, Delete, Get, Param, Post, Put, ValidationPipe } from '@nestjs/common';
import { Body, Controller, Delete, Get, Param, Post, Put, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { GetAuthUser } from '../decorators/auth-user.decorator';
import { Authenticated } from '../decorators/authenticated.decorator';

@ApiTags('API Key')
@Controller('api-key')
@Authenticated()
@UsePipes(new ValidationPipe({ transform: true }))
export class APIKeyController {
constructor(private service: APIKeyService) {}

@Post()
createKey(
@GetAuthUser() authUser: AuthUserDto,
@Body(ValidationPipe) dto: APIKeyCreateDto,
): Promise<APIKeyCreateResponseDto> {
createKey(@GetAuthUser() authUser: AuthUserDto, @Body() dto: APIKeyCreateDto): Promise<APIKeyCreateResponseDto> {
return this.service.create(authUser, dto);
}

Expand All @@ -39,7 +37,7 @@ export class APIKeyController {
updateKey(
@GetAuthUser() authUser: AuthUserDto,
@Param('id') id: string,
@Body(ValidationPipe) dto: APIKeyUpdateDto,
@Body() dto: APIKeyUpdateDto,
): Promise<APIKeyResponseDto> {
return this.service.update(authUser, id, dto);
}
Expand Down
9 changes: 4 additions & 5 deletions server/apps/immich/src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ import {
UserResponseDto,
ValidateAccessTokenResponseDto,
} from '@app/domain';
import { Body, Controller, Ip, Post, Req, Res, ValidationPipe } from '@nestjs/common';
import { Body, Controller, Ip, Post, Req, Res, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiBadRequestResponse, ApiTags } from '@nestjs/swagger';
import { Request, Response } from 'express';
import { GetAuthUser } from '../decorators/auth-user.decorator';
import { Authenticated } from '../decorators/authenticated.decorator';

@ApiTags('Authentication')
@Controller('auth')
@UsePipes(new ValidationPipe({ transform: true }))
export class AuthController {
constructor(private readonly service: AuthService) {}

@Post('login')
async login(
@Body(new ValidationPipe({ transform: true })) loginCredential: LoginCredentialDto,
@Body() loginCredential: LoginCredentialDto,
@Ip() clientIp: string,
@Req() req: Request,
@Res({ passthrough: true }) res: Response,
Expand All @@ -38,9 +39,7 @@ export class AuthController {

@Post('admin-sign-up')
@ApiBadRequestResponse({ description: 'The server already has an admin' })
adminSignUp(
@Body(new ValidationPipe({ transform: true })) signUpCredential: SignUpDto,
): Promise<AdminSignupResponseDto> {
adminSignUp(@Body() signUpCredential: SignUpDto): Promise<AdminSignupResponseDto> {
return this.service.adminSignUp(signUpCredential);
}

Expand Down
5 changes: 3 additions & 2 deletions server/apps/immich/src/controllers/device-info.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import {
DeviceInfoService,
UpsertDeviceInfoDto as UpsertDto,
} from '@app/domain';
import { Body, Controller, Put, ValidationPipe } from '@nestjs/common';
import { Body, Controller, Put, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { GetAuthUser } from '../decorators/auth-user.decorator';
import { Authenticated } from '../decorators/authenticated.decorator';

@ApiTags('Device Info')
@Controller('device-info')
@Authenticated()
@UsePipes(new ValidationPipe({ transform: true }))
export class DeviceInfoController {
constructor(private readonly service: DeviceInfoService) {}

@Put()
upsertDeviceInfo(@GetAuthUser() authUser: AuthUserDto, @Body(ValidationPipe) dto: UpsertDto): Promise<ResponseDto> {
upsertDeviceInfo(@GetAuthUser() authUser: AuthUserDto, @Body() dto: UpsertDto): Promise<ResponseDto> {
return this.service.upsert(authUser, dto);
}
}
5 changes: 3 additions & 2 deletions server/apps/immich/src/controllers/job.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { AllJobStatusResponseDto, JobCommandDto, JobIdDto, JobService } from '@app/domain';
import { Body, Controller, Get, Param, Put, ValidationPipe } from '@nestjs/common';
import { Body, Controller, Get, Param, Put, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Authenticated } from '../decorators/authenticated.decorator';

@ApiTags('Job')
@Controller('jobs')
@Authenticated({ admin: true })
@UsePipes(new ValidationPipe({ transform: true }))
export class JobController {
constructor(private service: JobService) {}

Expand All @@ -15,7 +16,7 @@ export class JobController {
}

@Put('/:jobId')
sendJobCommand(@Param(ValidationPipe) { jobId }: JobIdDto, @Body(ValidationPipe) dto: JobCommandDto): Promise<void> {
sendJobCommand(@Param() { jobId }: JobIdDto, @Body() dto: JobCommandDto): Promise<void> {
return this.service.handleCommand(jobId, dto);
}
}
9 changes: 5 additions & 4 deletions server/apps/immich/src/controllers/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {
OAuthService,
UserResponseDto,
} from '@app/domain';
import { Body, Controller, Get, HttpStatus, Post, Redirect, Req, Res, ValidationPipe } from '@nestjs/common';
import { Body, Controller, Get, HttpStatus, Post, Redirect, Req, Res, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Request, Response } from 'express';
import { GetAuthUser } from '../decorators/auth-user.decorator';
import { Authenticated } from '../decorators/authenticated.decorator';

@ApiTags('OAuth')
@Controller('oauth')
@UsePipes(new ValidationPipe({ transform: true }))
export class OAuthController {
constructor(private service: OAuthService) {}

Expand All @@ -28,14 +29,14 @@ export class OAuthController {
}

@Post('config')
generateConfig(@Body(ValidationPipe) dto: OAuthConfigDto): Promise<OAuthConfigResponseDto> {
generateConfig(@Body() dto: OAuthConfigDto): Promise<OAuthConfigResponseDto> {
return this.service.generateConfig(dto);
}

@Post('callback')
async callback(
@Res({ passthrough: true }) res: Response,
@Body(ValidationPipe) dto: OAuthCallbackDto,
@Body() dto: OAuthCallbackDto,
@Req() req: Request,
): Promise<LoginResponseDto> {
const { response, cookie } = await this.service.login(dto, req.secure);
Expand All @@ -45,7 +46,7 @@ export class OAuthController {

@Authenticated()
@Post('link')
link(@GetAuthUser() authUser: AuthUserDto, @Body(ValidationPipe) dto: OAuthCallbackDto): Promise<UserResponseDto> {
link(@GetAuthUser() authUser: AuthUserDto, @Body() dto: OAuthCallbackDto): Promise<UserResponseDto> {
return this.service.link(authUser, dto);
}

Expand Down
8 changes: 3 additions & 5 deletions server/apps/immich/src/controllers/search.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import {
SearchResponseDto,
SearchService,
} from '@app/domain';
import { Controller, Get, Query, ValidationPipe } from '@nestjs/common';
import { Controller, Get, Query, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { GetAuthUser } from '../decorators/auth-user.decorator';
import { Authenticated } from '../decorators/authenticated.decorator';

@ApiTags('Search')
@Controller('search')
@Authenticated()
@UsePipes(new ValidationPipe({ transform: true }))
export class SearchController {
constructor(private service: SearchService) {}

@Get()
search(
@GetAuthUser() authUser: AuthUserDto,
@Query(new ValidationPipe({ transform: true })) dto: SearchDto,
): Promise<SearchResponseDto> {
search(@GetAuthUser() authUser: AuthUserDto, @Query() dto: SearchDto): Promise<SearchResponseDto> {
return this.service.search(authUser, dto);
}

Expand Down
3 changes: 2 additions & 1 deletion server/apps/immich/src/controllers/server-info.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {
ServerStatsResponseDto,
ServerVersionReponseDto,
} from '@app/domain';
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Authenticated } from '../decorators/authenticated.decorator';

@ApiTags('Server Info')
@Controller('server-info')
@UsePipes(new ValidationPipe({ transform: true }))
export class ServerInfoController {
constructor(private service: ServerInfoService) {}

Expand Down
5 changes: 3 additions & 2 deletions server/apps/immich/src/controllers/share.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { AuthUserDto, EditSharedLinkDto, SharedLinkResponseDto, ShareService } from '@app/domain';
import { Body, Controller, Delete, Get, Param, Patch, ValidationPipe } from '@nestjs/common';
import { Body, Controller, Delete, Get, Param, Patch, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { GetAuthUser } from '../decorators/auth-user.decorator';
import { Authenticated } from '../decorators/authenticated.decorator';

@ApiTags('share')
@Controller('share')
@UsePipes(new ValidationPipe({ transform: true }))
export class ShareController {
constructor(private readonly service: ShareService) {}

Expand Down Expand Up @@ -38,7 +39,7 @@ export class ShareController {
editSharedLink(
@GetAuthUser() authUser: AuthUserDto,
@Param('id') id: string,
@Body(ValidationPipe) dto: EditSharedLinkDto,
@Body() dto: EditSharedLinkDto,
): Promise<SharedLinkResponseDto> {
return this.service.edit(authUser, id, dto);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { SystemConfigDto, SystemConfigService, SystemConfigTemplateStorageOptionDto } from '@app/domain';
import { Body, Controller, Get, Put, ValidationPipe } from '@nestjs/common';
import { Body, Controller, Get, Put, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Authenticated } from '../decorators/authenticated.decorator';

@ApiTags('System Config')
@Controller('system-config')
@Authenticated({ admin: true })
@UsePipes(new ValidationPipe({ transform: true }))
export class SystemConfigController {
constructor(private readonly service: SystemConfigService) {}

Expand All @@ -20,7 +21,7 @@ export class SystemConfigController {
}

@Put()
updateConfig(@Body(ValidationPipe) dto: SystemConfigDto): Promise<SystemConfigDto> {
updateConfig(@Body() dto: SystemConfigDto): Promise<SystemConfigDto> {
return this.service.updateConfig(dto);
}

Expand Down
17 changes: 6 additions & 11 deletions server/apps/immich/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
UseInterceptors,
UploadedFile,
Response,
ParseBoolPipe,
StreamableFile,
Header,
UsePipes,
} from '@nestjs/common';
import { UserService } from '@app/domain';
import { Authenticated } from '../decorators/authenticated.decorator';
Expand All @@ -32,15 +32,13 @@ import { UserCountDto } from '@app/domain';

@ApiTags('User')
@Controller('user')
@UsePipes(new ValidationPipe({ transform: true }))
export class UserController {
constructor(private service: UserService) {}

@Authenticated()
@Get()
getAllUsers(
@GetAuthUser() authUser: AuthUserDto,
@Query('isAll', ParseBoolPipe) isAll: boolean,
): Promise<UserResponseDto[]> {
getAllUsers(@GetAuthUser() authUser: AuthUserDto, @Query('isAll') isAll: boolean): Promise<UserResponseDto[]> {
return this.service.getAllUsers(authUser, isAll);
}

Expand All @@ -58,12 +56,12 @@ export class UserController {

@Authenticated({ admin: true })
@Post()
createUser(@Body(new ValidationPipe({ transform: true })) createUserDto: CreateUserDto): Promise<UserResponseDto> {
createUser(@Body() createUserDto: CreateUserDto): Promise<UserResponseDto> {
return this.service.createUser(createUserDto);
}

@Get('/count')
getUserCount(@Query(new ValidationPipe({ transform: true })) dto: UserCountDto): Promise<UserCountResponseDto> {
getUserCount(@Query() dto: UserCountDto): Promise<UserCountResponseDto> {
return this.service.getUserCount(dto);
}

Expand All @@ -81,10 +79,7 @@ export class UserController {

@Authenticated()
@Put()
updateUser(
@GetAuthUser() authUser: AuthUserDto,
@Body(ValidationPipe) updateUserDto: UpdateUserDto,
): Promise<UserResponseDto> {
updateUser(@GetAuthUser() authUser: AuthUserDto, @Body() updateUserDto: UpdateUserDto): Promise<UserResponseDto> {
return this.service.updateUser(authUser, updateUserDto);
}

Expand Down

0 comments on commit 51785a1

Please sign in to comment.