@@ -5,6 +5,7 @@ import Deprecator from './Deprecator/Deprecator';
5
5
import { logger } from './logger' ;
6
6
import RestQuery from './RestQuery' ;
7
7
import RestWrite from './RestWrite' ;
8
+ import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter' ;
8
9
9
10
// An Auth object tells you who is requesting something and whether
10
11
// the master key was used.
@@ -139,7 +140,6 @@ const getAuthForSessionToken = async function ({
139
140
limit : 1 ,
140
141
include : 'user' ,
141
142
} ;
142
- const RestQuery = require ( './RestQuery' ) ;
143
143
const query = new RestQuery ( config , master ( config ) , '_Session' , { sessionToken } , restOptions ) ;
144
144
results = ( await query . execute ( ) ) . results ;
145
145
} else {
@@ -183,7 +183,6 @@ var getAuthForLegacySessionToken = function ({ config, sessionToken, installatio
183
183
var restOptions = {
184
184
limit : 1 ,
185
185
} ;
186
- const RestQuery = require ( './RestQuery' ) ;
187
186
var query = new RestQuery ( config , master ( config ) , '_User' , { sessionToken } , restOptions ) ;
188
187
return query . execute ( ) . then ( response => {
189
188
var results = response . results ;
@@ -221,100 +220,112 @@ Auth.prototype.getRolesForUser = async function () {
221
220
//Stack all Parse.Role
222
221
const results = [ ] ;
223
222
if ( this . config ) {
224
- const RestQuery = require ( './RestQuery' ) ;
225
- const prefix = this . config . databaseAdapter . _collectionPrefix || '' ;
226
- const result = await new RestQuery (
227
- this . config ,
228
- master ( this . config ) ,
229
- '_Join:users:_Role' ,
230
- { } ,
231
- {
232
- pipeline : [
233
- {
234
- $match : {
235
- relatedId : this . user . id ,
223
+ if ( this . config . database . adapter instanceof MongoStorageAdapter ) {
224
+ const prefix = this . config . databaseAdapter . _collectionPrefix || '' ;
225
+ const result = await new RestQuery (
226
+ this . config ,
227
+ master ( this . config ) ,
228
+ '_Join:users:_Role' ,
229
+ { } ,
230
+ {
231
+ pipeline : [
232
+ {
233
+ $match : {
234
+ relatedId : this . user . id ,
235
+ } ,
236
236
} ,
237
- } ,
238
- {
239
- $graphLookup : {
240
- from : ` ${ prefix } _Join:roles:_Role` ,
241
- startWith : '$ owningId' ,
242
- connectFromField : 'owningId ' ,
243
- connectToField : 'relatedId ' ,
244
- as : 'childRolePath' ,
237
+ {
238
+ $graphLookup : {
239
+ from : ` ${ prefix } _Join:roles:_Role` ,
240
+ startWith : '$owningId' ,
241
+ connectFromField : 'owningId' ,
242
+ connectToField : 'relatedId ' ,
243
+ as : 'childRolePath ' ,
244
+ } ,
245
245
} ,
246
- } ,
247
- {
248
- $facet : {
249
- directRoles : [
250
- {
251
- $lookup : {
252
- from : ` ${ prefix } _Role` ,
253
- localField : 'owningId ' ,
254
- foreignField : '_id ' ,
255
- as : 'Roles' ,
246
+ {
247
+ $facet : {
248
+ directRoles : [
249
+ {
250
+ $lookup : {
251
+ from : ` ${ prefix } _Role` ,
252
+ localField : 'owningId' ,
253
+ foreignField : '_id ' ,
254
+ as : 'Roles ' ,
255
+ } ,
256
256
} ,
257
- } ,
258
- {
259
- $unwind : {
260
- path : '$Roles' ,
257
+ {
258
+ $unwind : {
259
+ path : '$Roles' ,
260
+ } ,
261
261
} ,
262
- } ,
263
- {
264
- $replaceRoot : {
265
- newRoot : {
266
- $ifNull : [ '$Roles' , { $literal : { } } ] ,
262
+ {
263
+ $replaceRoot : {
264
+ newRoot : {
265
+ $ifNull : [ '$Roles' , { $literal : { } } ] ,
266
+ } ,
267
267
} ,
268
268
} ,
269
- } ,
270
- {
271
- $project : {
272
- name : 1 ,
269
+ {
270
+ $project : {
271
+ name : 1 ,
272
+ } ,
273
273
} ,
274
- } ,
275
- ] ,
276
- childRoles : [
277
- {
278
- $lookup : {
279
- from : ` ${ prefix } _Role` ,
280
- localField : 'childRolePath.owningId ' ,
281
- foreignField : '_id ' ,
282
- as : 'Roles' ,
274
+ ] ,
275
+ childRoles : [
276
+ {
277
+ $lookup : {
278
+ from : ` ${ prefix } _Role` ,
279
+ localField : 'childRolePath.owningId' ,
280
+ foreignField : '_id ' ,
281
+ as : 'Roles ' ,
282
+ } ,
283
283
} ,
284
- } ,
285
- {
286
- $unwind : {
287
- path : '$Roles' ,
284
+ {
285
+ $unwind : {
286
+ path : '$Roles' ,
287
+ } ,
288
288
} ,
289
- } ,
290
- {
291
- $replaceRoot : {
292
- newRoot : {
293
- $ifNull : [ '$Roles' , { $literal : { } } ] ,
289
+ {
290
+ $replaceRoot : {
291
+ newRoot : {
292
+ $ifNull : [ '$Roles' , { $literal : { } } ] ,
293
+ } ,
294
294
} ,
295
295
} ,
296
- } ,
297
- {
298
- $project : {
299
- name : 1 ,
296
+ {
297
+ $project : {
298
+ name : 1 ,
299
+ } ,
300
300
} ,
301
- } ,
302
- ] ,
301
+ ] ,
302
+ } ,
303
303
} ,
304
- } ,
305
- ] ,
306
- }
307
- ) . execute ( ) ;
308
- const { directRoles , childRoles } = result . results [ 0 ] || {
309
- directRoles : [ ] ,
310
- childRoles : [ ] ,
311
- } ;
312
- const roles = [ ... directRoles , ... childRoles ] ;
313
- for ( const role of roles ) {
314
- const roleName = `role: ${ role . name } ` ;
315
- if ( ! results . includes ( roleName ) ) {
316
- results . push ( role ) ;
304
+ ] ,
305
+ }
306
+ ) . execute ( ) ;
307
+ const { directRoles , childRoles } = result . results [ 0 ] || {
308
+ directRoles : [ ] ,
309
+ childRoles : [ ] ,
310
+ } ;
311
+ const roles = [ ... directRoles , ... childRoles ] ;
312
+ for ( const role of roles ) {
313
+ const roleName = `role: ${ role . name } ` ;
314
+ if ( ! results . includes ( roleName ) ) {
315
+ results . push ( role ) ;
316
+ }
317
317
}
318
+ } else {
319
+ const restWhere = {
320
+ users : {
321
+ __type : 'Pointer' ,
322
+ className : '_User' ,
323
+ objectId : this . user . id ,
324
+ } ,
325
+ } ;
326
+ await new RestQuery ( this . config , master ( this . config ) , '_Role' , restWhere , { } ) . each ( result =>
327
+ results . push ( result )
328
+ ) ;
318
329
}
319
330
} else {
320
331
await new Parse . Query ( Parse . Role )
@@ -345,6 +356,19 @@ Auth.prototype._loadRoles = async function () {
345
356
return this . userRoles ;
346
357
}
347
358
359
+ if ( ! ( this . config ?. database ?. adapter instanceof MongoStorageAdapter ) ) {
360
+ const rolesMap = results . reduce (
361
+ ( m , r ) => {
362
+ m . names . push ( r . name ) ;
363
+ m . ids . push ( r . objectId ) ;
364
+ return m ;
365
+ } ,
366
+ { ids : [ ] , names : [ ] }
367
+ ) ;
368
+ const roleNames = await this . _getAllRolesNamesForRoleIds ( rolesMap . ids , rolesMap . names ) ;
369
+ this . userRoles = roleNames . map ( r => `role:${ r } ` ) ;
370
+ }
371
+
348
372
if ( typeof results [ 0 ] === 'object' ) {
349
373
const rolesMap = results . reduce (
350
374
( m , r ) => {
@@ -410,7 +434,6 @@ Auth.prototype.getRolesByIds = async function (ins) {
410
434
} ;
411
435
} ) ;
412
436
const restWhere = { roles : { $in : roles } } ;
413
- const RestQuery = require ( './RestQuery' ) ;
414
437
await new RestQuery ( this . config , master ( this . config ) , '_Role' , restWhere , { } ) . each ( result =>
415
438
results . push ( result )
416
439
) ;
0 commit comments