问题
I'm using Parse Server on AWS and its been going well. However I am unable to assign ACL as I did in the parse.com cloud code.
I have read The Compatibility Notes, Docs and Git Read me however there is very little mention of ACL.
The following answer has been tried, however it is not working for me.
My old code was like this :
Parse.Cloud.afterSave("Favourites", function(request) {
var user = request.user;
var newACL = new Parse.ACL(Parse.User.current())
newACL.setReadAccess(user, true);
newACL.setWriteAccess(user, true);
request.object.setACL(newACL);
request.object.save();
});
Obviously Parse.User.current()
cannot be used which has brought me to this:
Parse.Cloud.beforeSave("Favourites", function(request, res) {
var acl = new Parse.ACL();
acl.setReadAccess(request.object.id, true);
acl.setWriteAccess(request.object.id, true);
request.object.setACL(acl);
res.success();
});
However, this is not working for me.
I have also tried setting the ACL at the same time as saving using the following, however it still doesn't work for me.
var favourites = new Favourites();
var acl = new Parse.ACL();
acl.setReadAccess('request.object.id', true);
acl.setWriteAccess('request.object.id', true);
favourites.setACL(acl);
favourites.save({
user: request.user,
favourites: request.params.favourites
}, {
success: function(favourites) {
console.log('Great!')
},
error: function(favourites, error) {
console.log('The save failed');
}
});
EDIT
I have just tried doing things using the Javascript Docs:
var favourites = new Favourites();
var acl = new Parse.ACL(request.user);
acl.setReadAccess(request.user, true);
acl.setWriteAccess(request.user, true);
acl.setPublicWriteAccess(false);
acl.setPublicReadAccess(false);
favourites.setACL(acl);
favourites.set("favourites", request.params.favourites);
favourites.set("userId", request.user.id);
favourites.set("user", request.user);
favourites.save({}, { useMasterKey: true });
Still doesn't work!
It seems there is a serious lack of node.js cloud code examples in the Docs.
Any help would be greatly appreciated.
回答1:
This is all you need to do. I have add deny access fir the public, since it isn't totally clear what you want.
Parse.Cloud.afterSave("Favourites", function(request) {
var newACL = new Parse.ACL();
// Are you sure you don't need to disable public access?
newACL.setPublicReadAccess(false);
newACL.setPublicWriteAccess(false);
// Give access to user
newACL.setReadAccess(request.user, true);
newACL.setWriteAccess(request.user, true);
// Assign ACL to object
request.object.setACL(newACL);
// Use master key
request.object.save(null, {useMasterKey: true});
});
来源:https://stackoverflow.com/questions/37104632/setacls-on-parse-server