You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
16 lines
551 B
16 lines
551 B
1 year ago
|
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
|