mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.0 KiB
37 lines
1.0 KiB
import { HAS_PERMISSION_KEY } from '@ghostfolio/api/decorators/has-permission.decorator';
|
|
import { hasPermission } from '@ghostfolio/common/permissions';
|
|
import {
|
|
CanActivate,
|
|
ExecutionContext,
|
|
HttpException,
|
|
Injectable
|
|
} from '@nestjs/common';
|
|
import { Reflector } from '@nestjs/core';
|
|
import { StatusCodes, getReasonPhrase } from 'http-status-codes';
|
|
|
|
@Injectable()
|
|
export class HasPermissionGuard implements CanActivate {
|
|
public constructor(private reflector: Reflector) {}
|
|
|
|
public canActivate(context: ExecutionContext): boolean {
|
|
const { user } = context.switchToHttp().getRequest();
|
|
const requiredPermission = this.reflector.get<string>(
|
|
HAS_PERMISSION_KEY,
|
|
context.getHandler()
|
|
);
|
|
|
|
if (!requiredPermission) {
|
|
// No specific permissions required
|
|
return true;
|
|
}
|
|
|
|
if (!user || !hasPermission(user.permissions, requiredPermission)) {
|
|
throw new HttpException(
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
StatusCodes.FORBIDDEN
|
|
);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|