添加判断用户登录是否需要验证码的API

This commit is contained in:
sxw@401
2015-09-17 10:24:01 +08:00
parent 03ef1e49f3
commit 880a5139b2
2 changed files with 29 additions and 1 deletions

View File

@@ -26,6 +26,14 @@ class UserLoginAPIView(APIView):
serializer = UserLoginSerializer(data=request.data) serializer = UserLoginSerializer(data=request.data)
if serializer.is_valid(): if serializer.is_valid():
data = serializer.data data = serializer.data
user = User.objects.get(username=data["username"])
# 只有管理员才适用验证码登录
if user.admin_type > 0:
if not "captcha" in data:
return error_response(u"请填写验证码!")
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
return error_response(u"验证码错误")
user = auth.authenticate(username=data["username"], password=data["password"]) user = auth.authenticate(username=data["username"], password=data["password"])
# 用户名或密码错误的话 返回None # 用户名或密码错误的话 返回None
if user: if user:
@@ -64,6 +72,9 @@ class UserRegisterAPIView(APIView):
serializer = UserRegisterSerializer(data=request.data) serializer = UserRegisterSerializer(data=request.data)
if serializer.is_valid(): if serializer.is_valid():
data = serializer.data data = serializer.data
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
return error_response(u"验证码错误")
try: try:
User.objects.get(username=data["username"]) User.objects.get(username=data["username"])
return error_response(u"用户名已存在") return error_response(u"用户名已存在")
@@ -206,3 +217,19 @@ class UserInfoAPIView(APIView):
response_serializer: UserSerializer response_serializer: UserSerializer
""" """
return success_response(UserSerializer(request.user).data) return success_response(UserSerializer(request.user).data)
class AccountSecurityAPIView(APIView):
def get(self, request):
"""
判断用户登录是否需要验证码
---
"""
username = request.GET.get("username", None)
if username:
try:
User.objects.get(username=username, admin_type__gt=0)
except User.DoesNotExist:
return success_response({"applied_captcha":False})
return success_response({"applied_captcha":True})
return success_response({"applied_captcha":False})

View File

@@ -4,7 +4,7 @@ from django.views.generic import TemplateView
from account.views import (UserLoginAPIView, UsernameCheckAPIView, UserRegisterAPIView, from account.views import (UserLoginAPIView, UsernameCheckAPIView, UserRegisterAPIView,
UserChangePasswordAPIView, EmailCheckAPIView, UserChangePasswordAPIView, EmailCheckAPIView,
UserAdminAPIView, UserInfoAPIView) UserAdminAPIView, UserInfoAPIView, AccountSecurityAPIView)
from announcement.views import AnnouncementAdminAPIView from announcement.views import AnnouncementAdminAPIView
@@ -116,4 +116,5 @@ urlpatterns = [
url(r'^api/submission/share/$', SubmissionShareAPIView.as_view(), name="submission_share_api"), url(r'^api/submission/share/$', SubmissionShareAPIView.as_view(), name="submission_share_api"),
url(r'^captcha/$', "utils.captcha.views.show_captcha", name="show_captcha"), url(r'^captcha/$', "utils.captcha.views.show_captcha", name="show_captcha"),
url(r'^api/account_security_check/$', AccountSecurityAPIView.as_view(), name="account_security_check"),
] ]