Control access in afterSave Parse Server to modify User

17 Views Asked by At

In my App a User should write some data a class (named Contributions) and after that an afterSave trigger should add a relation from that user to the data. Therefore, I use

final contribution = ParseObject('Contributions')
      ..set('contribution', newContrib)
      ..set('roundId', round.toPointer())
      ..set('commonId', common.toPointer())
      ..set('userId', user.toPointer())
      ..setACL(ParseACL(owner: user));

After the data is saved an afterSave call is triggered. In this call I want to add a relation from the User to the new added data in Contributions. However, even though I specified the ACL (see upper), the afterSave does not save the new relation due to

I tried the following which leads to Parse error: Cannot modify user Yd9uidQxz9. However, I expected to be able to modify the User since I added ACL in my Flutter code.

Parse.Cloud.afterSave("Contributions", async (request) => {
  const contribution = request.object.get('contribution');
  const round = request.object.get('roundId');
  await round.fetch();
  
  const user = request.object.get('userId');
  await user.fetch();

  const query = new Parse.Query('Rounds');
  
  query.get(round.id).then(
    function(round) {
    round.increment('contribution', contribution);
    round.save();
  }).then(function(savedRound) {
      const userRelation = user.relation('contributions');
      userRelation.add(request.object);
      return user.save();
    }).catch(function(error) {
    throw "Got an error " + error.code + " : " + error.message;
  });
});
0

There are 0 best solutions below