Skip to content

Commit 0f840b6

Browse files
authored
Fix/issue pointer permissions (#4141)
* Makes sure we don't override roles * Reduces the query size whith pointer permissions - Does not return as $and if not needed - Returns just the query with the additional constraint * Do not use $in if include is just of length 1
1 parent 019f9e5 commit 0f840b6

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

spec/PointerPermissions.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,47 @@ describe('Pointer Permissions', () => {
171171
})
172172
});
173173

174+
it('should query on pointer permission enabled column', (done) => {
175+
const config = new Config(Parse.applicationId);
176+
const user = new Parse.User();
177+
const user2 = new Parse.User();
178+
user.set({
179+
username: 'user1',
180+
password: 'password'
181+
});
182+
user2.set({
183+
username: 'user2',
184+
password: 'password'
185+
});
186+
const obj = new Parse.Object('AnObject');
187+
const obj2 = new Parse.Object('AnObject');
188+
user.signUp().then(() => {
189+
return user2.signUp()
190+
}).then(() => {
191+
Parse.User.logOut();
192+
}).then(() => {
193+
obj.set('owner', user);
194+
return Parse.Object.saveAll([obj, obj2]);
195+
}).then(() => {
196+
return config.database.loadSchema().then((schema) => {
197+
return schema.updateClass('AnObject', {}, {find: {}, get:{}, readUserFields: ['owner']})
198+
});
199+
}).then(() => {
200+
return Parse.User.logIn('user1', 'password');
201+
}).then(() => {
202+
const q = new Parse.Query('AnObject');
203+
q.equalTo('owner', user2);
204+
return q.find();
205+
}).then((res) => {
206+
expect(res.length).toBe(0);
207+
done();
208+
}).catch((err) => {
209+
jfail(err);
210+
fail('should not fail');
211+
done();
212+
})
213+
});
214+
174215
it('should not allow creating objects', (done) => {
175216
const config = new Config(Parse.applicationId);
176217
const user = new Parse.User();

src/Controllers/DatabaseController.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,14 @@ DatabaseController.prototype.addPointerPermissions = function(schema, className,
941941
const q = {
942942
[key]: userPointer
943943
};
944-
return {'$and': [q, query]};
944+
// if we already have a constraint on the key, use the $and
945+
if (query.hasOwnProperty(key)) {
946+
return {'$and': [q, query]};
947+
}
948+
// otherwise just add the constaint
949+
return Object.assign({}, query, {
950+
[`${key}`]: userPointer,
951+
})
945952
});
946953
if (ors.length > 1) {
947954
return {'$or': ors};

src/RestQuery.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,13 @@ function includePath(config, auth, response, path, restOptions = {}) {
635635
}
636636

637637
const queryPromises = Object.keys(pointersHash).map((className) => {
638-
const where = {'objectId': {'$in': Array.from(pointersHash[className])}};
638+
const objectIds = Array.from(pointersHash[className]);
639+
let where;
640+
if (objectIds.length === 1) {
641+
where = {'objectId': objectIds[0]};
642+
} else {
643+
where = {'objectId': {'$in': objectIds}};
644+
}
639645
var query = new RestQuery(config, auth, className, where, includeRestOptions);
640646
return query.execute({op: 'get'}).then((results) => {
641647
results.className = className;

src/RestWrite.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ RestWrite.prototype.getUserAndRoleACL = function() {
102102

103103
if (this.auth.user) {
104104
return this.auth.getUserRoles().then((roles) => {
105-
roles.push(this.auth.user.id);
106-
this.runOptions.acl = this.runOptions.acl.concat(roles);
105+
this.runOptions.acl = this.runOptions.acl.concat(roles, [this.auth.user.id]);
107106
return;
108107
});
109108
} else {

0 commit comments

Comments
 (0)