1
1
import {
2
+ ExceptionMessage ,
2
3
type PermissionKey ,
3
4
type ProjectPermissionKey ,
4
5
} from "~/libs/enums/enums.js" ;
5
- import { checkPermissions } from "~/libs/helpers/helpers.js" ;
6
+ import { checkHasPermission } from "~/libs/helpers/helpers.js" ;
6
7
import {
7
8
type APIHandlerOptions ,
8
9
type APIPreHandler ,
9
10
} from "~/libs/modules/controller/controller.js" ;
11
+ import { HTTPCode , HTTPError } from "~/libs/modules/http/http.js" ;
10
12
import { type UserAuthResponseDto } from "~/modules/users/users.js" ;
11
13
12
14
import { type ValueOf } from "../types/types.js" ;
@@ -20,12 +22,29 @@ const checkUserPermissions = (
20
22
const user = options . user as UserAuthResponseDto ;
21
23
const projectId = getProjectId ?.( options ) ;
22
24
23
- checkPermissions ( {
24
- projectId : projectId ?? null ,
25
- projectsPermissions : projectsPermissions ?? null ,
26
- rootPermissions : permissions ,
27
- user,
28
- } ) ;
25
+ const userPermissions = user . groups . flatMap ( ( group ) => group . permissions ) ;
26
+ const projectPermissions = projectId
27
+ ? user . projectGroups
28
+ . filter ( ( group ) => group . projectId === projectId )
29
+ . flatMap ( ( projectGroup ) => projectGroup . permissions )
30
+ : [ ] ;
31
+
32
+ const hasGlobalPermission = checkHasPermission (
33
+ permissions ,
34
+ userPermissions ,
35
+ ) ;
36
+
37
+ const hasProjectPermission =
38
+ projectId && projectsPermissions
39
+ ? checkHasPermission ( projectsPermissions , projectPermissions )
40
+ : false ;
41
+
42
+ if ( ! hasGlobalPermission && ( ! projectId || ! hasProjectPermission ) ) {
43
+ throw new HTTPError ( {
44
+ message : ExceptionMessage . NO_PERMISSION ,
45
+ status : HTTPCode . FORBIDDEN ,
46
+ } ) ;
47
+ }
29
48
30
49
done ( ) ;
31
50
} ;
0 commit comments