mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 11:16:31 -05:00
feat(backend): add RegistrationDisabledError
This error is thrown by RegistrationEnabledGuard instead of directly throwing an http error. The new RegistrationDisabledError is mapped to the Forbidden HTTP code 403, since this better represents the actual error. Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
50e3452574
commit
c39a9430a2
4 changed files with 17 additions and 12 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
@ -53,7 +53,7 @@ export class AuthController {
|
|||
|
||||
@UseGuards(RegistrationEnabledGuard)
|
||||
@Post('local')
|
||||
@OpenApi(201, 400, 409)
|
||||
@OpenApi(201, 400, 403, 409)
|
||||
async registerUser(
|
||||
@Req() request: RequestWithSession,
|
||||
@Body() registerDto: RegisterDto,
|
||||
|
|
|
@ -1,16 +1,12 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import {
|
||||
BadRequestException,
|
||||
CanActivate,
|
||||
Inject,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import { CanActivate, Inject, Injectable } from '@nestjs/common';
|
||||
|
||||
import authConfiguration, { AuthConfig } from '../../config/auth.config';
|
||||
import { RegistrationDisabledError } from '../../errors/errors';
|
||||
import { ConsoleLoggerService } from '../../logger/console-logger.service';
|
||||
|
||||
@Injectable()
|
||||
|
@ -26,7 +22,7 @@ export class RegistrationEnabledGuard implements CanActivate {
|
|||
canActivate(): boolean {
|
||||
if (!this.authConfig.local.enableRegister) {
|
||||
this.logger.debug('User registration is disabled.', 'canActivate');
|
||||
throw new BadRequestException('User registration is disabled.');
|
||||
throw new RegistrationDisabledError();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
@ -8,6 +8,7 @@ import {
|
|||
BadRequestException,
|
||||
Catch,
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
InternalServerErrorException,
|
||||
NotFoundException,
|
||||
PayloadTooLargeException,
|
||||
|
@ -75,6 +76,10 @@ const mapOfHedgeDocErrorsToHttpErrors: Map<string, HttpExceptionConstructor> =
|
|||
'MaximumDocumentLengthExceededError',
|
||||
(object): HttpException => new PayloadTooLargeException(object),
|
||||
],
|
||||
[
|
||||
'RegistrationDisabledError',
|
||||
(object): HttpException => new ForbiddenException(object),
|
||||
],
|
||||
]);
|
||||
|
||||
@Catch()
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
@ -59,3 +59,7 @@ export class PasswordTooWeakError extends Error {
|
|||
export class MaximumDocumentLengthExceededError extends Error {
|
||||
name = 'MaximumDocumentLengthExceededError';
|
||||
}
|
||||
|
||||
export class RegistrationDisabledError extends Error {
|
||||
name = 'RegistrationDisabledError';
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue