Skip to content

Commit b257017

Browse files
committed
Adds ClassLevelPermissions type
1 parent 7b41730 commit b257017

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/Controllers/SchemaController.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,22 @@ type SchemaFields = { [string]: SchemaField }
2424
type Schema = {
2525
className: string,
2626
fields: SchemaFields,
27-
classLevelPermissions: any,
27+
classLevelPermissions: ClassLevelPermissions,
2828
indexes?: ?any
2929
};
3030

31+
type ClassLevelPermissions = {
32+
find?: {[string]: boolean};
33+
count?: {[string]: boolean};
34+
get?: {[string]: boolean};
35+
create?: {[string]: boolean};
36+
update?: {[string]: boolean};
37+
delete?: {[string]: boolean};
38+
addField?: {[string]: boolean};
39+
readUserFields?: string[];
40+
writeUserFields?: string[];
41+
};
42+
3143
// @flow-disable-next
3244
const Parse = require('parse/node').Parse;
3345
import { StorageAdapter } from '../Adapters/Storage/StorageAdapter';
@@ -176,17 +188,21 @@ function verifyPermissionKey(key) {
176188
}
177189

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

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

217+
// @flow-disable-next
201218
Object.keys(perms[operation]).forEach((key) => {
202219
verifyPermissionKey(key);
220+
// @flow-disable-next
203221
const perm = perms[operation][key];
204222
if (perm !== true) {
223+
// @flow-disable-next
205224
throw new Parse.Error(Parse.Error.INVALID_JSON, `'${perm}' is not a valid value for class level permissions ${operation}:${key}:${perm}`);
206225
}
207226
});
@@ -352,7 +371,7 @@ const _AudienceSchema = convertSchemaToAdapterSchema(injectDefaultSchema({
352371
}));
353372
const VolatileClassesSchemas = [_HooksSchema, _JobStatusSchema, _JobScheduleSchema, _PushStatusSchema, _GlobalConfigSchema, _AudienceSchema];
354373

355-
const dbTypeMatchesObjectType = (dbType: any, objectType: any) => {
374+
const dbTypeMatchesObjectType = (dbType: SchemaField | string, objectType: SchemaField) => {
356375
if (dbType.type !== objectType.type) return false;
357376
if (dbType.targetClass !== objectType.targetClass) return false;
358377
if (dbType === objectType.type) return true;
@@ -629,7 +648,7 @@ export default class SchemaController {
629648
return this.validateSchemaData(className, fields, classLevelPermissions, []);
630649
}
631650

632-
validateSchemaData(className: string, fields: SchemaFields, classLevelPermissions: any, existingFieldNames: Array<string>) {
651+
validateSchemaData(className: string, fields: SchemaFields, classLevelPermissions: ClassLevelPermissions, existingFieldNames: Array<string>) {
633652
for (const fieldName in fields) {
634653
if (existingFieldNames.indexOf(fieldName) < 0) {
635654
if (!fieldNameIsValid(fieldName)) {
@@ -721,7 +740,11 @@ export default class SchemaController {
721740
return this.reloadData({ clearCache: true });
722741
}).then(() => {
723742
// Ensure that the schema now validates
724-
if (!dbTypeMatchesObjectType(this.getExpectedType(className, fieldName), type)) {
743+
const expectedType = this.getExpectedType(className, fieldName);
744+
if (typeof type === 'string') {
745+
type = { type };
746+
}
747+
if (!expectedType || !dbTypeMatchesObjectType(expectedType, type)) {
725748
throw new Parse.Error(Parse.Error.INVALID_JSON, `Could not add field ${fieldName}`);
726749
}
727750
// Remove the cached schema
@@ -732,7 +755,7 @@ export default class SchemaController {
732755
}
733756

734757
// maintain compatibility
735-
deleteField(fieldName: string, className: any, database: DatabaseController) {
758+
deleteField(fieldName: string, className: string, database: DatabaseController) {
736759
return this.deleteFields([fieldName], className, database);
737760
}
738761

0 commit comments

Comments
 (0)