forked from nest-cloud/nestcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbac.guard.ts
37 lines (34 loc) · 1.47 KB
/
rbac.guard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Injectable, CanActivate, ExecutionContext, ForbiddenException } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { RESOURCE_METADATA, VERB_METADATA } from "./constants";
import { IRbacAccount } from "./interfaces/rbac-account.interface";
import { InjectRbac } from "./decorators/inject-rbac.decorator";
import { Rbac } from "./rbac";
import { IRbacValidator } from "./interfaces/rbac-validator.interface";
@Injectable()
export class RbacGuard implements CanActivate {
constructor(
private readonly reflector: Reflector,
@InjectRbac() private readonly rbac: Rbac,
) {
}
canActivate(context: ExecutionContext): boolean {
const methodResource = this.reflector.get<string>(RESOURCE_METADATA, context.getHandler());
const clsResource = this.reflector.get<string>(RESOURCE_METADATA, context.getClass());
const verb = this.reflector.get<string>(VERB_METADATA, context.getHandler());
const resource = methodResource || clsResource;
if (!resource || !verb) {
return true;
}
const request = context.switchToHttp().getRequest();
const user: IRbacAccount = request.user;
if (!user) {
return false;
}
const validator: IRbacValidator = this.rbac.getValidator();
if (!validator) {
throw new ForbiddenException('Cannot find an available validator');
}
return validator.validate(resource, verb, user);
}
}