parent
a1e220a67f
commit
f9acb2a120
5 changed files with 75 additions and 3 deletions
@ -0,0 +1,11 @@ |
||||
import rest_framework.permissions |
||||
|
||||
|
||||
def jwt_response_payload_handler(token, user=None, request=None): |
||||
return { |
||||
'token': token, |
||||
'id': user.id, |
||||
'username': user.username, |
||||
'email': user.email, |
||||
'is_active': user.is_active, |
||||
} |
@ -0,0 +1,15 @@ |
||||
from rest_framework.permissions import BasePermission |
||||
from rest_framework_jwt.authentication import jwt_decode_handler |
||||
|
||||
|
||||
class IsOwnerOrReadOnly(BasePermission): |
||||
def has_permission(self, request, view): |
||||
if request.user.username == 'admin': |
||||
return True |
||||
|
||||
def has_object_permission(self, request, view, obj): |
||||
token = request.META['HTTP_AUTHORIZATION'][5:] |
||||
token_user = jwt_decode_handler(token) # 解析token |
||||
if token_user: |
||||
return obj.user.id == token_user['user_id'] |
||||
return False |
Loading…
Reference in new issue