Skip to content

Commit ee0b6c3

Browse files
committed
Adds ClassLevelPermissions type
1 parent aef7074 commit ee0b6c3

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/Controllers/SchemaController.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,19 @@ type SchemaFields = { [string]: SchemaField }
2424
type Schema = {
2525
className: string,
2626
fields: SchemaFields,
27-
classLevelPermissions: any
27+
classLevelPermissions: ClassLevelPermissions
28+
};
29+
30+
type ClassLevelPermissions = {
31+
find?: {[string]: boolean};
32+
count?: {[string]: boolean};
33+
get?: {[string]: boolean};
34+
create?: {[string]: boolean};
35+
update?: {[string]: boolean};
36+
delete?: {[string]: boolean};
37+
addField?: {[string]: boolean};
38+
readUserFields?: string[];
39+
writeUserFields?: string[];
2840
};
2941

3042
// @flow-disable-next
@@ -175,17 +187,21 @@ function verifyPermissionKey(key) {
175187
}
176188

177189
const CLPValidKeys = Object.freeze(['find', 'count', 'get', 'create', 'update', 'delete', 'addField', 'readUserFields', 'writeUserFields']);
178-
function validateCLP(perms: any, fields: SchemaFields) {
190+
function validateCLP(perms: ClassLevelPermissions, fields: SchemaFields) {
179191
if (!perms) {
180192
return;
181193
}
182194
Object.keys(perms).forEach((operation) => {
183195
if (CLPValidKeys.indexOf(operation) == -1) {
184196
throw new Parse.Error(Parse.Error.INVALID_JSON, `${operation} is not a valid operation for class level permissions`);
185197
}
198+
if (!perms[operation]) {
199+
return;
200+
}
186201

187202
if (operation === 'readUserFields' || operation === 'writeUserFields') {
188203
if (!Array.isArray(perms[operation])) {
204+
// @flow-disable-next
189205
throw new Parse.Error(Parse.Error.INVALID_JSON, `'${perms[operation]}' is not a valid value for class level permissions ${operation}`);
190206
} else {
191207
perms[operation].forEach((key) => {
@@ -197,10 +213,13 @@ function validateCLP(perms: any, fields: SchemaFields) {
197213
return;
198214
}
199215

216+
// @flow-disable-next
200217
Object.keys(perms[operation]).forEach((key) => {
201218
verifyPermissionKey(key);
219+
// @flow-disable-next
202220
const perm = perms[operation][key];
203221
if (perm !== true) {
222+
// @flow-disable-next
204223
throw new Parse.Error(Parse.Error.INVALID_JSON, `'${perm}' is not a valid value for class level permissions ${operation}:${key}:${perm}`);
205224
}
206225
});
@@ -341,7 +360,7 @@ const _AudienceSchema = convertSchemaToAdapterSchema(injectDefaultSchema({
341360
}));
342361
const VolatileClassesSchemas = [_HooksSchema, _JobStatusSchema, _JobScheduleSchema, _PushStatusSchema, _GlobalConfigSchema, _AudienceSchema];
343362

344-
const dbTypeMatchesObjectType = (dbType: any, objectType: any) => {
363+
const dbTypeMatchesObjectType = (dbType: SchemaField | string, objectType: SchemaField) => {
345364
if (dbType.type !== objectType.type) return false;
346365
if (dbType.targetClass !== objectType.targetClass) return false;
347366
if (dbType === objectType.type) return true;
@@ -602,7 +621,7 @@ export default class SchemaController {
602621
return this.validateSchemaData(className, fields, classLevelPermissions, []);
603622
}
604623

605-
validateSchemaData(className: string, fields: SchemaFields, classLevelPermissions: any, existingFieldNames: Array<string>) {
624+
validateSchemaData(className: string, fields: SchemaFields, classLevelPermissions: ClassLevelPermissions, existingFieldNames: Array<string>) {
606625
for (const fieldName in fields) {
607626
if (existingFieldNames.indexOf(fieldName) < 0) {
608627
if (!fieldNameIsValid(fieldName)) {
@@ -695,7 +714,11 @@ export default class SchemaController {
695714
return this.reloadData({ clearCache: true });
696715
}).then(() => {
697716
// Ensure that the schema now validates
698-
if (!dbTypeMatchesObjectType(this.getExpectedType(className, fieldName), type)) {
717+
const expectedType = this.getExpectedType(className, fieldName);
718+
if (typeof type === 'string') {
719+
type = { type };
720+
}
721+
if (!expectedType || !dbTypeMatchesObjectType(expectedType, type)) {
699722
throw new Parse.Error(Parse.Error.INVALID_JSON, `Could not add field ${fieldName}`);
700723
}
701724
// Remove the cached schema
@@ -706,7 +729,7 @@ export default class SchemaController {
706729
}
707730

708731
// maintain compatibility
709-
deleteField(fieldName: string, className: any, database: DatabaseController) {
732+
deleteField(fieldName: string, className: string, database: DatabaseController) {
710733
return this.deleteFields([fieldName], className, database);
711734
}
712735

0 commit comments

Comments
 (0)