I'm trying to set up a "general_user" role for my app so admin can ban a user by removing them from that role.
This code:
Parse.Cloud.afterSave(Parse.User, function(request, response) {
var user = request.object;
if (user.existed()) { return; }
var roleQuery = new Parse.Query(CONSTANTS.ROLE_CLASSNAME);
roleQuery.equalTo("name", CONSTANTS.GENERAL_USER_ROLENAME);
roleQuery.first(CONSTANTS.USE_MASTER_KEY)
.then(function(generalUserRole) {
if(generalUserRole)
{
generalUserRole.getUsers().add(user);
return generalUserRole.save();
}
else
{
var role = new Parse.Role(CONSTANTS.GENERAL_USER_ROLENAME, new Parse.ACL());
role.getUsers().add(user);
return role.save();
}
}).then(function(generalUserRole) {
var acl = new Parse.ACL();
acl.setWriteAccess(user, true);
acl.setRoleReadAccess(generalUserRole, true);
req.object.setACL(acl);
},
function(error) {
console.log(JSON.stringify(error));
});
});
Results in an ACL that's public read, user(write)
(Reading in the dashboard).
I've tried saving the saving the req.object after adding the ACL.
I've tried with and without res.success()
(which I understand is not necessary in afterSaves).
Thank you.
i tested your code and found some issues and fix them. The issues that i found were:
At the end your code should look like the following:
Parse.Cloud.afterSave(Parse.User, function (req) {
var user = req.object;
if (user.existed()) { return; }
var roleQuery = new Parse.Query(Parse.Role);
roleQuery.equalTo("name", "someRole");
roleQuery.first({ useMasterKey: true }).then(function (role) {
if (role) {
role.getUsers().add(user);
return role.save();
} else {
var myNewRole = new Parse.Role("someRole", new Parse.ACL());
myNewRole.getUsers().add(user);
return myNewRole.save()
}
}).then(function (userRole) {
var acl = new Parse.ACL();
acl.setWriteAccess(user, true);
acl.setRoleReadAccess(userRole, true);
user.setACL(acl);
return user.save(null,{ useMasterKey: true });
}, function (error) {
console.log(JSON.stringify(error));
});
});