jumpserver/apps/perms/serializers/permission.py

122 lines
4.2 KiB
Python
Raw Normal View History

2016-10-19 11:30:55 +00:00
# -*- coding: utf-8 -*-
#
2021-07-30 11:13:47 +00:00
from django.db.models import Q
2022-11-11 07:04:31 +00:00
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
2021-04-29 11:10:45 +00:00
2022-08-17 03:54:18 +00:00
from assets.models import Asset, Node
2022-11-11 07:04:31 +00:00
from common.drf.fields import BitChoicesField, ObjectRelatedField
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
from perms.models import ActionChoices, AssetPermission
from users.models import User, UserGroup
2022-11-11 07:04:31 +00:00
__all__ = ["AssetPermissionSerializer", "ActionChoicesField"]
2022-11-11 07:04:31 +00:00
class ActionChoicesField(BitChoicesField):
def __init__(self, **kwargs):
2022-11-11 07:04:31 +00:00
super().__init__(ActionChoices, **kwargs)
2019-06-30 12:10:34 +00:00
class AssetPermissionSerializer(BulkOrgResourceModelSerializer):
2022-11-09 03:43:55 +00:00
users = ObjectRelatedField(queryset=User.objects, many=True, required=False)
2022-11-11 07:04:31 +00:00
user_groups = ObjectRelatedField(
queryset=UserGroup.objects, many=True, required=False
)
2022-11-09 03:43:55 +00:00
assets = ObjectRelatedField(queryset=Asset.objects, many=True, required=False)
nodes = ObjectRelatedField(queryset=Node.objects, many=True, required=False)
2022-11-11 07:04:31 +00:00
actions = ActionChoicesField(required=False, allow_null=True, label=_("Actions"))
2021-07-30 11:13:47 +00:00
is_valid = serializers.BooleanField(read_only=True, label=_("Is valid"))
2022-11-11 07:04:31 +00:00
is_expired = serializers.BooleanField(read_only=True, label=_("Is expired"))
accounts = serializers.ListField(label=_("Accounts"), required=False)
2018-04-10 12:29:06 +00:00
2018-02-01 09:14:15 +00:00
class Meta:
2018-04-08 12:02:40 +00:00
model = AssetPermission
2022-11-11 07:04:31 +00:00
fields_mini = ["id", "name"]
fields_small = fields_mini + [
2022-11-11 07:04:31 +00:00
"accounts",
"is_active",
"is_expired",
"is_valid",
"actions",
"created_by",
"date_created",
"date_expired",
"date_start",
"comment",
"from_ticket",
2020-05-09 06:51:19 +00:00
]
fields_m2m = [
2022-11-11 07:04:31 +00:00
"users",
"user_groups",
"assets",
"nodes",
2020-05-09 06:51:19 +00:00
]
fields = fields_small + fields_m2m
2022-11-11 07:04:31 +00:00
read_only_fields = ["created_by", "date_created", "from_ticket"]
2020-11-11 02:27:18 +00:00
extra_kwargs = {
2022-11-11 07:04:31 +00:00
"actions": {"label": _("Actions")},
"is_expired": {"label": _("Is expired")},
"is_valid": {"label": _("Is valid")},
2020-11-11 02:27:18 +00:00
}
2020-05-09 06:51:19 +00:00
2022-09-29 12:41:40 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_actions_field()
def set_actions_field(self):
2022-11-11 07:04:31 +00:00
actions = self.fields.get("actions")
2022-09-29 12:41:40 +00:00
if not actions:
return
choices = actions._choices
actions._choices = choices
actions.default = list(choices.keys())
2020-05-09 06:51:19 +00:00
@classmethod
def setup_eager_loading(cls, queryset):
2022-11-11 07:04:31 +00:00
"""Perform necessary eager loading of data."""
2021-07-30 11:13:47 +00:00
queryset = queryset.prefetch_related(
2022-11-11 07:04:31 +00:00
"users",
"user_groups",
"assets",
"nodes",
2021-07-30 11:13:47 +00:00
)
2020-05-09 06:51:19 +00:00
return queryset
2021-07-30 11:13:47 +00:00
@staticmethod
def perform_display_create(instance, **kwargs):
# 用户
users_to_set = User.objects.filter(
2022-11-11 07:04:31 +00:00
Q(name__in=kwargs.get("users_display"))
| Q(username__in=kwargs.get("users_display"))
).distinct()
instance.users.add(*users_to_set)
# 用户组
2021-07-30 11:13:47 +00:00
user_groups_to_set = UserGroup.objects.filter(
2022-11-11 07:04:31 +00:00
name__in=kwargs.get("user_groups_display")
2021-07-30 11:13:47 +00:00
).distinct()
instance.user_groups.add(*user_groups_to_set)
# 资产
assets_to_set = Asset.objects.filter(
2022-11-11 07:04:31 +00:00
Q(address__in=kwargs.get("assets_display"))
| Q(name__in=kwargs.get("assets_display"))
).distinct()
instance.assets.add(*assets_to_set)
# 节点
2021-07-30 11:13:47 +00:00
nodes_to_set = Node.objects.filter(
2022-11-11 07:04:31 +00:00
full_value__in=kwargs.get("nodes_display")
2021-07-30 11:13:47 +00:00
).distinct()
instance.nodes.add(*nodes_to_set)
def create(self, validated_data):
display = {
2022-11-11 07:04:31 +00:00
"users_display": validated_data.pop("users_display", ""),
"user_groups_display": validated_data.pop("user_groups_display", ""),
"assets_display": validated_data.pop("assets_display", ""),
"nodes_display": validated_data.pop("nodes_display", ""),
}
instance = super().create(validated_data)
self.perform_display_create(instance, **display)
return instance