Skip to content

Commit 95535a0

Browse files
committed
refactor(backend): consolidate project, mission, and file guards into parameterizable guards using route metadata
1 parent 3c0d394 commit 95535a0

5 files changed

Lines changed: 131 additions & 539 deletions

File tree

backend/src/endpoints/auth/guards/file.guards.ts

Lines changed: 16 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Injectable,
77
UnauthorizedException,
88
} from '@nestjs/common';
9+
import { Reflector } from '@nestjs/core';
910
import { BaseGuard } from './base.guards';
1011

1112
interface 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
}

backend/src/endpoints/auth/guards/index.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,18 @@ export {
88
} from './base.guards';
99

1010
// Project guards
11-
export {
12-
CreateGuard,
13-
CreateInProjectByBodyGuard,
14-
DeleteProjectGuard,
15-
ReadProjectByNameGuard,
16-
ReadProjectGuard,
17-
WriteProjectGuard,
18-
} from './project.guards';
11+
export { CreateGuard, ProjectAccessGuard } from './project.guards';
1912

2013
// Mission guards
2114
export {
22-
AddTagGuard,
23-
CanDeleteMissionGuard,
2415
CanReadManyMissionsGuard,
25-
CreateInMissionByBodyGuard,
2616
DeleteTagGuard,
17+
MissionAccessGuard,
2718
MoveMissionToProjectGuard,
28-
ReadMissionByNameGuard,
29-
ReadMissionGuard,
30-
WriteMissionByBodyGuard,
3119
} from './mission.guards';
3220

3321
// File guards
34-
export {
35-
DeleteFileGuard,
36-
MoveFilesGuard,
37-
ReadFileByNameGuard,
38-
ReadFileGuard,
39-
WriteFileGuard,
40-
} from './file.guards';
22+
export { FileAccessGuard, MoveFilesGuard } from './file.guards';
4123

4224
// Action guards
4325
export {

0 commit comments

Comments
 (0)