2016-07-27 11 views
0

私の質問は、自分のカスタム認証を正しく書く方法です。カスタム認証がdjango-tastypieで動作しない

私はこれに従うことを試みた

:私は管理者のユーザー名とパスワードを提供するAPIを呼び出すとき、それはいつもの基本的な方法、

api.py

def prepareResponce(responceData): 
    """Prepares a Json responce with status 200""" 
    response = JsonResponse(responceData) 
    return response # {"foo": "bar"} 

class CustomBasicAuthentication(BasicAuthentication): 
    userID = None 
    userType = None 
    userAccess = None 
    userName = None 

    def is_authenticated(self, request, **kwargs): 
     if 'admin' in request.user.username: 
      return prepareResponce({'logged in': 'Admin' }) 
        #return True 
     return prepareResponce({'not allowed for':userName }) 


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


class UserResource(ModelResource): 
    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'user' 
     authentication = CustomBasicAuthentication() 
     allowed_methods = ['get', 'post'] 

を実装している http://django-tastypie.readthedocs.org/en/latest/authentication.html#implementing-your-own-authentication-authorization

をelse部分を返します。 どこが間違っていますか?

答えて

1

あなたはreturnを逃し、あなたが親is_authenticated機能を呼び出すことはありません:

def is_authenticated(self, request, **kwargs): 
    super(CustomBasicAuthentication, self).is_authenticated(request, **kwargs) 
    if 'admin' == request.user.username: 
     return prepareResponce({'logged in': 'Admin' }) 
    return prepareResponce({'not allowed for': self.userName }) 
関連する問題