66 Injectable ,
77 UnauthorizedException ,
88} from '@nestjs/common' ;
9+ import { Reflector } from '@nestjs/core' ;
910import { BaseGuard } from './base.guards' ;
1011
1112interface FileBody {
@@ -19,122 +20,29 @@ interface FileParameters {
1920}
2021
2122@Injectable ( )
22- export class DeleteFileGuard extends BaseGuard {
23- constructor ( private fileGuardService : FileGuardService ) {
23+ export class FileAccessGuard extends BaseGuard {
24+ constructor (
25+ private fileGuardService : FileGuardService ,
26+ private reflector : Reflector ,
27+ ) {
2428 super ( ) ;
2529 }
2630
2731 async canActivate ( context : ExecutionContext ) : Promise < boolean > {
2832 const { user, apiKey, request } = await this . getUser ( context ) ;
2933
34+ const requiredRight =
35+ this . reflector . get < AccessGroupRights | undefined > (
36+ 'accessRight' ,
37+ context . getHandler ( ) ,
38+ ) ?? AccessGroupRights . READ ;
39+
3040 const body = request . body as FileBody | undefined ;
3141 const params = request . params as FileParameters | undefined ;
32- let fileUUID = request . query . uuid as string | undefined ;
33-
34- if ( ! fileUUID && body ) {
35- fileUUID = body . uuid ;
36- }
37-
38- if ( ! fileUUID && params ) {
39- fileUUID = params . uuid ;
40- }
41-
42- if ( ! fileUUID ) {
43- return false ; // Deny access if UUID not provided
44- }
45-
46- if ( apiKey ) {
47- return this . fileGuardService . canKeyAccessFile (
48- apiKey ,
49- fileUUID ,
50- AccessGroupRights . DELETE ,
51- ) ;
52- }
53- return this . fileGuardService . canAccessFile (
54- user ,
55- fileUUID ,
56- AccessGroupRights . DELETE ,
57- ) ;
58- }
59- }
60-
61- @Injectable ( )
62- export class ReadFileGuard extends BaseGuard {
63- constructor ( private fileGuardService : FileGuardService ) {
64- super ( ) ;
65- }
66-
67- async canActivate ( context : ExecutionContext ) : Promise < boolean > {
68- const { user, apiKey, request } = await this . getUser ( context ) ;
69-
70- const params = request . params as FileParameters ;
71- const fileUUID =
72- ( request . query . uuid as string | undefined ) ?? params . uuid ;
73-
74- if ( ! fileUUID ) {
75- return false ; // Deny access if UUID not provided
76- }
77-
78- if ( apiKey ) {
79- return this . fileGuardService . canKeyAccessFile (
80- apiKey ,
81- fileUUID ,
82- AccessGroupRights . READ ,
83- ) ;
84- }
85- return this . fileGuardService . canAccessFile (
86- user ,
87- fileUUID ,
88- AccessGroupRights . READ ,
89- ) ;
90- }
91- }
92-
93- @Injectable ( )
94- export class ReadFileByNameGuard extends BaseGuard {
95- constructor ( private fileGuardService : FileGuardService ) {
96- super ( ) ;
97- }
98-
99- async canActivate ( context : ExecutionContext ) : Promise < boolean > {
100- const { user, apiKey, request } = await this . getUser ( context ) ;
101-
102- const filename = request . query . name as string | undefined ;
103-
104- if ( ! filename ) {
105- return false ; // Deny access if filename not provided
106- }
107-
108- if ( apiKey ) {
109- return this . fileGuardService . canKeyAccessFileByName (
110- apiKey ,
111- filename ,
112- AccessGroupRights . READ ,
113- ) ;
114- }
115- return this . fileGuardService . canAccessFileByName (
116- user ,
117- filename ,
118- AccessGroupRights . READ ,
119- ) ;
120- }
121- }
122-
123- @Injectable ( )
124- export class WriteFileGuard extends BaseGuard {
125- constructor ( private fileGuardService : FileGuardService ) {
126- super ( ) ;
127- }
128-
129- async canActivate ( context : ExecutionContext ) : Promise < boolean > {
130- const { user, apiKey, request } = await this . getUser ( context ) ;
131-
132- const body = request . body as FileBody ;
133- const params = request . params as FileParameters ;
13442 const fileUUID =
13543 ( request . query . uuid as string | undefined ) ??
136- body . uuid ??
137- params . uuid ;
44+ body ? .uuid ??
45+ params ? .uuid ;
13846
13947 if ( ! fileUUID ) {
14048 return false ; // Deny access if UUID not provided
@@ -144,13 +52,13 @@ export class WriteFileGuard extends BaseGuard {
14452 return this . fileGuardService . canKeyAccessFile (
14553 apiKey ,
14654 fileUUID ,
147- AccessGroupRights . WRITE ,
55+ requiredRight ,
14856 ) ;
14957 }
15058 return this . fileGuardService . canAccessFile (
15159 user ,
15260 fileUUID ,
153- AccessGroupRights . WRITE ,
61+ requiredRight ,
15462 ) ;
15563 }
15664}
0 commit comments