Skip to content

Commit d623928

Browse files
committed
Update Auth.js
1 parent e0af690 commit d623928

File tree

1 file changed

+106
-83
lines changed

1 file changed

+106
-83
lines changed

src/Auth.js

Lines changed: 106 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Deprecator from './Deprecator/Deprecator';
55
import { logger } from './logger';
66
import RestQuery from './RestQuery';
77
import RestWrite from './RestWrite';
8+
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';
89

910
// An Auth object tells you who is requesting something and whether
1011
// the master key was used.
@@ -139,7 +140,6 @@ const getAuthForSessionToken = async function ({
139140
limit: 1,
140141
include: 'user',
141142
};
142-
const RestQuery = require('./RestQuery');
143143
const query = new RestQuery(config, master(config), '_Session', { sessionToken }, restOptions);
144144
results = (await query.execute()).results;
145145
} else {
@@ -183,7 +183,6 @@ var getAuthForLegacySessionToken = function ({ config, sessionToken, installatio
183183
var restOptions = {
184184
limit: 1,
185185
};
186-
const RestQuery = require('./RestQuery');
187186
var query = new RestQuery(config, master(config), '_User', { sessionToken }, restOptions);
188187
return query.execute().then(response => {
189188
var results = response.results;
@@ -221,100 +220,112 @@ Auth.prototype.getRolesForUser = async function () {
221220
//Stack all Parse.Role
222221
const results = [];
223222
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+
},
236236
},
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+
},
245245
},
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+
},
256256
},
257-
},
258-
{
259-
$unwind: {
260-
path: '$Roles',
257+
{
258+
$unwind: {
259+
path: '$Roles',
260+
},
261261
},
262-
},
263-
{
264-
$replaceRoot: {
265-
newRoot: {
266-
$ifNull: ['$Roles', { $literal: {} }],
262+
{
263+
$replaceRoot: {
264+
newRoot: {
265+
$ifNull: ['$Roles', { $literal: {} }],
266+
},
267267
},
268268
},
269-
},
270-
{
271-
$project: {
272-
name: 1,
269+
{
270+
$project: {
271+
name: 1,
272+
},
273273
},
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+
},
283283
},
284-
},
285-
{
286-
$unwind: {
287-
path: '$Roles',
284+
{
285+
$unwind: {
286+
path: '$Roles',
287+
},
288288
},
289-
},
290-
{
291-
$replaceRoot: {
292-
newRoot: {
293-
$ifNull: ['$Roles', { $literal: {} }],
289+
{
290+
$replaceRoot: {
291+
newRoot: {
292+
$ifNull: ['$Roles', { $literal: {} }],
293+
},
294294
},
295295
},
296-
},
297-
{
298-
$project: {
299-
name: 1,
296+
{
297+
$project: {
298+
name: 1,
299+
},
300300
},
301-
},
302-
],
301+
],
302+
},
303303
},
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+
}
317317
}
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+
);
318329
}
319330
} else {
320331
await new Parse.Query(Parse.Role)
@@ -345,6 +356,19 @@ Auth.prototype._loadRoles = async function () {
345356
return this.userRoles;
346357
}
347358

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+
348372
if (typeof results[0] === 'object') {
349373
const rolesMap = results.reduce(
350374
(m, r) => {
@@ -410,7 +434,6 @@ Auth.prototype.getRolesByIds = async function (ins) {
410434
};
411435
});
412436
const restWhere = { roles: { $in: roles } };
413-
const RestQuery = require('./RestQuery');
414437
await new RestQuery(this.config, master(this.config), '_Role', restWhere, {}).each(result =>
415438
results.push(result)
416439
);

0 commit comments

Comments
 (0)