问题
I have a cloud code which creates two account roles when a user signs up. Below is the method
Parse.Cloud.afterSave("account", function(request) {
var accountName = request.object.get("name");
//create admin role
var adminRoleACL = new Parse.ACL();
adminRoleACL.setPublicWriteAccess(true);
var adminRole = new Parse.Role(accountName + "_Administrator", adminRoleACL);
adminRole.save() ;
//create user role
var userRoleACL = new Parse.ACL();
userRoleACL.setPublicWriteAccess(true);
var userRole = new Parse.Role(accountName + "_User", userRoleACL);
userRole.save();
});
Now what i wanted to achieve was to add the user which just signed up to these two roles. But unfortunately i saw that in cloud code i can't get the current user. So what i did was to add the users in the role after the roles are created from the client side. Below is the code for the same. The code executes fine and i did not see any error, however i did not see the users being added to the roles in the data browser. Any idea why is this happening? Am i missing something. I would be really thankful for all your help.
user.signUp(null, {
success : function(user) {
var currentUser = Parse.User.current();
var accountName = account.get("name");
var query = new Parse.Query(Parse.Role);
query.contains("name", accountName);
query.find({
success : function(roles) {
if (!roles) {
alert("No roles for " + accountName + " were found");
} else {
for (var i = 0; i < roles.length; i++) {
//add the user for admin role
//TODO: this needs to be done only once for the account owner
if (roles[i].get("name").search(USER_TYPE.ADMIN) >= 0) {
roles[i].getUsers().add(currentUser);
}
//add the user for user role
if (roles[i].get("name").search(USER_TYPE.USER) >= 0) {
roles[i].getUsers().add(currentUser);
}
var saved = roles[i].save();
}
alert("User was added into roles");
}
},
error : function(error) {
alert("Could not add users to the account " + accountName + " error: " + error.message);
}
});
alert("User created successfully");
},
error : function(user, error) {
alert("Error: " + error.code + " " + error.message);
}
});
回答1:
thanks for the help. I go it done by using a cloud code like this
Create the roles while creating the account.
Parse.Cloud.afterSave("account", function(request) { var accountName = request.object.get("name"); //create admin role var adminRoleACL = new Parse.ACL(); adminRoleACL.setPublicReadAccess(false); adminRoleACL.setPublicWriteAccess(false); var adminRole = new Parse.Role(accountName + "_Administrator", adminRoleACL); adminRole.save(); //create user role var userRoleACL = new Parse.ACL(); userRoleACL.setPublicReadAccess(false); userRoleACL.setPublicWriteAccess(false); var userRole = new Parse.Role(accountName + "_User", userRoleACL); userRole.save(); });
Then add the users to the created role
Parse.Cloud.define("addUsersToRole", function(request, response) { Parse.Cloud.useMasterKey(); var currentUser = request.user; var accountName = request.params.accountname; var isAdmin = request.params.admin; var query = new Parse.Query(Parse.Role); query.contains("name", accountName); query.find({ success : function(roles) { console.log("roles: " + roles.length); for (var i = 0; i < roles.length; i++) { if ( isAdmin = false && roles[i].get("name").search("_Administrator") >= 0) continue; roles[i].getUsers().add(currentUser); roles[i].save(); } response.success(); }, error : function(error) { response.error("error adding to admin role " + error); } }); });
回答2:
Actually you can access the user that is performing the request, check the Cloud Code Documentation for more details.
In particular, what you want is to look at request.user
:
var currentUser = request.user;
来源:https://stackoverflow.com/questions/18970416/parse-com-adding-users-to-created-roles