I use Django/Tastypie to manage my user collection.
Is it possible to allow anonymous users to POST in the API (when creating a new user at some endpoint) and restrict authenticated users to GET only their own user, but not all the users ?
Thanks for your help.
I found the easiest thing to do was subclass the Authentication class I'm using. Just override the
is_authenticated
method to returnTrue
when the method is POST.I put my validation in a subclass of
Validation
and overrideis_valid
.I do the GET filtering the same way Sampson does it above.
Yes, it's possible to do both.
Here's a simple example of how you would let an authenticated user GET only their own user JSON and not from all the other users: (Assuming you are using Django's built-in user infrastructure):
And here is a simple example of how you would let an anonymous user POST to create a new user (Caveat: this doesn't use Tastypie, strictly speaking)
Based on that, here's a script of how you might create a new user through the REST endpoint using curl: