Added array support for pointer-permissions (#5921)

* added array support for pointer permissions

* added tests for array support for pointer permissions

* Postgres fix

* simplify PG, no idea why this works
This commit is contained in:
Dobbias Nan
2019-08-16 06:55:12 +02:00
committed by Diamond Lewis
parent 4f8da12674
commit 0fa315fc5b
4 changed files with 1920 additions and 927 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -268,7 +268,7 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => {
const initialPatternsLength = patterns.length;
const fieldValue = query[fieldName];
// nothingin the schema, it's gonna blow up
// nothing in the schema, it's gonna blow up
if (!schema.fields[fieldName]) {
// as it won't exist
if (fieldValue && fieldValue.$exists === false) {
@@ -498,6 +498,12 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => {
}
values.push(fieldName, JSON.stringify(fieldValue.$all));
index += 2;
} else if (Array.isArray(fieldValue.$all)) {
if (fieldValue.$all.length === 1) {
patterns.push(`$${index}:name = $${index + 1}`);
values.push(fieldName, fieldValue.$all[0].objectId);
index += 2;
}
}
if (typeof fieldValue.$exists !== 'undefined') {

View File

@@ -1482,23 +1482,23 @@ class DatabaseController {
};
const permFields = perms[field];
const ors = permFields.map(key => {
const ors = permFields.flatMap(key => {
// constraint for single pointer setup
const q = {
[key]: userPointer,
};
// constraint for users-array setup
const qa = {
[key]: { $all: [userPointer] },
};
// if we already have a constraint on the key, use the $and
if (Object.prototype.hasOwnProperty.call(query, key)) {
return { $and: [q, query] };
return [{ $and: [q, query] }, { $and: [qa, query] }];
}
// otherwise just add the constaint
return Object.assign({}, query, {
[`${key}`]: userPointer,
});
return [Object.assign({}, query, q), Object.assign({}, query, qa)];
});
if (ors.length > 1) {
return { $or: ors };
}
return ors[0];
return { $or: ors };
} else {
return query;
}

View File

@@ -238,9 +238,12 @@ function validateCLP(perms: ClassLevelPermissions, fields: SchemaFields) {
} else {
perms[operation].forEach(key => {
if (
!fields[key] ||
fields[key].type != 'Pointer' ||
fields[key].targetClass != '_User'
!(
fields[key] &&
((fields[key].type == 'Pointer' &&
fields[key].targetClass == '_User') ||
fields[key].type == 'Array')
)
) {
throw new Parse.Error(
Parse.Error.INVALID_JSON,