Search code examples
parse-platformparse-cloud-code

Setting ACL of objects based on Parse Relation field


I have two Parse Classes Group and Post. Group have a parse relation field people (users, that are in this group) and Post have a pointer field group (which group, this post belong to).

When someone create a group, he/she add his friends to this group that are being saved in "people" field.

Everybody who is in the people relation can post in the group.

I have some questions about setting ACL on post and group object.

  1. When a group object is being saved. How to set its acl based on the "people" (parse relation field) in beforeSave (I don't want to set ACL on client side) ?
  2. When some one post in the group the acl of the post object should also be based on the parse relation field "people" from "Group" class. **Note that i want that acl of the post object should be dynamic in a way that when someone joined group later, he/she should be able to see previous posts from his/her joined date

Parse.Cloud.beforeSave(POST, function(request, response){
	Parse.Cloud.useMasterKey();
	
	var post = request.object;
	var owner = post.get("owner");
	var community = post.get("group")
	var draft = post.get("draft");
	
	if (!owner || !draft || !community) return response.error(PARAMETERS_NOT_FOUND);
	if (draft.length <1) return response.error(FIELD_SIZE_LIMIT_NOT_ENOUGH)

	var community = validateCommunity(community.id);
	if (!community) return response.error(FAKE_COMMUNITY);	
  
    var people_to_share = community.relation("people");
    
    var acl = new Parse.ACL();
    acl.setWriteAccess(owner, true);
    acl.setReadAccess(owner, true);
  
    // Now here i want to set the acl of post for all people_to_share .... And that is working good ... but the problem is when i add new person in community (people field) .... I have to do set acl for him in every post of community. 
	
post.setACL(acl);
response.success();

});


Solution

  • Yes. I have solved this Issue using a common role. I created a role for every group and saved it in the Group Class as a pointer. Whenever a new person is added to group. I give him read access to that group's role. And on removing person from that group. I remove his read access.

    Similarly, When new post is created, I just set post's acl to that group's role. It is good dynamic way.