@@ -169,6 +169,126 @@ var getAuthForLegacySessionToken = function({
169
169
} ) ;
170
170
} ;
171
171
172
+ Auth . prototype . getUserRoles = function ( ) {
173
+ if ( this . isMaster || ! this . user ) {
174
+ return Promise . resolve ( [ ] ) ;
175
+ }
176
+ if ( this . fetchedRoles ) {
177
+ return Promise . resolve ( this . userRoles ) ;
178
+ }
179
+ if ( this . rolePromise ) {
180
+ return this . rolePromise ;
181
+ }
182
+ this . rolePromise = this . _loadUserRolesForAccess ( ) ;
183
+ return this . rolePromise ;
184
+ } ;
185
+
186
+ Auth . prototype . _loadUserRolesForAccess = async function ( ) {
187
+ if ( this . cacheController ) {
188
+ const cachedRoles = await this . cacheController . role . get ( this . user . id ) ;
189
+ if ( cachedRoles != null ) {
190
+ this . fetchedRoles = true ;
191
+ this . userRoles = cachedRoles ;
192
+ return cachedRoles ;
193
+ }
194
+ }
195
+
196
+ const { results } = await new RestQuery (
197
+ this . config ,
198
+ master ( this . config ) ,
199
+ '_Join:users:_Role' ,
200
+ { } ,
201
+ {
202
+ pipeline : this . _generateRoleGraphPipeline ( ) ,
203
+ }
204
+ ) . execute ( ) ;
205
+
206
+ const { directRoles, childRoles } = results [ 0 ] ;
207
+ const roles = [ ...directRoles , ...childRoles ] ;
208
+ this . userRoles = roles . map ( ( role ) => `role:${ role . name } ` ) ;
209
+ this . fetchedRoles = true ;
210
+ this . rolePromise = null ;
211
+ this . cacheRoles ( ) ;
212
+ return this . userRoles ;
213
+ } ;
214
+
215
+ Auth . prototype . _generateRoleGraphPipeline = function ( ) {
216
+ return [
217
+ {
218
+ $match : {
219
+ relatedId : this . user . id ,
220
+ } ,
221
+ } ,
222
+ {
223
+ $graphLookup : {
224
+ from : '_Join:roles:_Role' ,
225
+ startWith : '$owningId' ,
226
+ connectFromField : 'owningId' ,
227
+ connectToField : 'relatedId' ,
228
+ as : 'childRolePath' ,
229
+ } ,
230
+ } ,
231
+ {
232
+ $facet : {
233
+ directRoles : [
234
+ {
235
+ $lookup : {
236
+ from : '_Role' ,
237
+ localField : 'owningId' ,
238
+ foreignField : '_id' ,
239
+ as : 'Roles' ,
240
+ } ,
241
+ } ,
242
+ {
243
+ $unwind : {
244
+ path : '$Roles' ,
245
+ } ,
246
+ } ,
247
+ {
248
+ $replaceRoot : {
249
+ newRoot : {
250
+ $ifNull : [ '$Roles' , { $literal : { } } ] ,
251
+ } ,
252
+ } ,
253
+ } ,
254
+ {
255
+ $project : {
256
+ name : 1 ,
257
+ } ,
258
+ } ,
259
+ ] ,
260
+ childRoles : [
261
+ {
262
+ $lookup : {
263
+ from : '_Role' ,
264
+ localField : 'childRolePath.owningId' ,
265
+ foreignField : '_id' ,
266
+ as : 'Roles' ,
267
+ } ,
268
+ } ,
269
+ {
270
+ $unwind : {
271
+ path : '$Roles' ,
272
+ } ,
273
+ } ,
274
+ {
275
+ $replaceRoot : {
276
+ newRoot : {
277
+ $ifNull : [ '$Roles' , { $literal : { } } ] ,
278
+ } ,
279
+ } ,
280
+ } ,
281
+ {
282
+ $project : {
283
+ name : 1 ,
284
+ } ,
285
+ } ,
286
+ ] ,
287
+ } ,
288
+ } ,
289
+ ] ;
290
+ } ;
291
+
172
292
// Returns a promise that resolves to an array of role names
173
293
Auth . prototype . getUserRoles = function ( ) {
174
294
if ( this . isMaster || ! this . user ) {
0 commit comments