2013-09-23 5 views
5

私のDjangoアプリケーションで認証されたユーザーの詳細を取得しようとしています。そのためにapply_authorization_limitsが呼び出されていません

私は、新しいリソースを作成:

class MyUserResource(ModelResource): 
    class Meta: 
     queryset = ReaderUser.objects.all() 
     resource_name = 'me' 
     list_allowed_methods = [] 
     detail_allowed_methods = ['get'] 
     authorization = Authorization() 
     authentication = SessionAuthentication() 
     excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login') 

    def apply_authorization_limits(self, request, object_list): 
     print request.user 
     return object_list.filter(pk=request.user.pk) 

    def prepend_urls(self): 
     return [ 
      url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"), 
     ] 

を私は/api/me/?format=json を使用して、私のAPIを呼び出すと、私は次しまっ:More than one resource is found at this URI.

私もprepend_urlsずに試してみました。 は、私が理解していないことprint文はメソッドapply_authorization_limits

私が間違っているのかについての任意のヒントに実行されることはありませんということですか?

答えて

4

私は私の問題を解決するには、2つの方法を見つけました:

最初のものは私自身の認可を作成し2です。

私の場合は

、以下:

from tastypie.authorization import Authorization 

class SimpleReaderAuthorization(Authorization): 
    def read_list(self, object_list, bundle): 
     return object_list.filter(email=bundle.request.user.email) 

そして私はちょうど私のリソースを更新する必要があります。

class MyUserResource(ModelResource): 
    class Meta: 
     queryset = ReaderUser.objects.all() 
     resource_name = 'me' 
     list_allowed_methods = ['get'] 
     authorization = SimpleReaderAuthorization() 
     authentication = SessionAuthentication() 
     excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login') 

documentationに示すように、もう一つの簡単な方法は、以下のことを行うことです。

def get_object_list(self, request): 
     return super(YourResource, self).get_object_list(request).filter(pk=request.user.pk) 

結論:私はそれがクリーナーであり、シンプルなので、2番目を選択しました。

+1

、私はむしろ、一緒に行きたい: 'デフGET_OBJECT(自己、リクエスト): リターンスーパー(YourResource、自己).get_object(PK =からrequest.user。pub) ' このようにして、リストの代わりに1つのオブジェクト(1つのオブジェクトが常に含まれます)を取得し、http:// blabla/me/12を要求するRESTアーキテクチャを尊重します。ここで、12は現在のユーザーです。 http:// blabla/me/13(そしてあなたが12人)をリクエストすると、404 HTTPコードが返されます。 –

+0

@ DavidW.Thanksあなたの助言に感謝します。あなたが正しいです、私はこれに戻り、あなたの解決策が理にかなっているかどうかを見てください。 –

+0

@DavidD。 read_list操作は、リソースのリストを返すときに使用されます。個々のitensをフィルタリングするには、read_detailを使用して、許可されていないリソースに404の代わりにUnauthorizedを返すことができます。 –

0

ドキュメントは表示されませんが、apply_authorization_limitsは推奨されなくなりました。 推奨されていないだけでなく、2013年2月のリソースライフサイクルからthis commitでリッピングされました。それがもう呼び出されない理由です。

当時の更新されたドキュメントはhereであり、これ以降変更されていません。

「read_list」は、updated documentationでお勧めしていますので、お試し版を確認してください。あることを

class MyUserResource(ModelResource): 
    class Meta: 
     queryset = ReaderUser.objects.all() 
     resource_name = 'me' 
     list_allowed_methods = [] 
     detail_allowed_methods = ['get'] 
     authentication = SessionAuthentication() 
     excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login') 

    def authorized_read_list(self, object_list, bundle): 
     print request.user 
     return object_list.filter(pk=bundle.request.user.pk) 

    def prepend_urls(self): 
     return [ 
      url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"), 
     ] 

注:あなたは(あなたのケースでは非常に簡単)リソースレベルの認証を使用することができ、

from tastypie.authorization import Authorization 
from tastypie.exceptions import Unauthorized 


class MyAuthorization(Authorization): 
    def read_list(self, object_list, bundle): 
     print request.user 
     return object_list.filter(pk=bundle.request.user.pk) 


class MyUserResource(ModelResource): 
    class Meta: 
     queryset = ReaderUser.objects.all() 
     resource_name = 'me' 
     list_allowed_methods = [] 
     detail_allowed_methods = ['get'] 
     authorization = MyAuthorization() 
     authentication = SessionAuthentication() 
     excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login') 

    def prepend_urls(self): 
     return [ 
      url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"), 
     ] 

をまたは:

def read_list(self, object_list, bundle): 
    """ 
    Returns a list of all the objects a user is allowed to read. 

    Should return an empty list if none are allowed. 

    Returns the entire list by default. 
    """ 
    return object_list 

あなたのコードは次のようになります変更しましたこの場合、「承認」メタは必要ありません。

「認可」メタの使用は、同じ認可クラスを共有する異なるリソースにアクセス許可を適用する共通かつ一般的な方法がある場合に適しています。第二の溶液について

関連する問題