add user problem permission

This commit is contained in:
virusdefender 2017-02-10 10:38:32 +08:00
parent 0678dd4fca
commit 802f27a516
5 changed files with 49 additions and 5 deletions

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.12 on 2017-02-09 10:28
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('account', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='user',
name='problem_permission',
field=models.CharField(default='None', max_length=24),
),
migrations.AlterField(
model_name='user',
name='admin_type',
field=models.CharField(default='Regular User', max_length=24),
),
]

View File

@ -14,6 +14,12 @@ class ProblemSolutionStatus(object):
PENDING = 2
class ProblemPermission(object):
NONE = "None"
OWN = "Own"
ALL = "All"
class UserManager(models.Manager):
use_in_migrations = True
@ -28,6 +34,7 @@ class User(AbstractBaseUser):
create_time = models.DateTimeField(auto_now_add=True, null=True)
# One of UserType
admin_type = models.CharField(max_length=24, default=AdminType.REGULAR_USER)
problem_permission = models.CharField(max_length=24, default=ProblemPermission.NONE)
reset_password_token = models.CharField(max_length=40, null=True)
reset_password_token_expire_time = models.DateTimeField(null=True)
# SSO auth token

View File

@ -1,6 +1,6 @@
from utils.api import DateTimeTZField, serializers
from .models import AdminType, User
from .models import AdminType, User, ProblemPermission
class UserLoginSerializer(serializers.Serializer):
@ -28,7 +28,7 @@ class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ["id", "username", "real_name", "email", "admin_type",
fields = ["id", "username", "real_name", "email", "admin_type", "problem_permission",
"create_time", "last_login", "two_factor_auth", "open_api", "is_disabled"]
@ -39,6 +39,8 @@ class EditUserSerializer(serializers.Serializer):
password = serializers.CharField(max_length=30, min_length=6, allow_blank=True, required=False, default=None)
email = serializers.EmailField(max_length=254)
admin_type = serializers.ChoiceField(choices=(AdminType.REGULAR_USER, AdminType.ADMIN, AdminType.SUPER_ADMIN))
problem_permission = serializers.ChoiceField(choices=(ProblemPermission.NONE, ProblemPermission.OWN,
ProblemPermission.ALL))
open_api = serializers.BooleanField()
two_factor_auth = serializers.BooleanField()
is_disabled = serializers.BooleanField()

View File

@ -8,7 +8,7 @@ from otpauth import OtpAuth
from utils.api.tests import APIClient, APITestCase
from utils.shortcuts import rand_str
from .models import AdminType, User
from .models import AdminType, User, ProblemPermission
class PermissionDecoratorTest(APITestCase):
@ -182,7 +182,8 @@ class AdminUserTest(APITestCase):
self.url = self.reverse("user_admin_api")
self.data = {"id": self.regular_user.id, "username": self.username, "real_name": "test_name",
"email": "test@qq.com", "admin_type": AdminType.REGULAR_USER,
"open_api": True, "two_factor_auth": False, "is_disabled": False}
"problem_permission": ProblemPermission.OWN, "open_api": True,
"two_factor_auth": False, "is_disabled": False}
def test_user_list(self):
response = self.client.get(self.url)
@ -198,6 +199,7 @@ class AdminUserTest(APITestCase):
self.assertEqual(resp_data["open_api"], True)
self.assertEqual(resp_data["two_factor_auth"], False)
self.assertEqual(resp_data["is_disabled"], False)
self.assertEqual(resp_data["problem_permission"], ProblemPermission.NONE)
self.assertTrue(self.regular_user.check_password("test"))

View File

@ -6,7 +6,7 @@ from utils.api import APIView, validate_serializer
from utils.shortcuts import rand_str
from ..decorators import super_admin_required
from ..models import User
from ..models import User, AdminType, ProblemPermission
from ..serializers import EditUserSerializer, UserSerializer
@ -45,6 +45,13 @@ class UserAdminAPI(APIView):
user.admin_type = data["admin_type"]
user.is_disabled = data["is_disabled"]
if data["admin_type"] == AdminType.ADMIN:
user.problem_permission = data["problem_permission"]
elif data["admin_type"] == AdminType.SUPER_ADMIN:
user.problem_permission = ProblemPermission.ALL
else:
user.problem_permission = ProblemPermission.NONE
if data["password"]:
user.set_password(data["password"])
@ -62,6 +69,7 @@ class UserAdminAPI(APIView):
user.tfa_token = rand_str()
else:
user.tfa_token = None
user.two_factor_auth = data["two_factor_auth"]
user.save()