Skip to content

Using the master key in a save hook #1658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
juliusbtesh opened this issue Apr 27, 2016 · 15 comments
Closed

Using the master key in a save hook #1658

juliusbtesh opened this issue Apr 27, 2016 · 15 comments

Comments

@juliusbtesh
Copy link

I am trying to refer to the Cloud Code example given for "beforeSave(Parse.Installation" and the function calls Parse.Cloud.useMasterKey(); But with the new Parse Server setups for the open source deployment, I read that I cannot use that line for using Mater Key anymore. How can I add that into this part of the function?

if (request.user) { request.object.set('user', request.user); } else { request.object.unset('user'); } response.success();

Thanks!

@hramos
Copy link
Contributor

hramos commented Apr 27, 2016

This is already covered in the migration guide, specifically in the document that covers compatibility issues between hosted Parse and Parse Server. We highly recommend going through each section to make sure your app transitions to Parse Server smoothly.

You may use Server Fault for questions about managing Parse Server.

For code-level and/or implementation-related questions or technical support, please refer to Stack Overflow.

@hramos hramos closed this as completed Apr 27, 2016
@juliusbtesh
Copy link
Author

Thanks for the reply. I have read that guide and it says to use "useMasterKey: true" however I have not seen an example as the one I posted above with "useMasterKey: true" inside the function.

This is the reason why I have posted here.

@hramos
Copy link
Contributor

hramos commented Apr 27, 2016

Apologies, I misread your question. As a reminder you can use three backticks () followed byjsorjavascript` to denote a JavaScript code section, this will help with formatting and make it easier to understand the question.

If I understood correctly, you're asking for the proper way of implementing the following in Parse Server, given that useMasterKey() is not available and there is no explicit call to save():

Parse.Cloud.beforeSave(Parse.Installation, function(req, res) {
  Parse.Cloud.useMasterKey();
  if (request.user) { 
    request.object.set('user', request.user); 
  } else { 
    request.object.unset('user'); 
  } 

  response.success();
});

@hramos hramos reopened this Apr 27, 2016
@hramos hramos changed the title For the Parse.Installation Cloud Code example : How to use Master Key?? Using the master key in a save hook Apr 27, 2016
@juliusbtesh
Copy link
Author

Yes sir. That is correct.

@hramos
Copy link
Contributor

hramos commented Apr 27, 2016

In this specific example you posted, I don't think you need to use the master key at all. The save should complete as expected, since the user updating their current installation should have write access to it.

@juliusbtesh
Copy link
Author

Why would this exact code be on the Parse "AnyPic" example page if the Master Key isn't needed??

@hramos
Copy link
Contributor

hramos commented Apr 28, 2016

That's a great question. I don't think it is needed. Did you find otherwise?

@juliusbtesh
Copy link
Author

I am not entirely sure because i cannot seem to get my app to receive the direct notifications even though the logs have no errors and the database shows an increased counter in the "installation" section

@juliusbtesh
Copy link
Author

I don't think the above code will work, I am currently using without the master key. I have logged in with another device and noticed that the same user had a second 'installation' row in the database. Any ideas??

@juliusbtesh
Copy link
Author

I have watched this video and I understand the way of using the master key
within the cloud code function, but I have asked how to use it with the
code I provided because it is a completely different function and I do not
see a place to put the master key.

On Sat, May 7, 2016 at 3:21 PM, Adam Farag [email protected] wrote:

@D34THSTALKER https://github.com/D34thStalker Check out this video, at
about 10:20 he goes through how to use the master key in cloud code
functions. https://youtu.be/wAQsY0XdatQ?t=10m20s


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#1658 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AD4LSrYA2qI3907oceaAO38eZzkaNfylks5p_OYtgaJpZM4IRJot
.

Julius Btesh
www.juliusbtesh.com http://www.juliusbtesh.com

@ahashp
Copy link

ahashp commented May 12, 2016

You can read the Javascript API documentation and you will see you can do it like 4 different ways
https://parse.com/docs/js/api/classes/Parse.Object.html#methods_save

I've tested this and it works:

object.save(null, { useMasterKey: true });

@hramos
Copy link
Contributor

hramos commented May 12, 2016

@innopage that does not directly address the issue, as this is a beforeSave hook. If you modify request.object within a beforeSave hook, the object will be saved implicitly. Therefore you cannot directly pass the useMasterKey parameter. In hosted Cloud Code, you could globally enable the master key, but this is not available in Parse Server.

My original point still stands -- I don't think it is necessary in the specific use case brought up by @D34THSTALKER, and I haven't seen any evidence to the contrary yet.

@Syttr
Copy link

Syttr commented Jun 13, 2016

@hramos I'm having a similar issue..

I have a object that was being created by a particular user and the ACL was set to them for this object. Another use can accept that reservation and it sets them as a pointer on the original object. Before migrating to parse server, I created a cloud function and used Parse.Cloud.useMasterKey() to allow the second user to accept the reservation and all worked as intended.

Now as I'm migrating to parse server I've removed the calls to Parse.Cloud.useMasterKey and added it like so reservation.save( { useMasterKey: true } ) .. I'm getting the error "Object not found." If I add back Parse.Cloud.useMasterKey it works again.

If I log request.master it is returning false in the beforeSave() with reservation.save( { useMasterKey: true } )

In my cloud function it's querying the object without any issues with passing useMasterKey: true, but the save is failing with the same param when removing Parse.Cloud.useMasterKey

p.s. my beforeSave isn't doing anything other then logging request.master here.

parse 1.8.5
parse-server 2.2.11

**edit: My bad, the second param is options on save..

For anyone else make sure to do:

.save(null, {useMasterKey:true})**

@flovilmart
Copy link
Contributor

Closing due to lack of activity, please update to latest parse-server version and reopen if the issue persist.

Don't forget to include your current:

  • node version
  • npm version
  • parse-server version
  • any relevant logs (VERBOSE=1 will enable verbose logging)

@drdaz
Copy link
Member

drdaz commented Feb 23, 2018

@Syttr Your post just saved my week. I would never have thought to add null as the first parameter of the save call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants