1
1
const cryptoUtils = require ( './cryptoUtils' ) ;
2
2
const RestQuery = require ( './RestQuery' ) ;
3
3
const Parse = require ( 'parse/node' ) ;
4
- const { continueWhile } = require ( 'parse/lib/node/promiseUtils' ) ;
5
4
6
5
// An Auth object tells you who is requesting something and whether
7
6
// the master key was used.
@@ -186,51 +185,28 @@ Auth.prototype.getUserRoles = function() {
186
185
} ;
187
186
188
187
Auth . prototype . getRolesForUser = async function ( ) {
188
+ //Stack all Parse.Role
189
+ const results = [ ] ;
189
190
if ( this . config ) {
190
- const masterConfig = master ( this . config ) ;
191
191
const restWhere = {
192
192
users : {
193
193
__type : 'Pointer' ,
194
194
className : '_User' ,
195
195
objectId : this . user . id ,
196
196
} ,
197
197
} ;
198
- const queryOptions = {
199
- limit : 100 ,
200
- order : 'objectId'
201
- } ;
202
- let finished = false ;
203
-
204
- //Stack all Parse.Role
205
- let results = [ ] ;
206
-
207
- //Get All Parse.Role for User
208
- await continueWhile ( ( ) => {
209
- return ! finished ;
210
- } , async ( ) => {
211
- const query = new RestQuery (
212
- this . config ,
213
- masterConfig ,
214
- '_Role' ,
215
- restWhere ,
216
- queryOptions
217
- ) ;
218
- const currentResults = await query . execute ( ) . then ( ( { results } ) => results ) ;
219
- finished = currentResults . length < queryOptions . limit ;
220
- if ( ! finished ) {
221
- restWhere . objectId = { '$gt' : currentResults [ currentResults . length - 1 ] . objectId }
222
- }
223
- results = results . concat ( currentResults ) ;
224
- } ) ;
225
- return results ;
198
+ await new RestQuery (
199
+ this . config ,
200
+ master ( this . config ) ,
201
+ '_Role' ,
202
+ restWhere ,
203
+ { }
204
+ ) . each ( result => results . push ( result ) ) ;
205
+ } else {
206
+ await new Parse . Query ( Parse . Role )
207
+ . equalTo ( 'users' , this . user )
208
+ . each ( result => results . push ( result . toJSON ( ) ) , { useMasterKey : true } ) ;
226
209
}
227
-
228
- //Stack all Parse.Role
229
- const results = [ ] ;
230
-
231
- await new Parse . Query ( Parse . Role )
232
- . equalTo ( 'users' , this . user )
233
- . each ( result => results . push ( result . toJSON ( ) ) , { useMasterKey :true } )
234
210
return results ;
235
211
} ;
236
212
@@ -287,19 +263,11 @@ Auth.prototype.cacheRoles = function() {
287
263
return true ;
288
264
} ;
289
265
290
- Auth . prototype . getRolesByIds = function ( ins ) {
291
- const roles = ins . map ( id => {
292
- return {
293
- __type : 'Pointer' ,
294
- className : '_Role' ,
295
- objectId : id ,
296
- } ;
297
- } ) ;
298
- const restWhere = { roles : { $in : roles } } ;
299
-
266
+ Auth . prototype . getRolesByIds = async function ( ins ) {
267
+ const results = [ ] ;
300
268
// Build an OR query across all parentRoles
301
269
if ( ! this . config ) {
302
- return new Parse . Query ( Parse . Role )
270
+ await new Parse . Query ( Parse . Role )
303
271
. containedIn (
304
272
'roles' ,
305
273
ins . map ( id => {
@@ -308,13 +276,30 @@ Auth.prototype.getRolesByIds = function(ins) {
308
276
return role ;
309
277
} )
310
278
)
311
- . find ( { useMasterKey : true } )
312
- . then ( results => results . map ( obj => obj . toJSON ( ) ) ) ;
279
+ . each (
280
+ result => {
281
+ results . push ( result . toJSON ( ) ) ;
282
+ } ,
283
+ { useMasterKey : true }
284
+ ) ;
285
+ } else {
286
+ const roles = ins . map ( id => {
287
+ return {
288
+ __type : 'Pointer' ,
289
+ className : '_Role' ,
290
+ objectId : id ,
291
+ } ;
292
+ } ) ;
293
+ const restWhere = { roles : { $in : roles } } ;
294
+ await new RestQuery (
295
+ this . config ,
296
+ master ( this . config ) ,
297
+ '_Role' ,
298
+ restWhere ,
299
+ { }
300
+ ) . each ( result => results . push ( result ) ) ;
313
301
}
314
-
315
- return new RestQuery ( this . config , master ( this . config ) , '_Role' , restWhere , { } )
316
- . execute ( )
317
- . then ( ( { results } ) => results ) ;
302
+ return results ;
318
303
} ;
319
304
320
305
// Given a list of roleIds, find all the parent roles, returns a promise with all names
0 commit comments