30 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			30 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Python
		
	
	
	
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
 | 
						|
from rest_framework_jwt.serializers import jwt_decode_handler
 | 
						|
from rest_framework import exceptions
 | 
						|
from django.utils.translation import ugettext as _
 | 
						|
 | 
						|
from crm.models import Consumer
 | 
						|
from rbac.models import UserProfile
 | 
						|
    
 | 
						|
class MyTokenAuthentication(JSONWebTokenAuthentication):
 | 
						|
    def authenticate_credentials(self, payload):
 | 
						|
        """
 | 
						|
        返回登陆人员(学员或管理员)
 | 
						|
        """
 | 
						|
        id = payload['user_id']
 | 
						|
        if not id:
 | 
						|
            msg = _('签名有误.')
 | 
						|
            raise exceptions.AuthenticationFailed(msg)
 | 
						|
        if 'type' in payload and payload['type'] == 'consumer':
 | 
						|
            try:
 | 
						|
                user = Consumer.objects.get(id=id)
 | 
						|
            except Consumer.DoesNotExist:
 | 
						|
                msg = _('消费者不存在')
 | 
						|
                raise exceptions.AuthenticationFailed(msg)
 | 
						|
        else:
 | 
						|
            try:
 | 
						|
                user = UserProfile.objects.get(id=id)
 | 
						|
            except UserProfile.DoesNotExist:
 | 
						|
                msg = _('管理员不存在')
 | 
						|
                raise exceptions.AuthenticationFailed(msg)
 | 
						|
        return user |