11import { Injectable } from '@nestjs/common' ;
22import { InjectRepository } from '@nestjs/typeorm' ;
3- import { DataSource , EntityManager , IsNull , Repository } from 'typeorm' ;
3+ import { DataSource , EntityManager , In , IsNull , Repository } from 'typeorm' ;
44import { PermissionDto } from './dto/permission.dto' ;
55import { ListRespDto , UserPermissionDto } from './dto/list-resp.dto' ;
66import { plainToInstance } from 'class-transformer' ;
77import {
88 comparePermissionLevel ,
9+ maxPermission ,
10+ maxPermissions ,
911 PermissionLevel ,
1012} from './permission-level.enum' ;
1113import { UserPermission } from './entities/user-permission.entity' ;
@@ -14,6 +16,7 @@ import { Resource } from 'src/resources/resources.entity';
1416import { UserService } from 'src/user/user.service' ;
1517import { GroupUser } from 'src/groups/entities/group-user.entity' ;
1618import { NamespaceMember } from 'src/namespaces/entities/namespace-member.entity' ;
19+ import { Record } from 'openai/core' ;
1720
1821@Injectable ( )
1922export class PermissionsService {
@@ -428,6 +431,102 @@ export class PermissionsService {
428431 return level ;
429432 }
430433
434+ getGlobalPermissionFromParents (
435+ parentResources : Resource [ ] ,
436+ ) : PermissionLevel | null {
437+ for ( const resource of parentResources ) {
438+ if ( resource . globalLevel ) {
439+ return resource . globalLevel ;
440+ }
441+ }
442+ return null ;
443+ }
444+
445+ async getUserPermissionFromParents (
446+ namespaceId : string ,
447+ parentResourceIds : string [ ] ,
448+ userId : string ,
449+ ) : Promise < PermissionLevel | null > {
450+ const userPermissions = await this . userPermiRepo . find ( {
451+ where : {
452+ namespace : { id : namespaceId } ,
453+ user : { id : userId } ,
454+ resourceId : In ( parentResourceIds ) ,
455+ } ,
456+ } ) ;
457+ const userPermiMap : Map < string , UserPermission > = new Map (
458+ userPermissions . map ( ( permi ) => [ permi . resourceId , permi ] ) ,
459+ ) ;
460+ for ( const resourceId of parentResourceIds ) {
461+ const permission = userPermiMap . get ( resourceId ) ;
462+ if ( permission ) {
463+ return permission . level ;
464+ }
465+ }
466+ return null ;
467+ }
468+
469+ async getGroupPermissionFromParents (
470+ namespaceId : string ,
471+ parentResourceIds : string [ ] ,
472+ groupIds : string [ ] ,
473+ ) : Promise < PermissionLevel | null > {
474+ const groupPermissions = await this . groupPermiRepo . find ( {
475+ where : {
476+ namespace : { id : namespaceId } ,
477+ resource : { id : In ( parentResourceIds ) } ,
478+ group : { id : In ( groupIds ) } ,
479+ } ,
480+ } ) ;
481+ const permiMap : Map < string , PermissionLevel | null > = new Map ( ) ;
482+ for ( const groupPermi of groupPermissions ) {
483+ const resourceId = groupPermi . resource ! . id ;
484+ const curPermi = permiMap . get ( resourceId ) || null ;
485+ permiMap . set ( resourceId , maxPermission ( curPermi , groupPermi . level ) ) ;
486+ }
487+ for ( const resourceId of parentResourceIds ) {
488+ const permission = permiMap . get ( resourceId ) ;
489+ if ( permission ) {
490+ return permission ;
491+ }
492+ }
493+ return null ;
494+ }
495+
496+ async getCurrentPermissionFromParents (
497+ namespaceId : string ,
498+ parentResources : Resource [ ] ,
499+ userId : string ,
500+ ) : Promise < PermissionLevel > {
501+ const groups = await this . groupUserRepository . find ( {
502+ where : {
503+ namespace : { id : namespaceId } ,
504+ user : { id : userId } ,
505+ } ,
506+ } ) ;
507+ const groupIds = groups . map ( ( group ) => group . groupId ) ;
508+ const parentResourceIds = parentResources . map ( ( resource ) => resource . id ) ;
509+
510+ const globalPermission =
511+ this . getGlobalPermissionFromParents ( parentResources ) ;
512+ const userPermission = await this . getUserPermissionFromParents (
513+ namespaceId ,
514+ parentResourceIds ,
515+ userId ,
516+ ) ;
517+ const groupPermission = await this . getGroupPermissionFromParents (
518+ namespaceId ,
519+ parentResourceIds ,
520+ groupIds ,
521+ ) ;
522+ const curPermission = maxPermissions ( [
523+ globalPermission ,
524+ userPermission ,
525+ groupPermission ,
526+ ] ) ;
527+ return curPermission || PermissionLevel . NO_ACCESS ;
528+ }
529+
431530 async getParentId (
432531 namespaceId : string ,
433532 resourceId : string ,
0 commit comments