11import { HttpException , HttpStatus , Inject , Injectable , Logger , OnModuleInit } from '@nestjs/common' ;
2- import { AccessLevelEnum , PermissionTypeEnum } from '../../enums/index.js' ;
32import { Messages } from '../../exceptions/text/messages.js' ;
43import { Cacher } from '../../helpers/cache/cacher.js' ;
54import { IGlobalDatabaseContext } from '../../common/application/global-database-context.interface.js' ;
65import { BaseType } from '../../common/data-injection.tokens.js' ;
76import { GroupEntity } from '../group/group.entity.js' ;
87import { IComplexPermission } from '../permission/permission.interface.js' ;
9- import { PermissionEntity } from '../permission/permission.entity.js' ;
108import {
119 CedarAction ,
1210 CedarResourceType ,
@@ -87,14 +85,12 @@ export class CedarAuthorizationService implements ICedarAuthorizationService, On
8785 ) : Promise < { cedarPolicy : string ; classicalPermissions : IComplexPermission } > {
8886 this . validateCedarPolicyText ( cedarPolicy ) ;
8987
90- const group = await this . globalDbContext . groupRepository . findGroupWithPermissionsById ( groupId ) ;
88+ const group = await this . globalDbContext . groupRepository . findGroupByIdWithConnectionAndUsers ( groupId ) ;
9189 if ( ! group ) {
9290 throw new HttpException ( { message : Messages . GROUP_NOT_FOUND } , HttpStatus . BAD_REQUEST ) ;
9391 }
9492
95- const groupWithConnection = await this . globalDbContext . groupRepository . findGroupByIdWithConnectionAndUsers ( groupId ) ;
96-
97- if ( groupWithConnection ?. connection ?. id !== connectionId ) {
93+ if ( group . connection ?. id !== connectionId ) {
9894 throw new HttpException ( { message : Messages . GROUP_NOT_FROM_THIS_CONNECTION } , HttpStatus . BAD_REQUEST ) ;
9995 }
10096
@@ -106,8 +102,6 @@ export class CedarAuthorizationService implements ICedarAuthorizationService, On
106102
107103 const classicalPermissions = parseCedarPolicyToClassicalPermissions ( cedarPolicy , connectionId , groupId ) ;
108104
109- await this . syncClassicalPermissions ( group , classicalPermissions ) ;
110-
111105 group . cedarPolicy = cedarPolicy ;
112106 await this . globalDbContext . groupRepository . saveNewOrUpdatedGroup ( group ) ;
113107 Cacher . invalidateCedarPolicyCache ( connectionId ) ;
@@ -181,7 +175,7 @@ export class CedarAuthorizationService implements ICedarAuthorizationService, On
181175 const userGroups = await this . globalDbContext . groupRepository . findAllUserGroupsInConnection ( connectionId , userId ) ;
182176 if ( userGroups . length === 0 ) return false ;
183177
184- const groupPolicies = await this . loadPoliciesPerGroup ( connectionId , userGroups ) ;
178+ const groupPolicies = this . loadPoliciesPerGroup ( userGroups ) ;
185179 if ( groupPolicies . length === 0 ) return false ;
186180
187181 const entities = buildCedarEntities ( userId , userGroups , connectionId , tableName , dashboardId ) ;
@@ -210,13 +204,8 @@ export class CedarAuthorizationService implements ICedarAuthorizationService, On
210204 return false ;
211205 }
212206
213- private async loadPoliciesPerGroup ( connectionId : string , userGroups : Array < GroupEntity > ) : Promise < string [ ] > {
214- const groups = await this . globalDbContext . groupRepository . findAllGroupsInConnection ( connectionId ) ;
215- const userGroupIdSet = new Set ( userGroups . map ( ( g ) => g . id ) ) ;
216- return groups
217- . filter ( ( g ) => userGroupIdSet . has ( g . id ) )
218- . map ( ( g ) => g . cedarPolicy )
219- . filter ( Boolean ) ;
207+ private loadPoliciesPerGroup ( userGroups : Array < GroupEntity > ) : string [ ] {
208+ return userGroups . map ( ( g ) => g . cedarPolicy ) . filter ( Boolean ) ;
220209 }
221210
222211 private async assertUserNotSuspended ( userId : string ) : Promise < void > {
@@ -316,74 +305,4 @@ export class CedarAuthorizationService implements ICedarAuthorizationService, On
316305 }
317306 }
318307
319- private async syncClassicalPermissions ( group : GroupEntity , permissions : IComplexPermission ) : Promise < void > {
320- if ( group . permissions && group . permissions . length > 0 ) {
321- for ( const perm of group . permissions ) {
322- await this . globalDbContext . permissionRepository . removePermissionEntity ( perm ) ;
323- }
324- }
325- group . permissions = [ ] ;
326-
327- if ( permissions . connection . accessLevel !== AccessLevelEnum . none ) {
328- const connPerm = new PermissionEntity ( ) ;
329- connPerm . type = PermissionTypeEnum . Connection ;
330- connPerm . accessLevel = permissions . connection . accessLevel ;
331- const saved = await this . globalDbContext . permissionRepository . saveNewOrUpdatedPermission ( connPerm ) ;
332- group . permissions . push ( saved ) ;
333- }
334-
335- if ( permissions . group . accessLevel !== AccessLevelEnum . none ) {
336- const groupPerm = new PermissionEntity ( ) ;
337- groupPerm . type = PermissionTypeEnum . Group ;
338- groupPerm . accessLevel = permissions . group . accessLevel ;
339- const saved = await this . globalDbContext . permissionRepository . saveNewOrUpdatedPermission ( groupPerm ) ;
340- group . permissions . push ( saved ) ;
341- }
342-
343- for ( const table of permissions . tables ) {
344- const access = table . accessLevel ;
345- if ( access . visibility ) {
346- const perm = new PermissionEntity ( ) ;
347- perm . type = PermissionTypeEnum . Table ;
348- perm . accessLevel = AccessLevelEnum . visibility ;
349- perm . tableName = table . tableName ;
350- const saved = await this . globalDbContext . permissionRepository . saveNewOrUpdatedPermission ( perm ) ;
351- group . permissions . push ( saved ) ;
352- }
353- if ( access . readonly ) {
354- const perm = new PermissionEntity ( ) ;
355- perm . type = PermissionTypeEnum . Table ;
356- perm . accessLevel = AccessLevelEnum . readonly ;
357- perm . tableName = table . tableName ;
358- const saved = await this . globalDbContext . permissionRepository . saveNewOrUpdatedPermission ( perm ) ;
359- group . permissions . push ( saved ) ;
360- }
361- if ( access . add ) {
362- const perm = new PermissionEntity ( ) ;
363- perm . type = PermissionTypeEnum . Table ;
364- perm . accessLevel = AccessLevelEnum . add ;
365- perm . tableName = table . tableName ;
366- const saved = await this . globalDbContext . permissionRepository . saveNewOrUpdatedPermission ( perm ) ;
367- group . permissions . push ( saved ) ;
368- }
369- if ( access . edit ) {
370- const perm = new PermissionEntity ( ) ;
371- perm . type = PermissionTypeEnum . Table ;
372- perm . accessLevel = AccessLevelEnum . edit ;
373- perm . tableName = table . tableName ;
374- const saved = await this . globalDbContext . permissionRepository . saveNewOrUpdatedPermission ( perm ) ;
375- group . permissions . push ( saved ) ;
376- }
377- if ( access . delete ) {
378- const perm = new PermissionEntity ( ) ;
379- perm . type = PermissionTypeEnum . Table ;
380- perm . accessLevel = AccessLevelEnum . delete ;
381- perm . tableName = table . tableName ;
382- const saved = await this . globalDbContext . permissionRepository . saveNewOrUpdatedPermission ( perm ) ;
383- group . permissions . push ( saved ) ;
384- }
385- }
386-
387- await this . globalDbContext . groupRepository . saveNewOrUpdatedGroup ( group ) ;
388- }
389308}
0 commit comments