will
will

Reputation: 53

GAE : objectify delete by id

I'm trying to delete a record from the GAE datastore via an ajax query which sends the object "primary key" (Long Id with auto increment).

Currently, I'm doing this (hard coded the key=6):

Objectify ofy = ObjectifyService.begin();
ofy.delete( Test1.class , 6);

This works : it deletes the entity which has the Key=6. But for security reasons, I need another parameter (fyi : "parent_user") so only the owner can delete this object. It seems Objectify.delete() doesn't allow to pass more parameters than the key...

How could I solve this ? Because making a Objectify.get() with my optional parameters+key to get the full object then sending the whole object to the delete() is nubish & unoptimized...

Upvotes: 3

Views: 2443

Answers (2)

Ricky Helgesson
Ricky Helgesson

Reputation: 3586

If your data model allows you to let the user be the Datastore ancestor of your objects, you can get rid of the query, since the ancestor is part of the key.

What I often do is to authenticate the user in the beginning of every request, which uses the @Cached annotation of Objectify to cache all users (and their privileges, which are embedded into the user).

Then, most of the user related data has the user as the ancestor. This way, whenever a user tries to access or delete a resource, I will never accidently allow the user to do it on any objects that isn't hers. All-in-all, only gets which are quick and cachable.

Upvotes: 0

Thanos Makris
Thanos Makris

Reputation: 3115

As presented at http://objectify-appengine.googlecode.com/svn/trunk/javadoc/index.html, Objectify.delete() does not take any additional parameters besides object keys, ids, or strings.

So, you need to first get the object based on your filters and then delete them. However, in order to optimize this, you can get only the key of the object and not the full object. Then you delete based on the key.

Hope this helps!

Upvotes: 3

Related Questions