Skip to content

Commit 6e6a67a

Browse files
committed
Moves transform to MongoTransform
- Adds ACL query injection in MongoTransform
1 parent af30f66 commit 6e6a67a

File tree

6 files changed

+46
-35
lines changed

6 files changed

+46
-35
lines changed

spec/transform.spec.js renamed to spec/MongoTransform.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// These tests are unit tests designed to only test transform.js.
22
"use strict";
33

4-
let transform = require('../src/transform');
4+
let transform = require('../src/Adapters/Storage/Mongo/MongoTransform');
55
let dd = require('deep-diff');
66

77
var dummySchema = {

src/Adapters/Storage/Mongo/MongoSchemaCollection.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import MongoCollection from './MongoCollection';
3+
import * as transform from './MongoTransform';
34

45
function mongoFieldToParseSchemaField(type) {
56
if (type[0] === '*') {
@@ -200,6 +201,10 @@ class MongoSchemaCollection {
200201
update = {'$set': update};
201202
return this.upsertSchema(className, query, update);
202203
}
204+
205+
get transform() {
206+
return transform;
207+
}
203208
}
204209

205210
// Exported for testing reasons and because we haven't moved all mongo schema format

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
import MongoCollection from './MongoCollection';
33
import MongoSchemaCollection from './MongoSchemaCollection';
4-
import {parse as parseUrl, format as formatUrl} from '../../../vendor/mongodbUrl';
4+
import { parse as parseUrl, format as formatUrl } from '../../../vendor/mongodbUrl';
5+
import * as transform from './MongoTransform';
56

67
let mongodb = require('mongodb');
78
let MongoClient = mongodb.MongoClient;
@@ -78,6 +79,10 @@ export class MongoStorageAdapter {
7879
});
7980
});
8081
}
82+
83+
get transform() {
84+
return transform;
85+
}
8186
}
8287

8388
export default MongoStorageAdapter;

src/transform.js renamed to src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,27 @@ function untransformObject(schema, className, mongoObject, isNestedObject = fals
759759
}
760760
}
761761

762+
function addWriteACL(mongoWhere, acl) {
763+
var writePerms = [
764+
{_wperm: {'$exists': false}}
765+
];
766+
for (var entry of acl) {
767+
writePerms.push({_wperm: {'$in': [entry]}});
768+
}
769+
return {'$and': [mongoWhere, {'$or': writePerms}]};
770+
}
771+
772+
function addReadACL(mongoWhere, acl) {
773+
var orParts = [
774+
{"_rperm" : { "$exists": false }},
775+
{"_rperm" : { "$in" : ["*"]}}
776+
];
777+
for (var entry of acl) {
778+
orParts.push({"_rperm" : { "$in" : [entry]}});
779+
}
780+
return {'$and': [mongoWhere, {'$or': orParts}]};
781+
}
782+
762783
var DateCoder = {
763784
JSONToDatabase(json) {
764785
return new Date(json.iso);
@@ -852,5 +873,7 @@ module.exports = {
852873
transformCreate: transformCreate,
853874
transformUpdate: transformUpdate,
854875
transformWhere: transformWhere,
876+
addReadACL: addReadACL,
877+
addWriteACL: addWriteACL,
855878
untransformObject: untransformObject
856879
};

src/Controllers/DatabaseController.js

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var mongodb = require('mongodb');
77
var Parse = require('parse/node').Parse;
88

99
var Schema = require('./../Schema');
10-
var transform = require('./../transform');
1110
const deepcopy = require('deepcopy');
1211

1312
// options can contain:
@@ -121,7 +120,7 @@ DatabaseController.prototype.validateObject = function(className, object, query,
121120
// Filters out any data that shouldn't be on this REST-formatted object.
122121
DatabaseController.prototype.untransformObject = function(
123122
schema, isMaster, aclGroup, className, mongoObject) {
124-
var object = transform.untransformObject(schema, className, mongoObject);
123+
var object = this.adapter.transform.untransformObject(schema, className, mongoObject);
125124

126125
if (className !== '_User') {
127126
return object;
@@ -167,17 +166,11 @@ DatabaseController.prototype.update = function(className, query, update, options
167166
.then(() => this.handleRelationUpdates(className, query.objectId, update))
168167
.then(() => this.adaptiveCollection(className))
169168
.then(collection => {
170-
var mongoWhere = transform.transformWhere(schema, className, query);
169+
var mongoWhere = this.adapter.transform.transformWhere(schema, className, query);
171170
if (options.acl) {
172-
var writePerms = [
173-
{_wperm: {'$exists': false}}
174-
];
175-
for (var entry of options.acl) {
176-
writePerms.push({_wperm: {'$in': [entry]}});
177-
}
178-
mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]};
171+
mongoWhere = this.adapter.transform.addWriteACL(mongoWhere, options.acl);
179172
}
180-
mongoUpdate = transform.transformUpdate(schema, className, update);
173+
mongoUpdate = this.adapter.transform.transformUpdate(schema, className, update);
181174
return collection.findOneAndUpdate(mongoWhere, mongoUpdate);
182175
})
183176
.then(result => {
@@ -304,16 +297,9 @@ DatabaseController.prototype.destroy = function(className, query, options = {})
304297
})
305298
.then(() => this.adaptiveCollection(className))
306299
.then(collection => {
307-
let mongoWhere = transform.transformWhere(schema, className, query);
308-
300+
let mongoWhere = this.adapter.transform.transformWhere(schema, className, query, options);
309301
if (options.acl) {
310-
var writePerms = [
311-
{ _wperm: { '$exists': false } }
312-
];
313-
for (var entry of options.acl) {
314-
writePerms.push({ _wperm: { '$in': [entry] } });
315-
}
316-
mongoWhere = { '$and': [mongoWhere, { '$or': writePerms }] };
302+
mongoWhere = this.adapter.transform.addWriteACL(mongoWhere, options.acl);
317303
}
318304
return collection.deleteMany(mongoWhere);
319305
})
@@ -349,7 +335,7 @@ DatabaseController.prototype.create = function(className, object, options) {
349335
.then(() => this.handleRelationUpdates(className, null, object))
350336
.then(() => this.adaptiveCollection(className))
351337
.then(coll => {
352-
var mongoObject = transform.transformCreate(schema, className, object);
338+
var mongoObject = this.adapter.transform.transformCreate(schema, className, object);
353339
return coll.insertOne(mongoObject);
354340
})
355341
.then(result => {
@@ -606,7 +592,7 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
606592
if (options.sort) {
607593
mongoOptions.sort = {};
608594
for (let key in options.sort) {
609-
let mongoKey = transform.transformKey(schema, className, key);
595+
let mongoKey = this.adapter.transform.transformKey(schema, className, key);
610596
mongoOptions.sort[mongoKey] = options.sort[key];
611597
}
612598
}
@@ -623,16 +609,9 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
623609
.then(() => this.reduceInRelation(className, query, schema))
624610
.then(() => this.adaptiveCollection(className))
625611
.then(collection => {
626-
let mongoWhere = transform.transformWhere(schema, className, query);
612+
let mongoWhere = this.adapter.transform.transformWhere(schema, className, query);
627613
if (!isMaster) {
628-
let orParts = [
629-
{"_rperm" : { "$exists": false }},
630-
{"_rperm" : { "$in" : ["*"]}}
631-
];
632-
for (let acl of aclGroup) {
633-
orParts.push({"_rperm" : { "$in" : [acl]}});
634-
}
635-
mongoWhere = {'$and': [mongoWhere, {'$or': orParts}]};
614+
mongoWhere = this.adapter.transform.addReadACL(mongoWhere, aclGroup);
636615
}
637616
if (options.count) {
638617
delete mongoOptions.limit;

src/Schema.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// TODO: hide all schema logic inside the database adapter.
1616

1717
const Parse = require('parse/node').Parse;
18-
const transform = require('./transform');
1918
import MongoSchemaCollection from './Adapters/Storage/Mongo/MongoSchemaCollection';
2019
import _ from 'lodash';
2120

@@ -429,7 +428,7 @@ class Schema {
429428
validateField(className, fieldName, type, freeze) {
430429
return this.reloadData().then(() => {
431430
// Just to check that the fieldName is valid
432-
transform.transformKey(this, className, fieldName);
431+
this._collection.transform.transformKey(this, className, fieldName);
433432

434433
if( fieldName.indexOf(".") > 0 ) {
435434
// subdocument key (x.y) => ok if x is of type 'object'

0 commit comments

Comments
 (0)