add user problem permission
This commit is contained in:
25
account/migrations/0002_auto_20170209_1028.py
Normal file
25
account/migrations/0002_auto_20170209_1028.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -14,6 +14,12 @@ class ProblemSolutionStatus(object):
|
|||||||
PENDING = 2
|
PENDING = 2
|
||||||
|
|
||||||
|
|
||||||
|
class ProblemPermission(object):
|
||||||
|
NONE = "None"
|
||||||
|
OWN = "Own"
|
||||||
|
ALL = "All"
|
||||||
|
|
||||||
|
|
||||||
class UserManager(models.Manager):
|
class UserManager(models.Manager):
|
||||||
use_in_migrations = True
|
use_in_migrations = True
|
||||||
|
|
||||||
@@ -28,6 +34,7 @@ class User(AbstractBaseUser):
|
|||||||
create_time = models.DateTimeField(auto_now_add=True, null=True)
|
create_time = models.DateTimeField(auto_now_add=True, null=True)
|
||||||
# One of UserType
|
# One of UserType
|
||||||
admin_type = models.CharField(max_length=24, default=AdminType.REGULAR_USER)
|
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 = models.CharField(max_length=40, null=True)
|
||||||
reset_password_token_expire_time = models.DateTimeField(null=True)
|
reset_password_token_expire_time = models.DateTimeField(null=True)
|
||||||
# SSO auth token
|
# SSO auth token
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from utils.api import DateTimeTZField, serializers
|
from utils.api import DateTimeTZField, serializers
|
||||||
|
|
||||||
from .models import AdminType, User
|
from .models import AdminType, User, ProblemPermission
|
||||||
|
|
||||||
|
|
||||||
class UserLoginSerializer(serializers.Serializer):
|
class UserLoginSerializer(serializers.Serializer):
|
||||||
@@ -28,7 +28,7 @@ class UserSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
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"]
|
"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)
|
password = serializers.CharField(max_length=30, min_length=6, allow_blank=True, required=False, default=None)
|
||||||
email = serializers.EmailField(max_length=254)
|
email = serializers.EmailField(max_length=254)
|
||||||
admin_type = serializers.ChoiceField(choices=(AdminType.REGULAR_USER, AdminType.ADMIN, AdminType.SUPER_ADMIN))
|
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()
|
open_api = serializers.BooleanField()
|
||||||
two_factor_auth = serializers.BooleanField()
|
two_factor_auth = serializers.BooleanField()
|
||||||
is_disabled = serializers.BooleanField()
|
is_disabled = serializers.BooleanField()
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from otpauth import OtpAuth
|
|||||||
from utils.api.tests import APIClient, APITestCase
|
from utils.api.tests import APIClient, APITestCase
|
||||||
from utils.shortcuts import rand_str
|
from utils.shortcuts import rand_str
|
||||||
|
|
||||||
from .models import AdminType, User
|
from .models import AdminType, User, ProblemPermission
|
||||||
|
|
||||||
|
|
||||||
class PermissionDecoratorTest(APITestCase):
|
class PermissionDecoratorTest(APITestCase):
|
||||||
@@ -182,7 +182,8 @@ class AdminUserTest(APITestCase):
|
|||||||
self.url = self.reverse("user_admin_api")
|
self.url = self.reverse("user_admin_api")
|
||||||
self.data = {"id": self.regular_user.id, "username": self.username, "real_name": "test_name",
|
self.data = {"id": self.regular_user.id, "username": self.username, "real_name": "test_name",
|
||||||
"email": "test@qq.com", "admin_type": AdminType.REGULAR_USER,
|
"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):
|
def test_user_list(self):
|
||||||
response = self.client.get(self.url)
|
response = self.client.get(self.url)
|
||||||
@@ -198,6 +199,7 @@ class AdminUserTest(APITestCase):
|
|||||||
self.assertEqual(resp_data["open_api"], True)
|
self.assertEqual(resp_data["open_api"], True)
|
||||||
self.assertEqual(resp_data["two_factor_auth"], False)
|
self.assertEqual(resp_data["two_factor_auth"], False)
|
||||||
self.assertEqual(resp_data["is_disabled"], False)
|
self.assertEqual(resp_data["is_disabled"], False)
|
||||||
|
self.assertEqual(resp_data["problem_permission"], ProblemPermission.NONE)
|
||||||
|
|
||||||
self.assertTrue(self.regular_user.check_password("test"))
|
self.assertTrue(self.regular_user.check_password("test"))
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from utils.api import APIView, validate_serializer
|
|||||||
from utils.shortcuts import rand_str
|
from utils.shortcuts import rand_str
|
||||||
|
|
||||||
from ..decorators import super_admin_required
|
from ..decorators import super_admin_required
|
||||||
from ..models import User
|
from ..models import User, AdminType, ProblemPermission
|
||||||
from ..serializers import EditUserSerializer, UserSerializer
|
from ..serializers import EditUserSerializer, UserSerializer
|
||||||
|
|
||||||
|
|
||||||
@@ -45,6 +45,13 @@ class UserAdminAPI(APIView):
|
|||||||
user.admin_type = data["admin_type"]
|
user.admin_type = data["admin_type"]
|
||||||
user.is_disabled = data["is_disabled"]
|
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"]:
|
if data["password"]:
|
||||||
user.set_password(data["password"])
|
user.set_password(data["password"])
|
||||||
|
|
||||||
@@ -62,6 +69,7 @@ class UserAdminAPI(APIView):
|
|||||||
user.tfa_token = rand_str()
|
user.tfa_token = rand_str()
|
||||||
else:
|
else:
|
||||||
user.tfa_token = None
|
user.tfa_token = None
|
||||||
|
|
||||||
user.two_factor_auth = data["two_factor_auth"]
|
user.two_factor_auth = data["two_factor_auth"]
|
||||||
|
|
||||||
user.save()
|
user.save()
|
||||||
|
|||||||
Reference in New Issue
Block a user