From 7a841214d44b79557c1aaf83a00a80fb09d280ff Mon Sep 17 00:00:00 2001 From: spxcds Date: Sun, 27 Mar 2016 01:26:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A6=81=E7=94=A8?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account/decorators.py | 6 ++++-- account/migrations/0019_user_is_forbidden.py | 20 ++++++++++++++++++++ account/models.py | 2 ++ account/serializers.py | 3 ++- account/views.py | 8 +++++++- static/src/js/app/admin/user/userList.js | 7 +++++-- template/src/admin/user/user_list.html | 6 +++++- 7 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 account/migrations/0019_user_is_forbidden.py diff --git a/account/decorators.py b/account/decorators.py index b0dbecbc..4fe3fc70 100644 --- a/account/decorators.py +++ b/account/decorators.py @@ -5,7 +5,7 @@ from functools import wraps from django.http import HttpResponseRedirect -from utils.shortcuts import error_response +from utils.shortcuts import error_response, error_page from .models import SUPER_ADMIN, ADMIN @@ -23,6 +23,8 @@ class BasePermissionDecorator(object): self.request = args[0] if self.check_permission(): + if self.request.user.is_forbidden is True: + return error_page(self.request, u"用户被禁用,请联系管理员") return self.func(*args, **kwargs) else: if self.request.is_ajax(): @@ -46,4 +48,4 @@ class super_admin_required(BasePermissionDecorator): class admin_required(BasePermissionDecorator): def check_permission(self): - return self.request.user.is_authenticated() and self.request.user.admin_type in [SUPER_ADMIN, ADMIN] \ No newline at end of file + return self.request.user.is_authenticated() and self.request.user.admin_type in [SUPER_ADMIN, ADMIN] diff --git a/account/migrations/0019_user_is_forbidden.py b/account/migrations/0019_user_is_forbidden.py new file mode 100644 index 00000000..a0266121 --- /dev/null +++ b/account/migrations/0019_user_is_forbidden.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2016-03-26 16:39 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0018_auto_20160217_0920'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='is_forbidden', + field=models.BooleanField(default=False), + ), + ] diff --git a/account/models.py b/account/models.py index abb21a03..033dbe6a 100644 --- a/account/models.py +++ b/account/models.py @@ -41,6 +41,8 @@ class User(AbstractBaseUser): tfa_token = models.CharField(max_length=40, blank=True, null=True) # open api key openapi_appkey = models.CharField(max_length=35, blank=True, null=True) + # 是否禁用用户 + is_forbidden = models.BooleanField(default=False) USERNAME_FIELD = 'username' REQUIRED_FIELDS = [] diff --git a/account/serializers.py b/account/serializers.py index d4f39950..fce610e2 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -39,7 +39,7 @@ class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ["id", "username", "real_name", "email", "admin_type", - "create_time", "last_login", "two_factor_auth", "openapi_appkey"] + "create_time", "last_login", "two_factor_auth", "openapi_appkey", "is_forbidden"] class EditUserSerializer(serializers.Serializer): @@ -51,6 +51,7 @@ class EditUserSerializer(serializers.Serializer): admin_type = serializers.IntegerField(default=0) openapi = serializers.BooleanField() tfa_auth = serializers.BooleanField() + is_forbidden_user = serializers.BooleanField() class ApplyResetPasswordSerializer(serializers.Serializer): diff --git a/account/views.py b/account/views.py index 4a6db826..ab3b3f40 100644 --- a/account/views.py +++ b/account/views.py @@ -65,7 +65,7 @@ class UserLoginAPIView(APIView): return serializer_invalid_response(serializer) -@login_required +#@login_required def logout(request): auth.logout(request) return http.HttpResponseRedirect("/") @@ -228,6 +228,12 @@ class UserAdminAPIView(APIView): user.two_factor_auth = True user.tfa_token = rand_str() + # 后台控制用户是否被禁用 + if data["is_forbidden_user"] is False: + user.is_forbidden = False + else: + user.is_forbidden = True + user.save() return success_response(UserSerializer(user).data) else: diff --git a/static/src/js/app/admin/user/userList.js b/static/src/js/app/admin/user/userList.js index 5a618ef8..df4960f6 100644 --- a/static/src/js/app/admin/user/userList.js +++ b/static/src/js/app/admin/user/userList.js @@ -22,6 +22,7 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "pager", "validator"], userId: -1, openAPI: false, tfa_auth: false, + is_forbidden_user: false, pager: { getPage: function (page) { @@ -36,6 +37,7 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "pager", "validator"], vm.userId = user.id; vm.tfa_auth = user.two_factor_auth; vm.openAPI = user.openapi_appkey ? true: false; + vm.is_forbidden_user = user.is_forbidden ? true: false; vm.isEditing = true; }, @@ -83,7 +85,8 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "pager", "validator"], id: vm.userId, admin_type: vm.adminType, openapi: vm.openAPI, - tfa_auth: vm.tfa_auth + tfa_auth: vm.tfa_auth, + is_forbidden_user: vm.is_forbidden_user }; if ($("#password").val() !== "") data.password = $("#password").val(); @@ -109,4 +112,4 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "pager", "validator"], }); avalon.scan(); - }); \ No newline at end of file + }); diff --git a/template/src/admin/user/user_list.html b/template/src/admin/user/user_list.html index ef621eb9..03b0e9fb 100644 --- a/template/src/admin/user/user_list.html +++ b/template/src/admin/user/user_list.html @@ -84,6 +84,10 @@ +
+ + +
@@ -91,4 +95,4 @@
- \ No newline at end of file + From 485462d702e39579ce6600076571ad715225b0e8 Mon Sep 17 00:00:00 2001 From: spxcds Date: Sun, 27 Mar 2016 18:29:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=A6=81=E7=94=A8=E5=8A=9F=E8=83=BD=E7=9A=84=E9=83=A8=E5=88=86?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account/decorators.py | 5 ++++- account/serializers.py | 2 +- account/views.py | 5 +---- static/src/js/app/admin/user/userList.js | 6 +++--- template/src/admin/user/user_list.html | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/account/decorators.py b/account/decorators.py index 4fe3fc70..2734ff0a 100644 --- a/account/decorators.py +++ b/account/decorators.py @@ -24,7 +24,10 @@ class BasePermissionDecorator(object): if self.check_permission(): if self.request.user.is_forbidden is True: - return error_page(self.request, u"用户被禁用,请联系管理员") + if self.request.is_ajax(): + return error_response(u"您已被禁用,请联系管理员") + else: + return error_page(self.request, u"您已被禁用,请联系管理员") return self.func(*args, **kwargs) else: if self.request.is_ajax(): diff --git a/account/serializers.py b/account/serializers.py index fce610e2..323a0203 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -51,7 +51,7 @@ class EditUserSerializer(serializers.Serializer): admin_type = serializers.IntegerField(default=0) openapi = serializers.BooleanField() tfa_auth = serializers.BooleanField() - is_forbidden_user = serializers.BooleanField() + is_forbidden = serializers.BooleanField() class ApplyResetPasswordSerializer(serializers.Serializer): diff --git a/account/views.py b/account/views.py index ab3b3f40..ce7f8ee0 100644 --- a/account/views.py +++ b/account/views.py @@ -229,10 +229,7 @@ class UserAdminAPIView(APIView): user.tfa_token = rand_str() # 后台控制用户是否被禁用 - if data["is_forbidden_user"] is False: - user.is_forbidden = False - else: - user.is_forbidden = True + user.is_forbidden = data["is_forbidden"] user.save() return success_response(UserSerializer(user).data) diff --git a/static/src/js/app/admin/user/userList.js b/static/src/js/app/admin/user/userList.js index df4960f6..b382f91c 100644 --- a/static/src/js/app/admin/user/userList.js +++ b/static/src/js/app/admin/user/userList.js @@ -22,7 +22,7 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "pager", "validator"], userId: -1, openAPI: false, tfa_auth: false, - is_forbidden_user: false, + is_forbidden: false, pager: { getPage: function (page) { @@ -37,7 +37,7 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "pager", "validator"], vm.userId = user.id; vm.tfa_auth = user.two_factor_auth; vm.openAPI = user.openapi_appkey ? true: false; - vm.is_forbidden_user = user.is_forbidden ? true: false; + vm.is_forbidden = user.is_forbidden ? true: false; vm.isEditing = true; }, @@ -86,7 +86,7 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "pager", "validator"], admin_type: vm.adminType, openapi: vm.openAPI, tfa_auth: vm.tfa_auth, - is_forbidden_user: vm.is_forbidden_user + is_forbidden : vm.is_forbidden }; if ($("#password").val() !== "") data.password = $("#password").val(); diff --git a/template/src/admin/user/user_list.html b/template/src/admin/user/user_list.html index 03b0e9fb..cc431e64 100644 --- a/template/src/admin/user/user_list.html +++ b/template/src/admin/user/user_list.html @@ -86,7 +86,7 @@
- +