2018-07-25 03:21:12 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
|
2020-07-15 06:35:48 +00:00
|
|
|
|
from django.utils.translation import ugettext as _
|
2020-10-14 07:17:06 +00:00
|
|
|
|
from rest_framework import status
|
2018-11-02 06:38:44 +00:00
|
|
|
|
from rest_framework.views import Response
|
|
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
2021-03-02 06:57:48 +00:00
|
|
|
|
from rest_framework.generics import RetrieveAPIView
|
|
|
|
|
from rest_framework.exceptions import PermissionDenied
|
2018-07-25 03:21:12 +00:00
|
|
|
|
|
2021-03-15 06:53:19 +00:00
|
|
|
|
from common.permissions import IsSuperUserOrAppUser, IsValidUser, UserCanAnyPermCurrentOrg
|
2020-11-06 02:46:04 +00:00
|
|
|
|
from common.drf.api import JMSBulkRelationModelViewSet
|
2020-07-20 02:42:22 +00:00
|
|
|
|
from .models import Organization, ROLE
|
2020-08-03 08:17:46 +00:00
|
|
|
|
from .serializers import (
|
|
|
|
|
OrgSerializer, OrgReadSerializer,
|
2020-11-06 02:46:04 +00:00
|
|
|
|
OrgRetrieveSerializer, OrgMemberSerializer,
|
2021-03-02 06:57:48 +00:00
|
|
|
|
OrgMemberAdminSerializer, OrgMemberUserSerializer,
|
|
|
|
|
CurrentOrgSerializer
|
2020-08-03 08:17:46 +00:00
|
|
|
|
)
|
2018-11-02 06:38:44 +00:00
|
|
|
|
from users.models import User, UserGroup
|
2021-03-15 09:46:43 +00:00
|
|
|
|
from assets.models import (
|
|
|
|
|
Asset, Domain, AdminUser, SystemUser, Label, Node, Gateway,
|
|
|
|
|
CommandFilter, CommandFilterRule, GatheredUser
|
|
|
|
|
)
|
|
|
|
|
from applications.models import Application
|
|
|
|
|
from perms.models import AssetPermission, ApplicationPermission
|
|
|
|
|
from orgs.utils import current_org, tmp_to_root_org
|
2018-11-02 06:38:44 +00:00
|
|
|
|
from common.utils import get_logger
|
2020-08-03 08:17:46 +00:00
|
|
|
|
from .filters import OrgMemberRelationFilterSet
|
2020-09-28 10:49:18 +00:00
|
|
|
|
from .models import OrganizationMember
|
|
|
|
|
|
2018-07-25 03:21:12 +00:00
|
|
|
|
|
2018-11-02 06:38:44 +00:00
|
|
|
|
logger = get_logger(__file__)
|
2018-07-25 03:21:12 +00:00
|
|
|
|
|
2018-11-02 06:38:44 +00:00
|
|
|
|
|
2021-03-15 09:46:43 +00:00
|
|
|
|
# 部分 org 相关的 model,需要清空这些数据之后才能删除该组织
|
|
|
|
|
org_related_models = [
|
|
|
|
|
User, UserGroup, Asset, Label, Domain, Gateway, Node, AdminUser, SystemUser, Label,
|
|
|
|
|
CommandFilter, CommandFilterRule, GatheredUser,
|
|
|
|
|
AssetPermission, ApplicationPermission,
|
|
|
|
|
Application,
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2018-11-02 06:38:44 +00:00
|
|
|
|
class OrgViewSet(BulkModelViewSet):
|
2021-01-07 02:53:10 +00:00
|
|
|
|
filterset_fields = ('name',)
|
2020-07-01 06:38:03 +00:00
|
|
|
|
search_fields = ('name', 'comment')
|
2018-07-25 03:21:12 +00:00
|
|
|
|
queryset = Organization.objects.all()
|
|
|
|
|
serializer_class = OrgSerializer
|
2018-07-25 07:05:28 +00:00
|
|
|
|
permission_classes = (IsSuperUserOrAppUser,)
|
2018-11-02 06:38:44 +00:00
|
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
2020-05-21 08:09:42 +00:00
|
|
|
|
mapper = {
|
|
|
|
|
'list': OrgReadSerializer,
|
|
|
|
|
'retrieve': OrgRetrieveSerializer
|
|
|
|
|
}
|
|
|
|
|
return mapper.get(self.action, super().get_serializer_class())
|
2018-11-02 06:38:44 +00:00
|
|
|
|
|
2021-03-15 09:46:43 +00:00
|
|
|
|
@tmp_to_root_org()
|
2021-06-03 03:16:54 +00:00
|
|
|
|
def get_data_from_model(self, org, model):
|
2018-11-02 06:38:44 +00:00
|
|
|
|
if model == User:
|
2021-05-17 11:11:55 +00:00
|
|
|
|
data = model.objects.filter(
|
2021-06-03 03:16:54 +00:00
|
|
|
|
orgs__id=org.id, m2m_org_members__role__in=[ROLE.USER, ROLE.ADMIN, ROLE.AUDITOR]
|
2021-05-17 11:11:55 +00:00
|
|
|
|
)
|
2021-03-15 09:46:43 +00:00
|
|
|
|
elif model == Node:
|
2021-06-03 03:16:54 +00:00
|
|
|
|
# 根节点不能手动删除,所以排除检查
|
|
|
|
|
data = model.objects.filter(org_id=org.id).exclude(parent_key='', key__regex=r'^[0-9]+$')
|
2018-11-02 06:38:44 +00:00
|
|
|
|
else:
|
2021-06-03 03:16:54 +00:00
|
|
|
|
data = model.objects.filter(org_id=org.id)
|
2018-11-02 06:38:44 +00:00
|
|
|
|
return data
|
|
|
|
|
|
2021-06-03 03:16:54 +00:00
|
|
|
|
def allow_bulk_destroy(self, qs, filtered):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def perform_destroy(self, instance):
|
|
|
|
|
if str(current_org) == str(instance):
|
|
|
|
|
msg = _('The current organization ({}) cannot be deleted'.format(current_org))
|
|
|
|
|
raise PermissionDenied(detail=msg)
|
|
|
|
|
|
2021-03-15 09:46:43 +00:00
|
|
|
|
for model in org_related_models:
|
2021-06-03 03:16:54 +00:00
|
|
|
|
data = self.get_data_from_model(instance, model)
|
|
|
|
|
if not data:
|
|
|
|
|
continue
|
|
|
|
|
msg = _(
|
|
|
|
|
'The organization have resource ({}) cannot be deleted'
|
|
|
|
|
).format(model._meta.verbose_name)
|
|
|
|
|
raise PermissionDenied(detail=msg)
|
|
|
|
|
|
|
|
|
|
super().perform_destroy(instance)
|
2018-11-02 06:38:44 +00:00
|
|
|
|
|
|
|
|
|
|
2020-08-03 08:17:46 +00:00
|
|
|
|
class OrgMemberRelationBulkViewSet(JMSBulkRelationModelViewSet):
|
2020-05-06 07:31:08 +00:00
|
|
|
|
permission_classes = (IsSuperUserOrAppUser,)
|
2020-08-03 08:17:46 +00:00
|
|
|
|
m2m_field = Organization.members.field
|
|
|
|
|
serializer_class = OrgMemberSerializer
|
|
|
|
|
filterset_class = OrgMemberRelationFilterSet
|
2020-11-02 08:40:51 +00:00
|
|
|
|
search_fields = ('user__name', 'user__username', 'org__name')
|
2020-05-06 07:31:08 +00:00
|
|
|
|
|
2021-03-30 01:39:30 +00:00
|
|
|
|
def get_queryset(self):
|
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
|
queryset = queryset.exclude(user__role=User.ROLE.APP)
|
|
|
|
|
return queryset
|
|
|
|
|
|
2020-08-03 08:17:46 +00:00
|
|
|
|
def perform_bulk_destroy(self, queryset):
|
|
|
|
|
objs = list(queryset.all().prefetch_related('user', 'org'))
|
|
|
|
|
queryset.delete()
|
|
|
|
|
self.send_m2m_changed_signal(objs, action='post_remove')
|
2020-11-06 02:46:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OrgMemberAdminRelationBulkViewSet(JMSBulkRelationModelViewSet):
|
|
|
|
|
permission_classes = (IsSuperUserOrAppUser,)
|
|
|
|
|
m2m_field = Organization.members.field
|
|
|
|
|
serializer_class = OrgMemberAdminSerializer
|
|
|
|
|
filterset_class = OrgMemberRelationFilterSet
|
|
|
|
|
search_fields = ('user__name', 'user__username', 'org__name')
|
2020-11-24 11:09:14 +00:00
|
|
|
|
lookup_field = 'user_id'
|
2020-11-06 02:46:04 +00:00
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
|
org_id = self.kwargs.get('org_id')
|
|
|
|
|
queryset = queryset.filter(org_id=org_id, role=ROLE.ADMIN)
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
def perform_bulk_create(self, serializer):
|
|
|
|
|
data = serializer.validated_data
|
|
|
|
|
relations = [OrganizationMember(**i) for i in data]
|
|
|
|
|
OrganizationMember.objects.bulk_create(relations, ignore_conflicts=True)
|
|
|
|
|
|
|
|
|
|
def perform_bulk_destroy(self, queryset):
|
|
|
|
|
objs = list(queryset.all().prefetch_related('user', 'org'))
|
|
|
|
|
queryset.delete()
|
|
|
|
|
self.send_m2m_changed_signal(objs, action='post_remove')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OrgMemberUserRelationBulkViewSet(JMSBulkRelationModelViewSet):
|
|
|
|
|
permission_classes = (IsSuperUserOrAppUser,)
|
|
|
|
|
m2m_field = Organization.members.field
|
|
|
|
|
serializer_class = OrgMemberUserSerializer
|
|
|
|
|
filterset_class = OrgMemberRelationFilterSet
|
|
|
|
|
search_fields = ('user__name', 'user__username', 'org__name')
|
2020-11-24 11:09:14 +00:00
|
|
|
|
lookup_field = 'user_id'
|
2020-11-06 02:46:04 +00:00
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
|
org_id = self.kwargs.get('org_id')
|
|
|
|
|
queryset = queryset.filter(org_id=org_id, role=ROLE.USER)
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
def perform_bulk_create(self, serializer):
|
|
|
|
|
data = serializer.validated_data
|
|
|
|
|
relations = [OrganizationMember(**i) for i in data]
|
|
|
|
|
OrganizationMember.objects.bulk_create(relations, ignore_conflicts=True)
|
|
|
|
|
|
|
|
|
|
def perform_bulk_destroy(self, queryset):
|
|
|
|
|
objs = list(queryset.all().prefetch_related('user', 'org'))
|
|
|
|
|
queryset.delete()
|
|
|
|
|
self.send_m2m_changed_signal(objs, action='post_remove')
|
2021-03-02 06:57:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CurrentOrgDetailApi(RetrieveAPIView):
|
|
|
|
|
serializer_class = CurrentOrgSerializer
|
2021-03-15 06:53:19 +00:00
|
|
|
|
permission_classes = (IsValidUser, UserCanAnyPermCurrentOrg)
|
2021-03-02 06:57:48 +00:00
|
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
|
return current_org
|