Ensure all roles are properly loaded #5131 (#5132)

* Fix Limitation Role #5131

Allow to manage Live Query with User that have more than 100 Parse.Roles

* Clean Up

* Add Custom Config Support and Test

* Fix Auth Test

* Switch to Async Function

* Fix restWhere

* Fix Test

* Clean Final Commit

* Lint Fix

* Need to Fix Test Callback

* Fixes broken test

* Restore find() method in spy

* adds restquery-each

* small nit

* adds changelog
This commit is contained in:
Antoine Cormouls
2018-10-20 22:45:23 +02:00
committed by Florent Vilmart
parent aa9580e59c
commit de79b70cbc
6 changed files with 157 additions and 34 deletions

View File

@@ -184,7 +184,9 @@ Auth.prototype.getUserRoles = function() {
return this.rolePromise;
};
Auth.prototype.getRolesForUser = function() {
Auth.prototype.getRolesForUser = async function() {
//Stack all Parse.Role
const results = [];
if (this.config) {
const restWhere = {
users: {
@@ -193,20 +195,19 @@ Auth.prototype.getRolesForUser = function() {
objectId: this.user.id,
},
};
const query = new RestQuery(
await new RestQuery(
this.config,
master(this.config),
'_Role',
restWhere,
{}
);
return query.execute().then(({ results }) => results);
).each(result => results.push(result));
} else {
await new Parse.Query(Parse.Role)
.equalTo('users', this.user)
.each(result => results.push(result.toJSON()), { useMasterKey: true });
}
return new Parse.Query(Parse.Role)
.equalTo('users', this.user)
.find({ useMasterKey: true })
.then(results => results.map(obj => obj.toJSON()));
return results;
};
// Iterates through the role tree and compiles a user's roles
@@ -262,19 +263,11 @@ Auth.prototype.cacheRoles = function() {
return true;
};
Auth.prototype.getRolesByIds = function(ins) {
const roles = ins.map(id => {
return {
__type: 'Pointer',
className: '_Role',
objectId: id,
};
});
const restWhere = { roles: { $in: roles } };
Auth.prototype.getRolesByIds = async function(ins) {
const results = [];
// Build an OR query across all parentRoles
if (!this.config) {
return new Parse.Query(Parse.Role)
await new Parse.Query(Parse.Role)
.containedIn(
'roles',
ins.map(id => {
@@ -283,13 +276,25 @@ Auth.prototype.getRolesByIds = function(ins) {
return role;
})
)
.find({ useMasterKey: true })
.then(results => results.map(obj => obj.toJSON()));
.each(result => results.push(result.toJSON()), { useMasterKey: true });
} else {
const roles = ins.map(id => {
return {
__type: 'Pointer',
className: '_Role',
objectId: id,
};
});
const restWhere = { roles: { $in: roles } };
await new RestQuery(
this.config,
master(this.config),
'_Role',
restWhere,
{}
).each(result => results.push(result));
}
return new RestQuery(this.config, master(this.config), '_Role', restWhere, {})
.execute()
.then(({ results }) => results);
return results;
};
// Given a list of roleIds, find all the parent roles, returns a promise with all names

View File

@@ -4,7 +4,7 @@
var SchemaController = require('./Controllers/SchemaController');
var Parse = require('parse/node').Parse;
const triggers = require('./triggers');
const { continueWhile } = require('parse/lib/node/promiseUtils');
const AlwaysSelectedKeys = ['objectId', 'createdAt', 'updatedAt', 'ACL'];
// restOptions can include:
// skip
@@ -199,6 +199,36 @@ RestQuery.prototype.execute = function(executeOptions) {
});
};
RestQuery.prototype.each = function(callback) {
const { config, auth, className, restWhere, restOptions, clientSDK } = this;
// if the limit is set, use it
restOptions.limit = restOptions.limit || 100;
restOptions.order = 'objectId';
let finished = false;
return continueWhile(
() => {
return !finished;
},
async () => {
const query = new RestQuery(
config,
auth,
className,
restWhere,
restOptions,
clientSDK
);
const { results } = await query.execute();
results.forEach(callback);
finished = results.length < restOptions.limit;
if (!finished) {
restWhere.objectId = { $gt: results[results.length - 1].objectId };
}
}
);
};
RestQuery.prototype.buildRestWhere = function() {
return Promise.resolve()
.then(() => {