Cloud code New user role and ACL

195 views
Skip to first unread message

hris...@gmail.com

unread,
Jul 15, 2016, 3:41:33 AM7/15/16
to back{4}app
Hello there, 

Great job everyone!!! Well done!

I have a small issue with my cloud code. I have an aftersave fuction on Users which adds the new user to an existing Role and then changes his ACL so that only himself can read and write to his entry.
Unfortunately the user is not added to the Role and for the ACL It alters it but it is also adding "Public Read" to it.

Parse.Cloud.afterSave(Parse.User, function(request) {
  var user = request.user; 
  if (!user.existed()) {
  query = new Parse.Query("_Role");
  query.equalTo("name", "Admins");
  query.first({useMasterKey: true}).then(function(role){
  role.getUsers().add(user);
  role.relation("users").add(user);
  role.save(null, {useMasterKey: true});

        }).then(function(){
                                             
var userACL = new Parse.ACL(user);
user.setACL(userACL);
user.save(null, {useMasterKey: true});
});
  }
});

Thanks in advance for your help!

Best Regards.

hris...@gmail.com

unread,
Jul 19, 2016, 9:15:05 AM7/19/16
to back{4}app
Any thoughts on the issue?

Ricardo Seiji

unread,
Jul 19, 2016, 9:51:44 AM7/19/16
to back{4}app
Greetings, Captain!


I'll look into the proper solutions to your concerns.
But first, let me just focus on details:
- You might return the Promises to properly fit them on the ".then()" chain.
"role.save(null, {useMasterKey: true});" might become "return role.save(null, {useMasterKey: true});".
Same with "user.save()".

- I'll recommend the usage of "query = new Parse.Query(Parse.Role);" instead of "query = new Parse.Query("_Role");".

I still need to check another changes on the code, but this might be a good start.


Permission to leave, Captain? Aye aye!
Ricardo "Parrotless" Seiji.

hris...@gmail.com

unread,
Jul 20, 2016, 1:58:16 AM7/20/16
to back{4}app
Hello there!!!
Thank you for the tips, I have adopted them on my code.
Waiting for your thoughts on the issue as it is still bothering me!

Aye aye!!!

Davi Macêdo

unread,
Jul 25, 2016, 1:00:01 AM7/25/16
to back{4}app
Instead of:
role.getUsers().add(user);
role.relation("users").add(user);

Try to use just:
role.relation("users").add(user);

And let me know

hris...@gmail.com

unread,
Jul 25, 2016, 1:31:37 AM7/25/16
to back{4}app
Hello there!

Thank you for your reply.
I have tried that before but I also tried it again now in order to make sure.
Unfortunately nothing changed. 
Very strange! The code used to work perfectly when it was on Parse.

Davi Macêdo

unread,
Jul 25, 2016, 8:00:39 AM7/25/16
to back{4}app
Hi, please select a time slot in the below:

https://calendly.com/back4app/15min/07-25-2016


One of our engineers will debug together with you and sort the issue.

hris...@gmail.com

unread,
Jul 26, 2016, 8:23:54 AM7/26/16
to back{4}app
Replaced 
var user = request.user; 
with
var user = request.object; 

Thank you Davi!!!

Davi Macêdo

unread,
Jul 26, 2016, 10:29:24 AM7/26/16
to back{4}app
No worries :)

pankraci...@gmail.com

unread,
Oct 25, 2017, 4:03:05 PM10/25/17
to Back4App
Hi there, did you manage to resolve this problem? I have the same issue. Please post your working code if you can. Thanks

Davi Macêdo

unread,
Nov 28, 2017, 5:18:17 PM11/28/17
to Back4App
Hi, Pankracije.

Can you please paste your current code here? I can help you to fix it.

Best!
Reply all
Reply to author
Forward
0 new messages