mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-22 09:46:30 -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
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
@ -53,7 +53,7 @@ export class AuthController {
|
||||||
|
|
||||||
@UseGuards(RegistrationEnabledGuard)
|
@UseGuards(RegistrationEnabledGuard)
|
||||||
@Post('local')
|
@Post('local')
|
||||||
@OpenApi(201, 400, 409)
|
@OpenApi(201, 400, 403, 409)
|
||||||
async registerUser(
|
async registerUser(
|
||||||
@Req() request: RequestWithSession,
|
@Req() request: RequestWithSession,
|
||||||
@Body() registerDto: RegisterDto,
|
@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
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
import {
|
import { CanActivate, Inject, Injectable } from '@nestjs/common';
|
||||||
BadRequestException,
|
|
||||||
CanActivate,
|
|
||||||
Inject,
|
|
||||||
Injectable,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
|
|
||||||
import authConfiguration, { AuthConfig } from '../../config/auth.config';
|
import authConfiguration, { AuthConfig } from '../../config/auth.config';
|
||||||
|
import { RegistrationDisabledError } from '../../errors/errors';
|
||||||
import { ConsoleLoggerService } from '../../logger/console-logger.service';
|
import { ConsoleLoggerService } from '../../logger/console-logger.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
@ -26,7 +22,7 @@ export class RegistrationEnabledGuard implements CanActivate {
|
||||||
canActivate(): boolean {
|
canActivate(): boolean {
|
||||||
if (!this.authConfig.local.enableRegister) {
|
if (!this.authConfig.local.enableRegister) {
|
||||||
this.logger.debug('User registration is disabled.', 'canActivate');
|
this.logger.debug('User registration is disabled.', 'canActivate');
|
||||||
throw new BadRequestException('User registration is disabled.');
|
throw new RegistrationDisabledError();
|
||||||
}
|
}
|
||||||
return true;
|
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
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
@ -8,6 +8,7 @@ import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
Catch,
|
Catch,
|
||||||
ConflictException,
|
ConflictException,
|
||||||
|
ForbiddenException,
|
||||||
InternalServerErrorException,
|
InternalServerErrorException,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
PayloadTooLargeException,
|
PayloadTooLargeException,
|
||||||
|
@ -75,6 +76,10 @@ const mapOfHedgeDocErrorsToHttpErrors: Map<string, HttpExceptionConstructor> =
|
||||||
'MaximumDocumentLengthExceededError',
|
'MaximumDocumentLengthExceededError',
|
||||||
(object): HttpException => new PayloadTooLargeException(object),
|
(object): HttpException => new PayloadTooLargeException(object),
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
'RegistrationDisabledError',
|
||||||
|
(object): HttpException => new ForbiddenException(object),
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@Catch()
|
@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
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
@ -59,3 +59,7 @@ export class PasswordTooWeakError extends Error {
|
||||||
export class MaximumDocumentLengthExceededError extends Error {
|
export class MaximumDocumentLengthExceededError extends Error {
|
||||||
name = 'MaximumDocumentLengthExceededError';
|
name = 'MaximumDocumentLengthExceededError';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class RegistrationDisabledError extends Error {
|
||||||
|
name = 'RegistrationDisabledError';
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue