refactor: extract permission metadata key

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-05-19 14:22:10 +02:00
parent 488238d854
commit 9ca6485219
2 changed files with 7 additions and 5 deletions

View file

@ -11,6 +11,7 @@ import { CompleteRequest } from '../api/utils/request.type';
import { ConsoleLoggerService } from '../logger/console-logger.service';
import { NotesService } from '../notes/notes.service';
import { PermissionsService } from './permissions.service';
import { PERMISSION_METADATA_KEY } from './require-permission.decorator';
import { RequiredPermission } from './required-permission.enum';
/**
@ -32,7 +33,7 @@ export class PermissionsGuard implements CanActivate {
async canActivate(context: ExecutionContext): Promise<boolean> {
const permissions = this.reflector.get<RequiredPermission[]>(
'permissions',
PERMISSION_METADATA_KEY,
context.getHandler(),
);
// If no permissions are set this is probably an error and this guard should not let the request pass

View file

@ -7,12 +7,13 @@ import { CustomDecorator, SetMetadata } from '@nestjs/common';
import { RequiredPermission } from './required-permission.enum';
export const PERMISSION_METADATA_KEY = 'requiredPermission';
/**
* This decorator gathers the {@link Permission Permission} a user must hold for the {@link PermissionsGuard}
* @param permissions - an array of permissions. In practice this should always contain exactly one {@link Permission}
* @constructor
* This decorator gathers the {@link RequiredPermission Permission} a user must hold for the {@link PermissionsGuard}
* @param permissions - an array of permissions. In practice this should always contain exactly one {@link RequiredPermission}
*/
// eslint-disable-next-line func-style,@typescript-eslint/naming-convention
export const RequirePermission = (
...permissions: RequiredPermission[]
): CustomDecorator => SetMetadata('permissions', permissions);
): CustomDecorator => SetMetadata(PERMISSION_METADATA_KEY, permissions);