@@ -74,48 +74,66 @@ export class DBHelper {
7474 Logger . otel . endSpan ( span ) ;
7575 }
7676 }
77- public static async GetRoles ( _id : string , ident : number , parent : Span ) : Promise < Role [ ] > {
78- const span : Span = Logger . otel . startSubSpan ( "dbhelper.GetRoles" , parent ) ;
79- span . setAttribute ( "_id" , _id ) ;
80- span . setAttribute ( "ident" , ident ) ;
81- try {
82- if ( ident > Config . max_recursive_group_depth ) return [ ] ;
83- const result : Role [ ] = [ ] ;
84- const query : any = { "members" : { "$elemMatch" : { _id : _id } } } ;
85- const ids : string [ ] = [ ] ;
86- const _roles : Role [ ] = await Config . db . query < Role > ( query , null , Config . expected_max_roles , 0 , null , "users" , Crypt . rootToken ( ) , undefined , undefined , span ) ;
87- for ( let role of _roles ) {
88- if ( ids . indexOf ( role . _id ) == - 1 ) {
89- ids . push ( role . _id ) ;
90- result . push ( role ) ;
91- const _subroles : Role [ ] = await this . GetRoles ( role . _id , ident + 1 , span ) ;
92- for ( let subrole of _subroles ) {
93- if ( ids . indexOf ( subrole . _id ) == - 1 ) {
94- ids . push ( subrole . _id ) ;
95- result . push ( subrole ) ;
96- }
97- }
98- }
99- }
100- return result ;
101- } catch ( error ) {
102- span . recordException ( error ) ;
103- throw error ;
104- } finally {
105- Logger . otel . endSpan ( span ) ;
106- }
107- }
77+ // public static async GetRoles(_id: string, ident: number, parent: Span): Promise<Role[]> {
78+ // const span: Span = Logger.otel.startSubSpan("dbhelper.GetRoles", parent);
79+ // span.setAttribute("_id", _id);
80+ // span.setAttribute("ident", ident);
81+ // try {
82+ // if (ident > Config.max_recursive_group_depth) return [];
83+ // const result: Role[] = [];
84+ // const query: any = { "members": { "$elemMatch": { _id: _id } } };
85+ // const ids: string[] = [];
86+ // const _roles: Role[] = await Config.db.query<Role>(query, null, Config.expected_max_roles, 0, null, "users", Crypt.rootToken(), undefined, undefined, span);
87+ // for (let role of _roles) {
88+ // if (ids.indexOf(role._id) == -1) {
89+ // ids.push(role._id);
90+ // result.push(role);
91+ // const _subroles: Role[] = await this.GetRoles(role._id, ident + 1, span);
92+ // for (let subrole of _subroles) {
93+ // if (ids.indexOf(subrole._id) == -1) {
94+ // ids.push(subrole._id);
95+ // result.push(subrole);
96+ // }
97+ // }
98+ // }
99+ // }
100+ // return result;
101+ // } catch (error) {
102+ // span.recordException(error);
103+ // throw error;
104+ // } finally {
105+ // Logger.otel.endSpan(span);
106+ // }
107+ // }
108108 public static cached_roles : Role [ ] = [ ] ;
109109 public static cached_at : Date = new Date ( ) ;
110110 public static async DecorateWithRoles ( user : User , parent : Span ) : Promise < void > {
111111 const span : Span = Logger . otel . startSubSpan ( "dbhelper.DecorateWithRoles" , parent ) ;
112112 try {
113113 if ( ! Config . decorate_roles_fetching_all_roles ) {
114- const roles : Role [ ] = await this . GetRoles ( user . _id , 0 , span ) ;
115- user . roles = [ ] ;
116- roles . forEach ( role => {
117- user . roles . push ( new Rolemember ( role . name , role . _id ) ) ;
118- } ) ;
114+ // const roles: Role[] = await this.GetRoles(user._id, 0, span);
115+ // user.roles = [];
116+ // roles.forEach(role => {
117+ // user.roles.push(new Rolemember(role.name, role._id));
118+ // });
119+ const pipe : any = [ { "$match" : { "_id" : user . _id } } ,
120+ {
121+ "$graphLookup" : {
122+ from : "users" ,
123+ startWith : "$_id" ,
124+ connectFromField : "_id" ,
125+ connectToField : "members._id" ,
126+ as : "roles" ,
127+ maxDepth : Config . max_recursive_group_depth ,
128+ restrictSearchWithMatch : { "_type" : "role" }
129+ }
130+ } ]
131+ const results = await Config . db . aggregate < User > ( pipe , "users" , Crypt . rootToken ( ) , null , span ) ;
132+ if ( results . length > 0 ) {
133+ user = results [ 0 ] ;
134+ user . roles = user . roles . map ( x => ( { "_id" : x . _id , "name" : x . name } ) ) as any ;
135+ user . roles = user . roles ;
136+ }
119137 } else {
120138 var end : number = new Date ( ) . getTime ( ) ;
121139 var seconds = Math . round ( ( end - this . cached_at . getTime ( ) ) / 1000 ) ;
0 commit comments