@@ -6,65 +6,70 @@ import {
66} from '@nestjs/common' ;
77import { Reflector } from '@nestjs/core' ;
88import { Observable } from 'rxjs' ;
9- import { AUTHZ_ENFORCER , PERMISSIONS_METADATA } from './authz.constants' ;
9+ import {
10+ AUTHZ_ENFORCER ,
11+ PERMISSIONS_METADATA ,
12+ AUTHZ_MODULE_OPTIONS
13+ } from './authz.constants' ;
1014import * as casbin from 'casbin' ;
1115import { Permission } from './interfaces/permission.interface' ;
1216import { UnauthorizedException } from '@nestjs/common' ;
1317import { AuthPossession } from './types' ;
18+ import { AuthZModuleOptions } from './interfaces/authz-module-options.interface' ;
1419
1520@Injectable ( )
1621export class AuthZGuard implements CanActivate {
1722 constructor (
1823 private readonly reflector : Reflector ,
19- @Inject ( AUTHZ_ENFORCER ) public enforcer : casbin . Enforcer
24+ @Inject ( AUTHZ_ENFORCER ) private enforcer : casbin . Enforcer ,
25+ @Inject ( AUTHZ_MODULE_OPTIONS ) private options : AuthZModuleOptions
2026 ) { }
2127
2228 canActivate (
2329 context : ExecutionContext
2430 ) : boolean | Promise < boolean > | Observable < boolean > {
25- const permissions : Permission [ ] = this . reflector . get < Permission [ ] > (
26- PERMISSIONS_METADATA ,
27- context . getHandler ( )
28- ) ;
29-
30- if ( ! permissions ) {
31- return true ;
32- }
33-
34- const request = context . switchToHttp ( ) . getRequest ( ) ;
35- const user = request . user ;
36- if ( ! user ) {
37- throw new UnauthorizedException ( ) ;
38- }
31+ try {
32+ const permissions : Permission [ ] = this . reflector . get < Permission [ ] > (
33+ PERMISSIONS_METADATA ,
34+ context . getHandler ( )
35+ ) ;
3936
40- const { username : uname } = user ;
37+ if ( ! permissions ) {
38+ return true ;
39+ }
4140
42- const hasPermission = (
43- username : string ,
44- permission : Permission
45- ) : boolean => {
46- const { possession, resource, action } = permission ;
47- const poss = [ ] ;
41+ const username = this . options . usernameFromContext ( context ) ;
4842
49- if ( possession === AuthPossession . OWN_ANY ) {
50- poss . push ( AuthPossession . ANY , AuthPossession . OWN ) ;
51- } else {
52- poss . push ( possession ) ;
43+ if ( ! username ) {
44+ throw new UnauthorizedException ( ) ;
5345 }
5446
55- return poss . some ( p => {
56- if ( p === AuthPossession . OWN ) {
57- return ( permission as any ) . isOwn ( request ) ;
47+ const hasPermission = ( user : string , permission : Permission ) : boolean => {
48+ const { possession, resource, action } = permission ;
49+ const poss = [ ] ;
50+
51+ if ( possession === AuthPossession . OWN_ANY ) {
52+ poss . push ( AuthPossession . ANY , AuthPossession . OWN ) ;
5853 } else {
59- return this . enforcer . enforce ( username , resource , ` ${ action } : ${ p } ` ) ;
54+ poss . push ( possession ) ;
6055 }
61- } ) ;
62- } ;
6356
64- const result = permissions . every ( permission =>
65- hasPermission ( uname , permission )
66- ) ;
57+ return poss . some ( p => {
58+ if ( p === AuthPossession . OWN ) {
59+ return ( permission as any ) . isOwn ( context ) ;
60+ } else {
61+ return this . enforcer . enforce ( user , resource , `${ action } :${ p } ` ) ;
62+ }
63+ } ) ;
64+ } ;
6765
68- return result ;
66+ const result = permissions . every ( permission =>
67+ hasPermission ( username , permission )
68+ ) ;
69+
70+ return result ;
71+ } catch ( e ) {
72+ throw e ;
73+ }
6974 }
7075}
0 commit comments