mirror of https://github.com/jumpserver/jumpserver
parent
9bb7c1adc1
commit
cf719f492d
@ -0,0 +1,30 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
|
||||
from rest_framework_bulk import BulkModelViewSet
|
||||
from django.db.models import F
|
||||
|
||||
from common.permissions import IsOrgAdmin
|
||||
from .. import serializers
|
||||
from ..models import User
|
||||
|
||||
__all__ = ['UserUserGroupRelationViewSet']
|
||||
|
||||
|
||||
class UserUserGroupRelationViewSet(BulkModelViewSet):
|
||||
filter_fields = ('user', 'usergroup')
|
||||
search_fields = filter_fields
|
||||
serializer_class = serializers.UserUserGroupRelationSerializer
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = User.groups.through.objects.all()\
|
||||
.annotate(user_name=F('user__name'))\
|
||||
.annotate(usergroup_name=F('usergroup__name'))
|
||||
return queryset
|
||||
|
||||
def allow_bulk_destroy(self, qs, filtered):
|
||||
if filtered.count() != 1:
|
||||
return False
|
||||
else:
|
||||
return True
|
@ -1,3 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from .v1 import *
|
||||
from .user import *
|
||||
from .group import *
|
||||
from .realtion import *
|
||||
|
@ -0,0 +1,68 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
from common.fields import StringManyToManyField
|
||||
from common.serializers import AdaptedBulkListSerializer
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
from ..models import User, UserGroup
|
||||
from .. import utils
|
||||
|
||||
__all__ = [
|
||||
'UserGroupSerializer', 'UserGroupListSerializer',
|
||||
'UserGroupUpdateMemberSerializer',
|
||||
]
|
||||
|
||||
|
||||
class UserGroupSerializer(BulkOrgResourceModelSerializer):
|
||||
users = serializers.PrimaryKeyRelatedField(
|
||||
required=False, many=True, queryset=User.objects, label=_('User')
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = UserGroup
|
||||
list_serializer_class = AdaptedBulkListSerializer
|
||||
fields = [
|
||||
'id', 'name', 'users', 'comment', 'date_created',
|
||||
'created_by',
|
||||
]
|
||||
extra_kwargs = {
|
||||
'created_by': {'label': _('Created by'), 'read_only': True}
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.set_fields_queryset()
|
||||
|
||||
def set_fields_queryset(self):
|
||||
users_field = self.fields['users']
|
||||
users_field.child_relation.queryset = utils.get_current_org_members()
|
||||
|
||||
def validate_users(self, users):
|
||||
for user in users:
|
||||
if user.is_super_auditor:
|
||||
msg = _('Auditors cannot be join in the user group')
|
||||
raise serializers.ValidationError(msg)
|
||||
return users
|
||||
|
||||
|
||||
class UserGroupListSerializer(UserGroupSerializer):
|
||||
users = StringManyToManyField(many=True, read_only=True)
|
||||
|
||||
|
||||
class UserGroupUpdateMemberSerializer(serializers.ModelSerializer):
|
||||
users = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects)
|
||||
|
||||
class Meta:
|
||||
model = UserGroup
|
||||
fields = ['id', 'users']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.set_fields_queryset()
|
||||
|
||||
def set_fields_queryset(self):
|
||||
users_field = self.fields['users']
|
||||
users_field.child_relation.queryset = utils.get_current_org_members()
|
@ -0,0 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from rest_framework import serializers
|
||||
|
||||
from ..models import User
|
||||
|
||||
__all__ = ['UserUserGroupRelationSerializer']
|
||||
|
||||
|
||||
class UserUserGroupRelationSerializer(serializers.ModelSerializer):
|
||||
user_name = serializers.CharField(read_only=True)
|
||||
usergroup_name = serializers.CharField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = User.groups.through
|
||||
fields = ['id', 'user', 'user_name', 'usergroup', 'usergroup_name']
|
Loading…
Reference in new issue