Skip to content

Commit 2afb285

Browse files
committed
Merge pull request #374 from flessard/user-roles
Fix : User Roles not added to create, update or delete calls
2 parents 39f8143 + 5b8aa22 commit 2afb285

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

spec/ParseRole.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Parse Role testing', () => {
4949
}).then((x) => {
5050
x.set('foo', 'baz');
5151
// This should fail:
52-
return x.save();
52+
return x.save({},{sessionToken: ""});
5353
}).then((x) => {
5454
fail('Should not have been able to save.');
5555
}, (e) => {

src/Auth.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Auth.prototype.getUserRoles = function() {
8080
return Promise.resolve(this.userRoles);
8181
}
8282
if (this.rolePromise) {
83-
return rolePromise;
83+
return this.rolePromise;
8484
}
8585
this.rolePromise = this._loadRoles();
8686
return this.rolePromise;

src/RestWrite.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function RestWrite(config, auth, className, query, data, originalData) {
2727
this.auth = auth;
2828
this.className = className;
2929
this.storage = {};
30+
this.runOptions = {};
3031

3132
if (!query && data.objectId) {
3233
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'objectId ' +
@@ -66,6 +67,8 @@ function RestWrite(config, auth, className, query, data, originalData) {
6667
// status and location are optional.
6768
RestWrite.prototype.execute = function() {
6869
return Promise.resolve().then(() => {
70+
return this.getUserAndRoleACL();
71+
}).then(() => {
6972
return this.validateSchema();
7073
}).then(() => {
7174
return this.handleInstallation();
@@ -88,6 +91,25 @@ RestWrite.prototype.execute = function() {
8891
});
8992
};
9093

94+
// Uses the Auth object to get the list of roles, adds the user id
95+
RestWrite.prototype.getUserAndRoleACL = function() {
96+
if (this.auth.isMaster) {
97+
return Promise.resolve();
98+
}
99+
100+
this.runOptions.acl = ['*'];
101+
102+
if( this.auth.user ){
103+
return this.auth.getUserRoles().then((roles) => {
104+
roles.push(this.auth.user.id);
105+
this.runOptions.acl = this.runOptions.acl.concat(roles);
106+
return Promise.resolve();
107+
});
108+
}else{
109+
return Promise.resolve();
110+
}
111+
};
112+
91113
// Validates this operation against the schema.
92114
RestWrite.prototype.validateSchema = function() {
93115
return this.config.database.validateObject(this.className, this.data);
@@ -690,18 +712,10 @@ RestWrite.prototype.runDatabaseOperation = function() {
690712
throw new Parse.Error(Parse.Error.INVALID_ACL, 'Invalid ACL.');
691713
}
692714

693-
var options = {};
694-
if (!this.auth.isMaster) {
695-
options.acl = ['*'];
696-
if (this.auth.user) {
697-
options.acl.push(this.auth.user.id);
698-
}
699-
}
700-
701715
if (this.query) {
702716
// Run an update
703717
return this.config.database.update(
704-
this.className, this.query, this.data, options).then((resp) => {
718+
this.className, this.query, this.data, this.runOptions).then((resp) => {
705719
this.response = resp;
706720
this.response.updatedAt = this.updatedAt;
707721
});
@@ -714,7 +728,7 @@ RestWrite.prototype.runDatabaseOperation = function() {
714728
this.data.ACL = ACL;
715729
}
716730
// Run a create
717-
return this.config.database.create(this.className, this.data, options)
731+
return this.config.database.create(this.className, this.data, this.runOptions)
718732
.then(() => {
719733
var resp = {
720734
objectId: this.data.objectId,

src/rest.js

+7
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ function del(config, auth, className, objectId) {
5656
});
5757
}
5858
return Promise.resolve({});
59+
}).then(() => {
60+
if (!auth.isMaster) {
61+
return auth.getUserRoles();
62+
}else{
63+
return Promise.resolve();
64+
}
5965
}).then(() => {
6066
var options = {};
6167
if (!auth.isMaster) {
6268
options.acl = ['*'];
6369
if (auth.user) {
6470
options.acl.push(auth.user.id);
71+
options.acl = options.acl.concat(auth.userRoles);
6572
}
6673
}
6774

0 commit comments

Comments
 (0)