second commit

main
zcx 6 months ago
parent a9820ca7f3
commit d4c78b8d5d

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
auth/.DS_Store vendored

Binary file not shown.

@ -0,0 +1,5 @@
from rest_framework.throttling import AnonRateThrottle
class MyRateThrottle(AnonRateThrottle):
THROTTLE_RATES = {"anon": "5/min"}

@ -0,0 +1,45 @@
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication
from myapp.models import User
# 后台接口认证
class AdminTokenAuthtication(BaseAuthentication):
def authenticate(self, request):
adminToken = request.META.get("HTTP_ADMINTOKEN")
print("检查adminToken==>" + adminToken)
users = User.objects.filter(admin_token=adminToken)
"""
判定条件
1. 传了adminToken
2. 查到了该帐号
3. 该帐号是管理员或演示帐号
"""
if not adminToken or len(users) == 0 or users[0].role == '2':
raise exceptions.AuthenticationFailed("AUTH_FAIL_END")
else:
print('adminToken验证通过')
# 前台接口认证
class TokenAuthtication(BaseAuthentication):
def authenticate(self, request):
token = request.META.get("HTTP_TOKEN", "")
if token is not None:
print("检查token==>" + token)
users = User.objects.filter(token=token)
# print(users)
"""
判定条件
1. 传了token
2. 查到了该帐号
3. 该帐号是普通用户
"""
if not token or len(users) == 0 or (users[0].role in ['1', '3']):
raise exceptions.AuthenticationFailed("AUTH_FAIL_FRONT")
else:
print('token验证通过')
else:
print("检查token==>token 为空")
raise exceptions.AuthenticationFailed("AUTH_FAIL_FRONT")

BIN
permission/.DS_Store vendored

Binary file not shown.

@ -0,0 +1,12 @@
from myapp.models import User
def isDemoAdminUser(request):
adminToken = request.META.get("HTTP_ADMINTOKEN")
users = User.objects.filter(admin_token=adminToken)
if len(users) > 0:
user = users[0]
if user.role == '3': # 角色3表示演示帐号
print('演示帐号===>')
return True
return False
Loading…
Cancel
Save