1

私はdjangoプロジェクトを実装しました。これにはtastypieアプリが含まれています。クライアントは CRUD 操作にリクエストを送信できます。

ログに記録されたユーザーの詳細へのアクセスに問題があります。ここでは、BasicAuthentication + TokenBased Authentication を実装しようとしています。

これは私のカスタム実装です

from tastypie.authentication import Authentication
from tastypie.authorization import Authorization

class MyAuthentication(Authentication):

    def is_authenticated(self, request, **kwargs):
        return True

    from django.contrib.sessions.models import Session
    if 'sessionid' in request.COOKIES:
        try:
            s = Session.objects.get(pk=request.COOKIES['sessionid'])
        except Exception,e:
            return False

        # get the user_id from the session
        if '_auth_user_id' in s.get_decoded():
            # try to find the user associated with the given sessionid
            try:
                u = User.objects.get(pk=s.get_decoded()['_auth_user_id'])
                if u is not None:
                    return False
            except Exception,e:
                return False
        else:
            return False
    else:
        return False

    def get_identifier(self, request):
        return request.user.username

だから私のリソースファイル、私はMyAuthenticateクラスにアクセスしようとしています。

class InvoiceResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')
    class Meta:
        user = User.objects.get(pk=1)
        queryset = user.invoice_set.all()
        resource_name = 'invoice'
        authorization = Authorization()
        authentication = MyAuthentication();

ここで問題が発生しました。ログインしたユーザーに属するすべての請求書を読み込む必要があります。基本的に、ログに記録されたユーザー ID をこのクエリのパラメーターとして渡したかったのです。

user = User.objects.get(pk=1)

何か案が?。

4

1 に答える 1

0

最新バージョンについては、独自の認可の実装を参照してください。クックブック (2013 年 4 月 18 日現在) は古く、使用法apply_authorization_limitsは非推奨です。

新しいスキームは、カスタム承認を作成して適用することです。承認は、必要なものを何でもチェックできます。ドキュメントでは、UserObjectsOnlyAuthorizationあなたが話していることの例を参照してください。

于 2013-04-18T18:12:30.217 に答える