mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-18 16:39:28 +00:00
reactor&remove: 重构applications模块 & 移除applications、perms中已不再使用的模块 (#5374)
* reactor: 重构applications模块 & 删除applications、perms中已不再使用的模块 * reactor: 1. 针对application.attrs字段的view-serializer映射逻辑,采用DynamicMapping的方案重写; * reactor: 2. 删除applications和perms模块中已不再使用的database-app/k8s-app/remote-app模块; * reactor: 添加迁移文件(删除perms/databaseperrmission/remoteapppermission/k8sapppermission) * reactor: 修改细节 Co-authored-by: Bai <bugatti_it@163.com>
This commit is contained in:
@@ -3,15 +3,4 @@
|
||||
|
||||
from .asset import *
|
||||
from .application import *
|
||||
|
||||
# TODO: 删除
|
||||
from .remote_app_permission import *
|
||||
from .remote_app_permission_relation import *
|
||||
from .user_remote_app_permission import *
|
||||
from .database_app_permission import *
|
||||
from .database_app_permission_relation import *
|
||||
from .user_database_app_permission import *
|
||||
from .system_user_permission import *
|
||||
from .k8s_app_permission import *
|
||||
from .k8s_app_permission_relation import *
|
||||
from .user_k8s_app_permission import *
|
||||
|
@@ -5,8 +5,9 @@ from django.db.models import Q
|
||||
from rest_framework.generics import ListAPIView
|
||||
|
||||
from common.permissions import IsOrgAdminOrAppUser
|
||||
from common.mixins.api import CommonApiMixin
|
||||
from applications.models import Application
|
||||
from applications.api.mixin import ApplicationAttrsSerializerViewMixin
|
||||
from applications.api.mixin import ApplicationViewMixin
|
||||
from perms import serializers
|
||||
|
||||
__all__ = [
|
||||
@@ -14,9 +15,9 @@ __all__ = [
|
||||
]
|
||||
|
||||
|
||||
class UserGroupGrantedApplicationsApi(ApplicationAttrsSerializerViewMixin, ListAPIView):
|
||||
class UserGroupGrantedApplicationsApi(ApplicationViewMixin, CommonApiMixin, ListAPIView):
|
||||
"""
|
||||
获取用户组直接授权的资产
|
||||
获取用户组直接授权的应用
|
||||
"""
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
serializer_class = serializers.ApplicationGrantedSerializer
|
||||
|
@@ -3,8 +3,9 @@
|
||||
from rest_framework.generics import ListAPIView
|
||||
from rest_framework.response import Response
|
||||
|
||||
from common.mixins.api import CommonApiMixin
|
||||
from applications.api.mixin import (
|
||||
SerializeApplicationToTreeNodeMixin, ApplicationAttrsSerializerViewMixin
|
||||
SerializeApplicationToTreeNodeMixin, ApplicationViewMixin
|
||||
)
|
||||
from perms import serializers
|
||||
from perms.api.asset.user_permission.mixin import ForAdminMixin, ForUserMixin
|
||||
@@ -21,7 +22,7 @@ __all__ = [
|
||||
]
|
||||
|
||||
|
||||
class AllGrantedApplicationsMixin(ApplicationAttrsSerializerViewMixin, ListAPIView):
|
||||
class AllGrantedApplicationsMixin(ApplicationViewMixin, CommonApiMixin, ListAPIView):
|
||||
only_fields = serializers.ApplicationGrantedSerializer.Meta.only_fields
|
||||
serializer_class = serializers.ApplicationGrantedSerializer
|
||||
filter_fields = ['id', 'name', 'category', 'type', 'comment']
|
||||
|
@@ -1,5 +1,3 @@
|
||||
from django.db.models import F
|
||||
from orgs.mixins.api import OrgRelationMixin
|
||||
from django.db.models import Q
|
||||
from common.permissions import IsOrgAdmin
|
||||
from common.utils import get_object_or_none
|
||||
@@ -8,9 +6,7 @@ from assets.models import SystemUser
|
||||
from users.models import User, UserGroup
|
||||
|
||||
|
||||
__all__ = [
|
||||
'RelationViewSet', 'BasePermissionViewSet'
|
||||
]
|
||||
__all__ = ['BasePermissionViewSet']
|
||||
|
||||
|
||||
class BasePermissionViewSet(OrgBulkModelViewSet):
|
||||
@@ -99,10 +95,3 @@ class BasePermissionViewSet(OrgBulkModelViewSet):
|
||||
queryset = self.filter_keyword(queryset)
|
||||
queryset = queryset.distinct()
|
||||
return queryset
|
||||
|
||||
|
||||
class RelationViewSet(OrgRelationMixin, OrgBulkModelViewSet):
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.annotate(**{f'{self.from_field}_display': F(f'{self.from_field}__name')})
|
||||
return queryset
|
||||
|
@@ -1,21 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from orgs.mixins.api import OrgBulkModelViewSet
|
||||
|
||||
from .. import models, serializers
|
||||
from common.permissions import IsOrgAdmin
|
||||
|
||||
|
||||
__all__ = ['DatabaseAppPermissionViewSet']
|
||||
|
||||
|
||||
class DatabaseAppPermissionViewSet(OrgBulkModelViewSet):
|
||||
model = models.DatabaseAppPermission
|
||||
serializer_classes = {
|
||||
'default': serializers.DatabaseAppPermissionSerializer,
|
||||
'display': serializers.DatabaseAppPermissionListSerializer
|
||||
}
|
||||
filter_fields = ('name',)
|
||||
search_fields = filter_fields
|
||||
permission_classes = (IsOrgAdmin,)
|
@@ -1,120 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
from rest_framework import generics
|
||||
from django.db.models import F, Value
|
||||
from django.db.models.functions import Concat
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from common.permissions import IsOrgAdmin
|
||||
from .base import RelationViewSet
|
||||
from .. import models, serializers
|
||||
|
||||
__all__ = [
|
||||
'DatabaseAppPermissionUserRelationViewSet',
|
||||
'DatabaseAppPermissionUserGroupRelationViewSet',
|
||||
'DatabaseAppPermissionAllUserListApi',
|
||||
'DatabaseAppPermissionDatabaseAppRelationViewSet',
|
||||
'DatabaseAppPermissionAllDatabaseAppListApi',
|
||||
'DatabaseAppPermissionSystemUserRelationViewSet',
|
||||
]
|
||||
|
||||
|
||||
class DatabaseAppPermissionUserRelationViewSet(RelationViewSet):
|
||||
serializer_class = serializers.DatabaseAppPermissionUserRelationSerializer
|
||||
m2m_field = models.DatabaseAppPermission.users.field
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
filter_fields = [
|
||||
'id', 'user', 'databaseapppermission'
|
||||
]
|
||||
search_fields = ('user__name', 'user__username', 'databaseapppermission__name')
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.annotate(user_display=F('user__name'))
|
||||
return queryset
|
||||
|
||||
|
||||
class DatabaseAppPermissionUserGroupRelationViewSet(RelationViewSet):
|
||||
serializer_class = serializers.DatabaseAppPermissionUserGroupRelationSerializer
|
||||
m2m_field = models.DatabaseAppPermission.user_groups.field
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
filter_fields = [
|
||||
'id', "usergroup", "databaseapppermission"
|
||||
]
|
||||
search_fields = ["usergroup__name", "databaseapppermission__name"]
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset \
|
||||
.annotate(usergroup_display=F('usergroup__name'))
|
||||
return queryset
|
||||
|
||||
|
||||
class DatabaseAppPermissionAllUserListApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
serializer_class = serializers.DatabaseAppPermissionAllUserSerializer
|
||||
filter_fields = ("username", "name")
|
||||
search_fields = filter_fields
|
||||
|
||||
def get_queryset(self):
|
||||
pk = self.kwargs.get("pk")
|
||||
perm = get_object_or_404(models.DatabaseAppPermission, pk=pk)
|
||||
users = perm.get_all_users().only(
|
||||
*self.serializer_class.Meta.only_fields
|
||||
)
|
||||
return users
|
||||
|
||||
|
||||
class DatabaseAppPermissionDatabaseAppRelationViewSet(RelationViewSet):
|
||||
serializer_class = serializers.DatabaseAppPermissionDatabaseAppRelationSerializer
|
||||
m2m_field = models.DatabaseAppPermission.database_apps.field
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
filter_fields = [
|
||||
'id', 'databaseapp', 'databaseapppermission',
|
||||
]
|
||||
search_fields = [
|
||||
"id", "databaseapp__name", "databaseapppermission__name"
|
||||
]
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset \
|
||||
.annotate(databaseapp_display=F('databaseapp__name'))
|
||||
return queryset
|
||||
|
||||
|
||||
class DatabaseAppPermissionAllDatabaseAppListApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
serializer_class = serializers.DatabaseAppPermissionAllDatabaseAppSerializer
|
||||
filter_fields = ("name",)
|
||||
search_fields = filter_fields
|
||||
|
||||
def get_queryset(self):
|
||||
pk = self.kwargs.get("pk")
|
||||
perm = get_object_or_404(models.DatabaseAppPermission, pk=pk)
|
||||
database_apps = perm.get_all_database_apps().only(
|
||||
*self.serializer_class.Meta.only_fields
|
||||
)
|
||||
return database_apps
|
||||
|
||||
|
||||
class DatabaseAppPermissionSystemUserRelationViewSet(RelationViewSet):
|
||||
serializer_class = serializers.DatabaseAppPermissionSystemUserRelationSerializer
|
||||
m2m_field = models.DatabaseAppPermission.system_users.field
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
filter_fields = [
|
||||
'id', 'systemuser', 'databaseapppermission'
|
||||
]
|
||||
search_fields = [
|
||||
'databaseapppermission__name', 'systemuser__name', 'systemuser__username'
|
||||
]
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.annotate(
|
||||
systemuser_display=Concat(
|
||||
F('systemuser__name'), Value('('), F('systemuser__username'),
|
||||
Value(')')
|
||||
)
|
||||
)
|
||||
return queryset
|
@@ -1,21 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from orgs.mixins.api import OrgBulkModelViewSet
|
||||
|
||||
from .. import models, serializers
|
||||
from common.permissions import IsOrgAdmin
|
||||
|
||||
|
||||
__all__ = ['K8sAppPermissionViewSet']
|
||||
|
||||
|
||||
class K8sAppPermissionViewSet(OrgBulkModelViewSet):
|
||||
model = models.K8sAppPermission
|
||||
serializer_classes = {
|
||||
'default': serializers.K8sAppPermissionSerializer,
|
||||
'display': serializers.K8sAppPermissionListSerializer
|
||||
}
|
||||
filter_fields = ('name',)
|
||||
search_fields = filter_fields
|
||||
permission_classes = (IsOrgAdmin,)
|
@@ -1,111 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
from rest_framework import generics
|
||||
from django.db.models import F, Value
|
||||
from django.db.models.functions import Concat
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from common.permissions import IsOrgAdmin
|
||||
from .base import RelationViewSet
|
||||
from .. import models, serializers
|
||||
|
||||
|
||||
class K8sAppPermissionUserRelationViewSet(RelationViewSet):
|
||||
serializer_class = serializers.K8sAppPermissionUserRelationSerializer
|
||||
m2m_field = models.K8sAppPermission.users.field
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
filter_fields = [
|
||||
'id', 'user', 'k8sapppermission'
|
||||
]
|
||||
search_fields = ('user__name', 'user__username', 'k8sapppermission__name')
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.annotate(user_display=F('user__name'))
|
||||
return queryset
|
||||
|
||||
|
||||
class K8sAppPermissionUserGroupRelationViewSet(RelationViewSet):
|
||||
serializer_class = serializers.K8sAppPermissionUserGroupRelationSerializer
|
||||
m2m_field = models.K8sAppPermission.user_groups.field
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
filter_fields = [
|
||||
'id', "usergroup", "k8sapppermission"
|
||||
]
|
||||
search_fields = ["usergroup__name", "k8sapppermission__name"]
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset \
|
||||
.annotate(usergroup_display=F('usergroup__name'))
|
||||
return queryset
|
||||
|
||||
|
||||
class K8sAppPermissionAllUserListApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
serializer_class = serializers.K8sAppPermissionAllUserSerializer
|
||||
filter_fields = ("username", "name")
|
||||
search_fields = filter_fields
|
||||
|
||||
def get_queryset(self):
|
||||
pk = self.kwargs.get("pk")
|
||||
perm = get_object_or_404(models.K8sAppPermission, pk=pk)
|
||||
users = perm.get_all_users().only(
|
||||
*self.serializer_class.Meta.only_fields
|
||||
)
|
||||
return users
|
||||
|
||||
|
||||
class K8sAppPermissionK8sAppRelationViewSet(RelationViewSet):
|
||||
serializer_class = serializers.K8sAppPermissionK8sAppRelationSerializer
|
||||
m2m_field = models.K8sAppPermission.k8s_apps.field
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
filter_fields = [
|
||||
'id', 'k8sapp', 'k8sapppermission',
|
||||
]
|
||||
search_fields = [
|
||||
"id", "k8sapp__name", "k8sapppermission__name"
|
||||
]
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset \
|
||||
.annotate(k8sapp_display=F('k8sapp__name'))
|
||||
return queryset
|
||||
|
||||
|
||||
class K8sAppPermissionAllK8sAppListApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
serializer_class = serializers.K8sAppPermissionAllK8sAppSerializer
|
||||
filter_fields = ("name",)
|
||||
search_fields = filter_fields
|
||||
|
||||
def get_queryset(self):
|
||||
pk = self.kwargs.get("pk")
|
||||
perm = get_object_or_404(models.K8sAppPermission, pk=pk)
|
||||
database_apps = perm.get_all_k8s_apps().only(
|
||||
*self.serializer_class.Meta.only_fields
|
||||
)
|
||||
return database_apps
|
||||
|
||||
|
||||
class K8sAppPermissionSystemUserRelationViewSet(RelationViewSet):
|
||||
serializer_class = serializers.K8sAppPermissionSystemUserRelationSerializer
|
||||
m2m_field = models.K8sAppPermission.system_users.field
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
filter_fields = [
|
||||
'id', 'systemuser', 'k8sapppermission'
|
||||
]
|
||||
search_fields = [
|
||||
'k8sapppermission__name', 'systemuser__name', 'systemuser__username'
|
||||
]
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.annotate(
|
||||
systemuser_display=Concat(
|
||||
F('systemuser__name'), Value('('), F('systemuser__username'),
|
||||
Value(')')
|
||||
)
|
||||
)
|
||||
return queryset
|
@@ -1,58 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
|
||||
from rest_framework.generics import get_object_or_404
|
||||
from common.permissions import IsValidUser, IsOrgAdminOrAppUser
|
||||
from common.utils import get_logger
|
||||
from orgs.utils import set_to_root_org, set_current_org, get_current_org
|
||||
from ..hands import User, UserGroup
|
||||
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
__all__ = [
|
||||
'UserPermissionMixin', 'UserGroupPermissionMixin',
|
||||
]
|
||||
|
||||
|
||||
class UserPermissionMixin:
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
current_org = None
|
||||
obj = None
|
||||
|
||||
def initial(self, *args, **kwargs):
|
||||
super().initial(*args, **kwargs)
|
||||
self.obj = self.get_obj()
|
||||
|
||||
def get_obj(self):
|
||||
user_id = self.kwargs.get('pk', '')
|
||||
if user_id:
|
||||
user = get_object_or_404(User, id=user_id)
|
||||
else:
|
||||
self.current_org = get_current_org()
|
||||
set_to_root_org()
|
||||
user = self.request.user
|
||||
return user
|
||||
|
||||
def get_permissions(self):
|
||||
if self.kwargs.get('pk') is None:
|
||||
self.permission_classes = (IsValidUser,)
|
||||
return super().get_permissions()
|
||||
|
||||
def finalize_response(self, request, response, *args, **kwargs):
|
||||
response = super().finalize_response(request, response, *args, **kwargs)
|
||||
org = getattr(self, 'current_org', None)
|
||||
if org:
|
||||
set_current_org(org)
|
||||
return response
|
||||
|
||||
|
||||
class UserGroupPermissionMixin:
|
||||
obj = None
|
||||
|
||||
def get_obj(self):
|
||||
user_group_id = self.kwargs.get('pk', '')
|
||||
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
||||
return user_group
|
||||
|
||||
|
@@ -1,98 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from rest_framework.views import Response
|
||||
|
||||
from common.permissions import IsOrgAdmin
|
||||
from orgs.mixins.api import OrgModelViewSet
|
||||
from orgs.mixins import generics
|
||||
from ..models import RemoteAppPermission
|
||||
from ..serializers import (
|
||||
RemoteAppPermissionSerializer,
|
||||
RemoteAppPermissionUpdateUserSerializer,
|
||||
RemoteAppPermissionUpdateRemoteAppSerializer,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
'RemoteAppPermissionViewSet',
|
||||
'RemoteAppPermissionAddUserApi', 'RemoteAppPermissionAddRemoteAppApi',
|
||||
'RemoteAppPermissionRemoveUserApi', 'RemoteAppPermissionRemoveRemoteAppApi',
|
||||
]
|
||||
|
||||
|
||||
class RemoteAppPermissionViewSet(OrgModelViewSet):
|
||||
model = RemoteAppPermission
|
||||
filter_fields = ('name', )
|
||||
search_fields = filter_fields
|
||||
serializer_class = RemoteAppPermissionSerializer
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
|
||||
|
||||
class RemoteAppPermissionAddUserApi(generics.RetrieveUpdateAPIView):
|
||||
model = RemoteAppPermission
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
serializer_class = RemoteAppPermissionUpdateUserSerializer
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
perm = self.get_object()
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
users = serializer.validated_data.get('users')
|
||||
if users:
|
||||
perm.users.add(*tuple(users))
|
||||
return Response({"msg": "ok"})
|
||||
else:
|
||||
return Response({"error": serializer.errors})
|
||||
|
||||
|
||||
class RemoteAppPermissionRemoveUserApi(generics.RetrieveUpdateAPIView):
|
||||
model = RemoteAppPermission
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
serializer_class = RemoteAppPermissionUpdateUserSerializer
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
perm = self.get_object()
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
users = serializer.validated_data.get('users')
|
||||
if users:
|
||||
perm.users.remove(*tuple(users))
|
||||
return Response({"msg": "ok"})
|
||||
else:
|
||||
return Response({"error": serializer.errors})
|
||||
|
||||
|
||||
class RemoteAppPermissionAddRemoteAppApi(generics.RetrieveUpdateAPIView):
|
||||
model = RemoteAppPermission
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
serializer_class = RemoteAppPermissionUpdateRemoteAppSerializer
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
perm = self.get_object()
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
remote_apps = serializer.validated_data.get('remote_apps')
|
||||
if remote_apps:
|
||||
perm.remote_apps.add(*tuple(remote_apps))
|
||||
return Response({"msg": "ok"})
|
||||
else:
|
||||
return Response({"error": serializer.errors})
|
||||
|
||||
|
||||
class RemoteAppPermissionRemoveRemoteAppApi(generics.RetrieveUpdateAPIView):
|
||||
model = RemoteAppPermission
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
serializer_class = RemoteAppPermissionUpdateRemoteAppSerializer
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
perm = self.get_object()
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
remote_apps = serializer.validated_data.get('remote_apps')
|
||||
if remote_apps:
|
||||
perm.remote_apps.remove(*tuple(remote_apps))
|
||||
return Response({"msg": "ok"})
|
||||
else:
|
||||
return Response({"error": serializer.errors})
|
||||
|
||||
|
@@ -1,79 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
from perms.api.base import RelationViewSet
|
||||
from rest_framework import generics
|
||||
from django.db.models import F
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from common.permissions import IsOrgAdmin
|
||||
from .. import models, serializers
|
||||
|
||||
__all__ = [
|
||||
'RemoteAppPermissionUserRelationViewSet',
|
||||
'RemoteAppPermissionRemoteAppRelationViewSet',
|
||||
'RemoteAppPermissionAllRemoteAppListApi',
|
||||
'RemoteAppPermissionAllUserListApi',
|
||||
]
|
||||
|
||||
|
||||
class RemoteAppPermissionAllUserListApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
serializer_class = serializers.PermissionAllUserSerializer
|
||||
filter_fields = ("username", "name")
|
||||
search_fields = filter_fields
|
||||
|
||||
def get_queryset(self):
|
||||
pk = self.kwargs.get("pk")
|
||||
perm = get_object_or_404(models.RemoteAppPermission, pk=pk)
|
||||
users = perm.all_users.only(
|
||||
*self.serializer_class.Meta.only_fields
|
||||
)
|
||||
return users
|
||||
|
||||
|
||||
class RemoteAppPermissionUserRelationViewSet(RelationViewSet):
|
||||
serializer_class = serializers.RemoteAppPermissionUserRelationSerializer
|
||||
m2m_field = models.RemoteAppPermission.users.field
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
filter_fields = [
|
||||
'id', 'user', 'remoteapppermission'
|
||||
]
|
||||
search_fields = ('user__name', 'user__username', 'remoteapppermission__name')
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.annotate(user_display=F('user__name'))
|
||||
return queryset
|
||||
|
||||
|
||||
class RemoteAppPermissionRemoteAppRelationViewSet(RelationViewSet):
|
||||
serializer_class = serializers.RemoteAppPermissionRemoteAppRelationSerializer
|
||||
m2m_field = models.RemoteAppPermission.remote_apps.field
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
filter_fields = [
|
||||
'id', 'remoteapp', 'remoteapppermission',
|
||||
]
|
||||
search_fields = [
|
||||
"id", "remoteapp__name", "remoteapppermission__name"
|
||||
]
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset \
|
||||
.annotate(remoteapp_display=F('remoteapp__name'))
|
||||
return queryset
|
||||
|
||||
|
||||
class RemoteAppPermissionAllRemoteAppListApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
serializer_class = serializers.RemoteAppPermissionAllRemoteAppSerializer
|
||||
filter_fields = ("name",)
|
||||
search_fields = filter_fields
|
||||
|
||||
def get_queryset(self):
|
||||
pk = self.kwargs.get("pk")
|
||||
perm = get_object_or_404(models.RemoteAppPermission, pk=pk)
|
||||
remote_apps = perm.all_remote_apps.only(
|
||||
*self.serializer_class.Meta.only_fields
|
||||
)
|
||||
return remote_apps
|
@@ -1,128 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
import uuid
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework.views import APIView, Response
|
||||
from common.permissions import IsOrgAdminOrAppUser, IsValidUser
|
||||
from common.tree import TreeNodeSerializer
|
||||
from orgs.mixins import generics
|
||||
from users.models import User, UserGroup
|
||||
from applications.serializers import DatabaseAppSerializer
|
||||
from applications.models import DatabaseApp
|
||||
from assets.models import SystemUser
|
||||
from .. import utils, serializers
|
||||
from .mixin import UserPermissionMixin
|
||||
|
||||
__all__ = [
|
||||
'UserGrantedDatabaseAppsApi',
|
||||
'UserGrantedDatabaseAppsAsTreeApi',
|
||||
'UserGroupGrantedDatabaseAppsApi',
|
||||
'ValidateUserDatabaseAppPermissionApi',
|
||||
'UserGrantedDatabaseAppSystemUsersApi',
|
||||
]
|
||||
|
||||
|
||||
class UserGrantedDatabaseAppsApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
serializer_class = DatabaseAppSerializer
|
||||
filter_fields = ['id', 'name', 'type', 'comment']
|
||||
search_fields = ['name', 'comment']
|
||||
|
||||
def get_object(self):
|
||||
user_id = self.kwargs.get('pk', '')
|
||||
if user_id:
|
||||
user = get_object_or_404(User, id=user_id)
|
||||
else:
|
||||
user = self.request.user
|
||||
return user
|
||||
|
||||
def get_queryset(self):
|
||||
util = utils.DatabaseAppPermissionUtil(self.get_object())
|
||||
queryset = util.get_database_apps()
|
||||
return queryset
|
||||
|
||||
def get_permissions(self):
|
||||
if self.kwargs.get('pk') is None:
|
||||
self.permission_classes = (IsValidUser,)
|
||||
return super().get_permissions()
|
||||
|
||||
|
||||
class UserGrantedDatabaseAppsAsTreeApi(UserGrantedDatabaseAppsApi):
|
||||
serializer_class = TreeNodeSerializer
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
|
||||
def get_serializer(self, database_apps, *args, **kwargs):
|
||||
if database_apps is None:
|
||||
database_apps = []
|
||||
only_database_app = self.request.query_params.get('only', '0') == '1'
|
||||
tree_root = None
|
||||
data = []
|
||||
if not only_database_app:
|
||||
amount = len(database_apps)
|
||||
tree_root = utils.construct_database_apps_tree_root(amount)
|
||||
data.append(tree_root)
|
||||
for database_app in database_apps:
|
||||
node = utils.parse_database_app_to_tree_node(tree_root, database_app)
|
||||
data.append(node)
|
||||
data.sort()
|
||||
return super().get_serializer(data, many=True)
|
||||
|
||||
|
||||
class UserGrantedDatabaseAppSystemUsersApi(UserPermissionMixin, generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
serializer_class = serializers.DatabaseAppSystemUserSerializer
|
||||
only_fields = serializers.DatabaseAppSystemUserSerializer.Meta.only_fields
|
||||
|
||||
def get_queryset(self):
|
||||
util = utils.DatabaseAppPermissionUtil(self.obj)
|
||||
database_app_id = self.kwargs.get('database_app_id')
|
||||
database_app = get_object_or_404(DatabaseApp, id=database_app_id)
|
||||
system_users = util.get_database_app_system_users(database_app)
|
||||
return system_users
|
||||
|
||||
|
||||
# Validate
|
||||
|
||||
class ValidateUserDatabaseAppPermissionApi(APIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
user_id = request.query_params.get('user_id', '')
|
||||
database_app_id = request.query_params.get('database_app_id', '')
|
||||
system_user_id = request.query_params.get('system_user_id', '')
|
||||
|
||||
try:
|
||||
user_id = uuid.UUID(user_id)
|
||||
database_app_id = uuid.UUID(database_app_id)
|
||||
system_user_id = uuid.UUID(system_user_id)
|
||||
except ValueError:
|
||||
return Response({'msg': False}, status=403)
|
||||
|
||||
user = get_object_or_404(User, id=user_id)
|
||||
database_app = get_object_or_404(DatabaseApp, id=database_app_id)
|
||||
system_user = get_object_or_404(SystemUser, id=system_user_id)
|
||||
|
||||
util = utils.DatabaseAppPermissionUtil(user)
|
||||
system_users = util.get_database_app_system_users(database_app)
|
||||
if system_user in system_users:
|
||||
return Response({'msg': True}, status=200)
|
||||
|
||||
return Response({'msg': False}, status=403)
|
||||
|
||||
|
||||
# UserGroup
|
||||
|
||||
class UserGroupGrantedDatabaseAppsApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
serializer_class = DatabaseAppSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = []
|
||||
user_group_id = self.kwargs.get('pk')
|
||||
if not user_group_id:
|
||||
return queryset
|
||||
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
||||
util = utils.DatabaseAppPermissionUtil(user_group)
|
||||
queryset = util.get_database_apps()
|
||||
return queryset
|
@@ -1,120 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
import uuid
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework.views import APIView, Response
|
||||
from common.permissions import IsOrgAdminOrAppUser, IsValidUser
|
||||
from common.tree import TreeNodeSerializer
|
||||
from orgs.mixins import generics
|
||||
from users.models import User, UserGroup
|
||||
from applications.serializers import K8sAppSerializer
|
||||
from applications.models import K8sApp
|
||||
from assets.models import SystemUser
|
||||
from .. import utils, serializers
|
||||
from .mixin import UserPermissionMixin
|
||||
|
||||
|
||||
class UserGrantedK8sAppsApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
serializer_class = K8sAppSerializer
|
||||
filter_fields = ['id', 'name', 'type', 'comment']
|
||||
search_fields = ['name', 'comment']
|
||||
|
||||
def get_object(self):
|
||||
user_id = self.kwargs.get('pk', '')
|
||||
if user_id:
|
||||
user = get_object_or_404(User, id=user_id)
|
||||
else:
|
||||
user = self.request.user
|
||||
return user
|
||||
|
||||
def get_queryset(self):
|
||||
util = utils.K8sAppPermissionUtil(self.get_object())
|
||||
queryset = util.get_k8s_apps()
|
||||
return queryset
|
||||
|
||||
def get_permissions(self):
|
||||
if self.kwargs.get('pk') is None:
|
||||
self.permission_classes = (IsValidUser,)
|
||||
return super().get_permissions()
|
||||
|
||||
|
||||
class UserGrantedK8sAppsAsTreeApi(UserGrantedK8sAppsApi):
|
||||
serializer_class = TreeNodeSerializer
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
|
||||
def get_serializer(self, k8s_apps, *args, **kwargs):
|
||||
if k8s_apps is None:
|
||||
k8s_apps = []
|
||||
only_k8s_app = self.request.query_params.get('only', '0') == '1'
|
||||
tree_root = None
|
||||
data = []
|
||||
if not only_k8s_app:
|
||||
amount = len(k8s_apps)
|
||||
tree_root = utils.construct_k8s_apps_tree_root(amount)
|
||||
data.append(tree_root)
|
||||
for k8s_app in k8s_apps:
|
||||
node = utils.parse_k8s_app_to_tree_node(tree_root, k8s_app)
|
||||
data.append(node)
|
||||
data.sort()
|
||||
return super().get_serializer(data, many=True)
|
||||
|
||||
|
||||
class UserGrantedK8sAppSystemUsersApi(UserPermissionMixin, generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
serializer_class = serializers.K8sAppSystemUserSerializer
|
||||
only_fields = serializers.K8sAppSystemUserSerializer.Meta.only_fields
|
||||
|
||||
def get_queryset(self):
|
||||
util = utils.K8sAppPermissionUtil(self.obj)
|
||||
k8s_app_id = self.kwargs.get('k8s_app_id')
|
||||
k8s_app = get_object_or_404(K8sApp, id=k8s_app_id)
|
||||
system_users = util.get_k8s_app_system_users(k8s_app)
|
||||
return system_users
|
||||
|
||||
|
||||
# Validate
|
||||
|
||||
class ValidateUserK8sAppPermissionApi(APIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
user_id = request.query_params.get('user_id', '')
|
||||
k8s_app_id = request.query_params.get('k8s_app_id', '')
|
||||
system_user_id = request.query_params.get('system_user_id', '')
|
||||
|
||||
try:
|
||||
user_id = uuid.UUID(user_id)
|
||||
k8s_app_id = uuid.UUID(k8s_app_id)
|
||||
system_user_id = uuid.UUID(system_user_id)
|
||||
except ValueError:
|
||||
return Response({'msg': False}, status=403)
|
||||
|
||||
user = get_object_or_404(User, id=user_id)
|
||||
k8s_app = get_object_or_404(K8sApp, id=k8s_app_id)
|
||||
system_user = get_object_or_404(SystemUser, id=system_user_id)
|
||||
|
||||
util = utils.K8sAppPermissionUtil(user)
|
||||
system_users = util.get_k8s_app_system_users(k8s_app)
|
||||
if system_user in system_users:
|
||||
return Response({'msg': True}, status=200)
|
||||
|
||||
return Response({'msg': False}, status=403)
|
||||
|
||||
|
||||
# UserGroup
|
||||
|
||||
class UserGroupGrantedK8sAppsApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
serializer_class = K8sAppSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = []
|
||||
user_group_id = self.kwargs.get('pk')
|
||||
if not user_group_id:
|
||||
return queryset
|
||||
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
||||
util = utils.K8sAppPermissionUtil(user_group)
|
||||
queryset = util.get_k8s_apps()
|
||||
return queryset
|
@@ -1,126 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import uuid
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework.views import APIView, Response
|
||||
|
||||
from common.permissions import IsValidUser, IsOrgAdminOrAppUser
|
||||
from common.tree import TreeNodeSerializer
|
||||
from orgs.mixins import generics
|
||||
from ..utils import (
|
||||
RemoteAppPermissionUtil, construct_remote_apps_tree_root,
|
||||
parse_remote_app_to_tree_node,
|
||||
)
|
||||
from ..hands import User, RemoteApp, RemoteAppSerializer, UserGroup, SystemUser
|
||||
from .mixin import UserPermissionMixin
|
||||
from .. import serializers
|
||||
|
||||
|
||||
__all__ = [
|
||||
'UserGrantedRemoteAppsApi', 'ValidateUserRemoteAppPermissionApi',
|
||||
'UserGrantedRemoteAppsAsTreeApi', 'UserGroupGrantedRemoteAppsApi',
|
||||
'UserGrantedRemoteAppSystemUsersApi',
|
||||
]
|
||||
|
||||
|
||||
class UserGrantedRemoteAppsApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
serializer_class = RemoteAppSerializer
|
||||
filter_fields = ['name', 'id', 'type', 'comment']
|
||||
search_fields = ['name', 'comment']
|
||||
|
||||
def get_object(self):
|
||||
user_id = self.kwargs.get('pk', '')
|
||||
if user_id:
|
||||
user = get_object_or_404(User, id=user_id)
|
||||
else:
|
||||
user = self.request.user
|
||||
return user
|
||||
|
||||
def get_queryset(self):
|
||||
util = RemoteAppPermissionUtil(self.get_object())
|
||||
queryset = util.get_remote_apps()
|
||||
return queryset
|
||||
|
||||
def get_permissions(self):
|
||||
if self.kwargs.get('pk') is None:
|
||||
self.permission_classes = (IsValidUser,)
|
||||
return super().get_permissions()
|
||||
|
||||
|
||||
class UserGrantedRemoteAppsAsTreeApi(UserGrantedRemoteAppsApi):
|
||||
serializer_class = TreeNodeSerializer
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
|
||||
def get_serializer(self, remote_apps=None, *args, **kwargs):
|
||||
if remote_apps is None:
|
||||
remote_apps = []
|
||||
only_remote_app = self.request.query_params.get('only', '0') == '1'
|
||||
tree_root = None
|
||||
data = []
|
||||
if not only_remote_app:
|
||||
amount = len(remote_apps)
|
||||
tree_root = construct_remote_apps_tree_root(amount)
|
||||
data.append(tree_root)
|
||||
for remote_app in remote_apps:
|
||||
node = parse_remote_app_to_tree_node(tree_root, remote_app)
|
||||
data.append(node)
|
||||
data.sort()
|
||||
return super().get_serializer(data, many=True)
|
||||
|
||||
|
||||
class UserGrantedRemoteAppSystemUsersApi(UserPermissionMixin, generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
serializer_class = serializers.RemoteAppSystemUserSerializer
|
||||
only_fields = serializers.RemoteAppSystemUserSerializer.Meta.only_fields
|
||||
|
||||
def get_queryset(self):
|
||||
util = RemoteAppPermissionUtil(self.obj)
|
||||
remote_app_id = self.kwargs.get('remote_app_id')
|
||||
remote_app = get_object_or_404(RemoteApp, id=remote_app_id)
|
||||
system_users = util.get_remote_app_system_users(remote_app)
|
||||
return system_users
|
||||
|
||||
|
||||
class ValidateUserRemoteAppPermissionApi(APIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
user_id = request.query_params.get('user_id', '')
|
||||
remote_app_id = request.query_params.get('remote_app_id', '')
|
||||
system_id = request.query_params.get('system_user_id', '')
|
||||
|
||||
try:
|
||||
user_id = uuid.UUID(user_id)
|
||||
remote_app_id = uuid.UUID(remote_app_id)
|
||||
system_id = uuid.UUID(system_id)
|
||||
except ValueError:
|
||||
return Response({'msg': False}, status=403)
|
||||
|
||||
user = get_object_or_404(User, id=user_id)
|
||||
remote_app = get_object_or_404(RemoteApp, id=remote_app_id)
|
||||
system_user = get_object_or_404(SystemUser, id=system_id)
|
||||
|
||||
util = RemoteAppPermissionUtil(user)
|
||||
system_users = util.get_remote_app_system_users(remote_app)
|
||||
if system_user in system_users:
|
||||
return Response({'msg': True}, status=200)
|
||||
|
||||
return Response({'msg': False}, status=403)
|
||||
|
||||
|
||||
# RemoteApp permission
|
||||
|
||||
class UserGroupGrantedRemoteAppsApi(generics.ListAPIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser, )
|
||||
serializer_class = RemoteAppSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = []
|
||||
user_group_id = self.kwargs.get('pk')
|
||||
if not user_group_id:
|
||||
return queryset
|
||||
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
||||
util = RemoteAppPermissionUtil(user_group)
|
||||
queryset = util.get_remote_apps()
|
||||
return queryset
|
@@ -1,6 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from .asset_permission import *
|
||||
from .remote_app_permission import *
|
||||
from .database_app_permission import *
|
@@ -1,119 +0,0 @@
|
||||
# ~*~ coding: utf-8 ~*~
|
||||
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from orgs.mixins.forms import OrgModelForm
|
||||
from assets.models import Asset, Node, SystemUser
|
||||
from ..models import AssetPermission, Action
|
||||
|
||||
__all__ = [
|
||||
'AssetPermissionForm',
|
||||
]
|
||||
|
||||
|
||||
class ActionField(forms.MultipleChoiceField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['choices'] = Action.CHOICES
|
||||
kwargs['initial'] = Action.ALL
|
||||
kwargs['label'] = _("Action")
|
||||
kwargs['widget'] = forms.CheckboxSelectMultiple()
|
||||
kwargs['help_text'] = _(
|
||||
'Tips: The RDP protocol does not support separate controls '
|
||||
'for uploading or downloading files'
|
||||
)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def to_python(self, value):
|
||||
value = super().to_python(value)
|
||||
return Action.choices_to_value(value)
|
||||
|
||||
def prepare_value(self, value):
|
||||
if value is None:
|
||||
return value
|
||||
value = Action.value_to_choices(value)
|
||||
return value
|
||||
|
||||
|
||||
class AssetPermissionForm(OrgModelForm):
|
||||
actions = ActionField()
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
if self.data:
|
||||
return
|
||||
# 前端渲染优化, 防止过多资产
|
||||
users_field = self.fields.get('users')
|
||||
assets_field = self.fields['assets']
|
||||
nodes_field = self.fields['nodes']
|
||||
if self.instance:
|
||||
assets_field.queryset = self.instance.assets.all()
|
||||
nodes_field.queryset = self.instance.nodes.all()
|
||||
users_field.queryset = self.instance.users.all()
|
||||
else:
|
||||
assets_field.queryset = Asset.objects.none()
|
||||
nodes_field.queryset = Node.objects.none()
|
||||
users_field.queryset = []
|
||||
|
||||
# 过滤系统用户
|
||||
system_users_field = self.fields.get('system_users')
|
||||
system_users_field.queryset = SystemUser.objects.exclude(
|
||||
protocol__in=SystemUser.ASSET_CATEGORY_PROTOCOLS
|
||||
)
|
||||
|
||||
def set_nodes_initial(self, nodes):
|
||||
field = self.fields['nodes']
|
||||
field.choices = [(n.id, n.full_value) for n in nodes]
|
||||
field.initial = nodes
|
||||
|
||||
def set_assets_initial(self, assets):
|
||||
field = self.fields['assets']
|
||||
field.choices = [(a.id, a.hostname) for a in assets]
|
||||
field.initial = assets
|
||||
|
||||
class Meta:
|
||||
model = AssetPermission
|
||||
exclude = (
|
||||
'id', 'date_created', 'created_by', 'org_id'
|
||||
)
|
||||
widgets = {
|
||||
'users': forms.SelectMultiple(
|
||||
attrs={'class': 'users-select2', 'data-placeholder': _("User")}
|
||||
),
|
||||
'user_groups': forms.SelectMultiple(
|
||||
attrs={'class': 'select2', 'data-placeholder': _("User group")}
|
||||
),
|
||||
'assets': forms.SelectMultiple(
|
||||
attrs={'class': 'select2', 'data-placeholder': _("Asset")}
|
||||
),
|
||||
'nodes': forms.SelectMultiple(
|
||||
attrs={'class': 'nodes-select2', 'data-placeholder': _("Node")}
|
||||
),
|
||||
'system_users': forms.SelectMultiple(
|
||||
attrs={'class': 'select2', 'data-placeholder': _('System user')}
|
||||
),
|
||||
}
|
||||
labels = {
|
||||
'nodes': _("Node"),
|
||||
}
|
||||
|
||||
def clean_user_groups(self):
|
||||
users = self.cleaned_data.get('users')
|
||||
user_groups = self.cleaned_data.get('user_groups')
|
||||
|
||||
if not users and not user_groups:
|
||||
raise forms.ValidationError(
|
||||
_("User or group at least one required"))
|
||||
return self.cleaned_data["user_groups"]
|
||||
|
||||
def clean_asset_groups(self):
|
||||
assets = self.cleaned_data.get('assets')
|
||||
asset_groups = self.cleaned_data.get('asset_groups')
|
||||
|
||||
if not assets and not asset_groups:
|
||||
raise forms.ValidationError(
|
||||
_("Asset or group at least one required"))
|
||||
|
||||
return self.cleaned_data["asset_groups"]
|
@@ -1,49 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
from django import forms
|
||||
from orgs.mixins.forms import OrgModelForm
|
||||
from assets.models import SystemUser
|
||||
|
||||
from ..models import DatabaseAppPermission
|
||||
|
||||
|
||||
__all__ = ['DatabaseAppPermissionCreateUpdateForm']
|
||||
|
||||
|
||||
class DatabaseAppPermissionCreateUpdateForm(OrgModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
users_field = self.fields.get('users')
|
||||
if self.instance:
|
||||
users_field.queryset = self.instance.users.all()
|
||||
else:
|
||||
users_field.queryset = []
|
||||
|
||||
# 过滤系统用户
|
||||
system_users_field = self.fields.get('system_users')
|
||||
system_users_field.queryset = SystemUser.objects.filter(
|
||||
protocol__in=SystemUser.APPLICATION_CATEGORY_DB_PROTOCOLS
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = DatabaseAppPermission
|
||||
exclude = (
|
||||
'id', 'date_created', 'created_by', 'org_id'
|
||||
)
|
||||
widgets = {
|
||||
'users': forms.SelectMultiple(
|
||||
attrs={'class': 'users-select2', 'data-placeholder': _('User')}
|
||||
),
|
||||
'user_groups': forms.SelectMultiple(
|
||||
attrs={'class': 'select2', 'data-placeholder': _('User group')}
|
||||
),
|
||||
'database_apps': forms.SelectMultiple(
|
||||
attrs={'class': 'select2', 'data-placeholder': _('DatabaseApp')}
|
||||
),
|
||||
'system_users': forms.SelectMultiple(
|
||||
attrs={'class': 'select2', 'data-placeholder': _('System users')}
|
||||
),
|
||||
}
|
@@ -1,51 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
from django import forms
|
||||
from orgs.mixins.forms import OrgModelForm
|
||||
from assets.models import SystemUser
|
||||
|
||||
from ..models import RemoteAppPermission
|
||||
|
||||
|
||||
__all__ = [
|
||||
'RemoteAppPermissionCreateUpdateForm',
|
||||
]
|
||||
|
||||
|
||||
class RemoteAppPermissionCreateUpdateForm(OrgModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
users_field = self.fields.get('users')
|
||||
if self.instance:
|
||||
users_field.queryset = self.instance.users.all()
|
||||
else:
|
||||
users_field.queryset = []
|
||||
|
||||
# 过滤系统用户
|
||||
system_users_field = self.fields.get('system_users')
|
||||
system_users_field.queryset = SystemUser.objects.filter(
|
||||
protocol=SystemUser.PROTOCOL_RDP
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = RemoteAppPermission
|
||||
exclude = (
|
||||
'id', 'date_created', 'created_by', 'org_id'
|
||||
)
|
||||
widgets = {
|
||||
'users': forms.SelectMultiple(
|
||||
attrs={'class': 'users-select2', 'data-placeholder': _('User')}
|
||||
),
|
||||
'user_groups': forms.SelectMultiple(
|
||||
attrs={'class': 'select2', 'data-placeholder': _('User group')}
|
||||
),
|
||||
'remote_apps': forms.SelectMultiple(
|
||||
attrs={'class': 'select2', 'data-placeholder': _('RemoteApp')}
|
||||
),
|
||||
'system_users': forms.SelectMultiple(
|
||||
attrs={'class': 'select2', 'data-placeholder': _('System user')}
|
||||
)
|
||||
}
|
@@ -4,13 +4,10 @@
|
||||
from users.models import User, UserGroup
|
||||
from assets.models import Asset, SystemUser, Node, Label, FavoriteAsset
|
||||
from assets.serializers import NodeSerializer
|
||||
from applications.serializers import RemoteAppSerializer
|
||||
from applications.models import RemoteApp
|
||||
|
||||
__all__ = [
|
||||
'User', 'UserGroup',
|
||||
'Asset', 'SystemUser', 'Node', 'Label', 'FavoriteAsset',
|
||||
'NodeSerializer', 'RemoteAppSerializer',
|
||||
'RemoteApp'
|
||||
'NodeSerializer',
|
||||
]
|
||||
|
||||
|
62
apps/perms/migrations/0017_auto_20210104_0435.py
Normal file
62
apps/perms/migrations/0017_auto_20210104_0435.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# Generated by Django 3.1 on 2021-01-03 20:35
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('perms', '0016_applicationpermission'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterUniqueTogether(
|
||||
name='k8sapppermission',
|
||||
unique_together=None,
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='k8sapppermission',
|
||||
name='k8s_apps',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='k8sapppermission',
|
||||
name='system_users',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='k8sapppermission',
|
||||
name='user_groups',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='k8sapppermission',
|
||||
name='users',
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='remoteapppermission',
|
||||
unique_together=None,
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='remoteapppermission',
|
||||
name='remote_apps',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='remoteapppermission',
|
||||
name='system_users',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='remoteapppermission',
|
||||
name='user_groups',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='remoteapppermission',
|
||||
name='users',
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='DatabaseAppPermission',
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='K8sAppPermission',
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='RemoteAppPermission',
|
||||
),
|
||||
]
|
@@ -1,22 +0,0 @@
|
||||
# ~*~ coding: utf-8 ~*~
|
||||
#
|
||||
|
||||
from orgs.utils import set_to_root_org
|
||||
|
||||
__all__ = [
|
||||
'ChangeOrgIfNeedMixin',
|
||||
]
|
||||
|
||||
|
||||
class ChangeOrgIfNeedMixin(object):
|
||||
|
||||
@staticmethod
|
||||
def change_org_if_need(request, kwargs):
|
||||
if request.user.is_authenticated and request.user.is_superuser \
|
||||
or request.user.is_app \
|
||||
or kwargs.get('pk') is None:
|
||||
set_to_root_org()
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
self.change_org_if_need(request, kwargs)
|
||||
return super().get(request, *args, **kwargs)
|
||||
|
@@ -3,6 +3,3 @@
|
||||
|
||||
from .asset_permission import *
|
||||
from .application_permission import *
|
||||
from .remote_app_permission import *
|
||||
from .database_app_permission import *
|
||||
from .k8s_app_permission import *
|
||||
|
@@ -8,7 +8,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from common.utils import lazyproperty
|
||||
from .base import BasePermission
|
||||
from users.models import User
|
||||
from applications.models import Category
|
||||
from applications.const import ApplicationCategoryChoices, ApplicationTypeChoices
|
||||
|
||||
__all__ = [
|
||||
'ApplicationPermission',
|
||||
@@ -16,16 +16,38 @@ __all__ = [
|
||||
|
||||
|
||||
class ApplicationPermission(BasePermission):
|
||||
category = models.CharField(max_length=16, choices=Category.choices, verbose_name=_('Category'))
|
||||
type = models.CharField(max_length=16, choices=Category.get_all_type_choices(), verbose_name=_('Type'))
|
||||
applications = models.ManyToManyField('applications.Application', related_name='granted_by_permissions', blank=True, verbose_name=_("Application"))
|
||||
system_users = models.ManyToManyField('assets.SystemUser', related_name='granted_by_application_permissions', verbose_name=_("System user"))
|
||||
category = models.CharField(
|
||||
max_length=16, choices=ApplicationCategoryChoices.choices, verbose_name=_('Category')
|
||||
)
|
||||
type = models.CharField(
|
||||
max_length=16, choices=ApplicationTypeChoices.choices, verbose_name=_('Type')
|
||||
)
|
||||
applications = models.ManyToManyField(
|
||||
'applications.Application', related_name='granted_by_permissions', blank=True,
|
||||
verbose_name=_("Application")
|
||||
)
|
||||
system_users = models.ManyToManyField(
|
||||
'assets.SystemUser', related_name='granted_by_application_permissions',
|
||||
verbose_name=_("System user")
|
||||
)
|
||||
|
||||
class Meta:
|
||||
unique_together = [('org_id', 'name')]
|
||||
verbose_name = _('Application permission')
|
||||
ordering = ('name',)
|
||||
|
||||
@property
|
||||
def category_remote_app(self):
|
||||
return self.category == ApplicationCategoryChoices.remote_app.value
|
||||
|
||||
@property
|
||||
def category_db(self):
|
||||
return self.category == ApplicationCategoryChoices.db.value
|
||||
|
||||
@property
|
||||
def category_cloud(self):
|
||||
return self.category == ApplicationCategoryChoices.cloud.value
|
||||
|
||||
@lazyproperty
|
||||
def users_amount(self):
|
||||
return self.users.count()
|
||||
|
@@ -1,4 +1,3 @@
|
||||
import uuid
|
||||
import logging
|
||||
from functools import reduce
|
||||
|
||||
@@ -6,8 +5,6 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.db import models
|
||||
from common.utils import lazyproperty
|
||||
from orgs.models import Organization
|
||||
from orgs.utils import get_current_org
|
||||
from assets.models import Asset, SystemUser, Node, FamilyMixin
|
||||
|
||||
from .base import BasePermission
|
||||
|
@@ -1,39 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.utils import lazyproperty
|
||||
from .base import BasePermission
|
||||
|
||||
__all__ = [
|
||||
'DatabaseAppPermission',
|
||||
]
|
||||
|
||||
|
||||
class DatabaseAppPermission(BasePermission):
|
||||
database_apps = models.ManyToManyField(
|
||||
'applications.DatabaseApp', related_name='granted_by_permissions',
|
||||
blank=True, verbose_name=_("DatabaseApp")
|
||||
)
|
||||
system_users = models.ManyToManyField(
|
||||
'assets.SystemUser', related_name='granted_by_database_app_permissions',
|
||||
verbose_name=_("System user")
|
||||
)
|
||||
|
||||
class Meta:
|
||||
unique_together = [('org_id', 'name')]
|
||||
verbose_name = _('DatabaseApp permission')
|
||||
ordering = ('name',)
|
||||
|
||||
def get_all_database_apps(self):
|
||||
return self.database_apps.all()
|
||||
|
||||
@lazyproperty
|
||||
def database_apps_amount(self):
|
||||
return self.database_apps.count()
|
||||
|
||||
@lazyproperty
|
||||
def system_users_amount(self):
|
||||
return self.system_users.count()
|
@@ -1,39 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.utils import lazyproperty
|
||||
from .base import BasePermission
|
||||
|
||||
__all__ = [
|
||||
'K8sAppPermission',
|
||||
]
|
||||
|
||||
|
||||
class K8sAppPermission(BasePermission):
|
||||
k8s_apps = models.ManyToManyField(
|
||||
'applications.K8sApp', related_name='granted_by_permissions',
|
||||
blank=True, verbose_name=_("KubernetesApp")
|
||||
)
|
||||
system_users = models.ManyToManyField(
|
||||
'assets.SystemUser', related_name='granted_by_k8s_app_permissions',
|
||||
verbose_name=_("System user")
|
||||
)
|
||||
|
||||
class Meta:
|
||||
unique_together = [('org_id', 'name')]
|
||||
verbose_name = _('KubernetesApp permission')
|
||||
ordering = ('name',)
|
||||
|
||||
def get_all_k8s_apps(self):
|
||||
return self.k8s_apps.all()
|
||||
|
||||
@lazyproperty
|
||||
def k8s_apps_amount(self):
|
||||
return self.k8s_apps.count()
|
||||
|
||||
@lazyproperty
|
||||
def system_users_amount(self):
|
||||
return self.system_users.count()
|
@@ -1,36 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.utils import lazyproperty
|
||||
from .base import BasePermission
|
||||
|
||||
__all__ = [
|
||||
'RemoteAppPermission',
|
||||
]
|
||||
|
||||
|
||||
class RemoteAppPermission(BasePermission):
|
||||
remote_apps = models.ManyToManyField('applications.RemoteApp', related_name='granted_by_permissions', blank=True, verbose_name=_("RemoteApp"))
|
||||
system_users = models.ManyToManyField('assets.SystemUser', related_name='granted_by_remote_app_permissions', verbose_name=_("System user"))
|
||||
|
||||
class Meta:
|
||||
unique_together = [('org_id', 'name')]
|
||||
verbose_name = _('RemoteApp permission')
|
||||
ordering = ('name',)
|
||||
|
||||
def get_all_remote_apps(self):
|
||||
return set(self.remote_apps.all())
|
||||
|
||||
@property
|
||||
def all_remote_apps(self):
|
||||
return self.remote_apps.all()
|
||||
|
||||
@lazyproperty
|
||||
def remote_apps_amount(self):
|
||||
return self.remote_apps.count()
|
||||
|
||||
@lazyproperty
|
||||
def system_users_amount(self):
|
||||
return self.system_users.count()
|
@@ -3,12 +3,3 @@
|
||||
from .asset import *
|
||||
from .application import *
|
||||
from .system_user_permission import *
|
||||
|
||||
# TODO: 删除
|
||||
from .remote_app_permission import *
|
||||
from .remote_app_permission_relation import *
|
||||
from .database_app_permission import *
|
||||
from .database_app_permission_relation import *
|
||||
from .base import *
|
||||
from .k8s_app_permission import *
|
||||
from .k8s_app_permission_relation import *
|
||||
|
@@ -6,10 +6,11 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from assets.models import SystemUser
|
||||
from applications.models import Application
|
||||
from applications.serializers.attrs import get_attrs_field_dynamic_mapping_rules
|
||||
from common.drf.fields import DynamicMappingField
|
||||
|
||||
__all__ = [
|
||||
'ApplicationGrantedSerializer',
|
||||
'ApplicationSystemUserSerializer'
|
||||
'ApplicationGrantedSerializer', 'ApplicationSystemUserSerializer'
|
||||
]
|
||||
|
||||
|
||||
@@ -32,6 +33,7 @@ class ApplicationGrantedSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
category_display = serializers.ReadOnlyField(source='get_category_display', label=_('Category'))
|
||||
type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type'))
|
||||
attrs = DynamicMappingField(mapping_rules=get_attrs_field_dynamic_mapping_rules())
|
||||
|
||||
class Meta:
|
||||
model = Application
|
||||
|
@@ -1,13 +0,0 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class PermissionAllUserSerializer(serializers.Serializer):
|
||||
user = serializers.UUIDField(read_only=True, source='id')
|
||||
user_display = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
only_fields = ['id', 'username', 'name']
|
||||
|
||||
@staticmethod
|
||||
def get_user_display(obj):
|
||||
return str(obj)
|
@@ -1,67 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
from django.db.models import Count
|
||||
from rest_framework import serializers
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.drf.serializers import AdaptedBulkListSerializer
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
from .. import models
|
||||
|
||||
__all__ = [
|
||||
'DatabaseAppPermissionSerializer', 'DatabaseAppPermissionListSerializer'
|
||||
]
|
||||
|
||||
|
||||
class AmountMixin:
|
||||
@classmethod
|
||||
def setup_eager_loading(cls, queryset):
|
||||
""" Perform necessary eager loading of data. """
|
||||
queryset = queryset.annotate(
|
||||
users_amount=Count('users', distinct=True), user_groups_amount=Count('user_groups', distinct=True),
|
||||
database_apps_amount=Count('database_apps', distinct=True),
|
||||
system_users_amount=Count('system_users', distinct=True)
|
||||
)
|
||||
return queryset
|
||||
|
||||
|
||||
class DatabaseAppPermissionSerializer(AmountMixin, BulkOrgResourceModelSerializer):
|
||||
class Meta:
|
||||
model = models.DatabaseAppPermission
|
||||
list_serializer_class = AdaptedBulkListSerializer
|
||||
fields = [
|
||||
'id', 'name', 'users', 'user_groups', 'database_apps', 'system_users',
|
||||
'comment', 'is_active', 'date_start', 'date_expired', 'is_valid',
|
||||
'created_by', 'date_created', 'users_amount', 'user_groups_amount',
|
||||
'database_apps_amount', 'system_users_amount',
|
||||
]
|
||||
read_only_fields = [
|
||||
'created_by', 'date_created', 'users_amount', 'user_groups_amount',
|
||||
'database_apps_amount', 'system_users_amount',
|
||||
]
|
||||
extra_kwargs = {
|
||||
'is_valid': {'label': _('Is valid')},
|
||||
'users_amount': {'label': _('Users amount')},
|
||||
'user_groups_amount': {'label': _('User groups amount')},
|
||||
'system_users_amount': {'label': _('System users amount')},
|
||||
'database_apps_amount': {'label': _('Database apps amount')},
|
||||
}
|
||||
|
||||
|
||||
class DatabaseAppPermissionListSerializer(AmountMixin, BulkOrgResourceModelSerializer):
|
||||
is_expired = serializers.BooleanField()
|
||||
|
||||
class Meta:
|
||||
model = models.DatabaseAppPermission
|
||||
fields = [
|
||||
'id', 'name', 'comment', 'is_active', 'users_amount', 'user_groups_amount',
|
||||
'date_start', 'date_expired', 'is_valid', 'database_apps_amount', 'system_users_amount',
|
||||
'created_by', 'date_created', 'is_expired'
|
||||
]
|
||||
extra_kwargs = {
|
||||
'is_valid': {'label': _('Is valid')},
|
||||
'users_amount': {'label': _('Users amount')},
|
||||
'user_groups_amount': {'label': _('User groups amount')},
|
||||
'system_users_amount': {'label': _('System users amount')},
|
||||
'database_apps_amount': {'label': _('Database apps amount')},
|
||||
}
|
@@ -1,87 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
from perms.serializers.base import PermissionAllUserSerializer
|
||||
from rest_framework import serializers
|
||||
|
||||
from common.mixins import BulkSerializerMixin
|
||||
from common.drf.serializers import AdaptedBulkListSerializer
|
||||
|
||||
from .. import models
|
||||
|
||||
__all__ = [
|
||||
'DatabaseAppPermissionUserRelationSerializer',
|
||||
'DatabaseAppPermissionUserGroupRelationSerializer',
|
||||
'DatabaseAppPermissionAllUserSerializer',
|
||||
'DatabaseAppPermissionDatabaseAppRelationSerializer',
|
||||
'DatabaseAppPermissionAllDatabaseAppSerializer',
|
||||
'DatabaseAppPermissionSystemUserRelationSerializer',
|
||||
]
|
||||
|
||||
|
||||
class RelationMixin(BulkSerializerMixin, serializers.Serializer):
|
||||
databaseapppermission_display = serializers.ReadOnlyField()
|
||||
|
||||
def get_field_names(self, declared_fields, info):
|
||||
fields = super().get_field_names(declared_fields, info)
|
||||
fields.extend(['databaseapppermission', "databaseapppermission_display"])
|
||||
return fields
|
||||
|
||||
class Meta:
|
||||
list_serializer_class = AdaptedBulkListSerializer
|
||||
|
||||
|
||||
class DatabaseAppPermissionUserRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
||||
user_display = serializers.ReadOnlyField()
|
||||
|
||||
class Meta(RelationMixin.Meta):
|
||||
model = models.DatabaseAppPermission.users.through
|
||||
fields = [
|
||||
'id', 'user', 'user_display',
|
||||
]
|
||||
|
||||
|
||||
class DatabaseAppPermissionUserGroupRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
||||
usergroup_display = serializers.ReadOnlyField()
|
||||
|
||||
class Meta(RelationMixin.Meta):
|
||||
model = models.DatabaseAppPermission.user_groups.through
|
||||
fields = [
|
||||
'id', 'usergroup', "usergroup_display",
|
||||
]
|
||||
|
||||
|
||||
class DatabaseAppPermissionAllUserSerializer(PermissionAllUserSerializer):
|
||||
class Meta(PermissionAllUserSerializer.Meta):
|
||||
pass
|
||||
|
||||
|
||||
class DatabaseAppPermissionDatabaseAppRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
||||
databaseapp_display = serializers.ReadOnlyField()
|
||||
|
||||
class Meta(RelationMixin.Meta):
|
||||
model = models.DatabaseAppPermission.database_apps.through
|
||||
fields = [
|
||||
'id', "databaseapp", "databaseapp_display",
|
||||
]
|
||||
|
||||
|
||||
class DatabaseAppPermissionAllDatabaseAppSerializer(serializers.Serializer):
|
||||
databaseapp = serializers.UUIDField(read_only=True, source='id')
|
||||
databaseapp_display = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
only_fields = ['id', 'name']
|
||||
|
||||
@staticmethod
|
||||
def get_databaseapp_display(obj):
|
||||
return str(obj)
|
||||
|
||||
|
||||
class DatabaseAppPermissionSystemUserRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
||||
systemuser_display = serializers.ReadOnlyField()
|
||||
|
||||
class Meta(RelationMixin.Meta):
|
||||
model = models.DatabaseAppPermission.system_users.through
|
||||
fields = [
|
||||
'id', 'systemuser', 'systemuser_display'
|
||||
]
|
@@ -1,65 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
from django.db.models import Count
|
||||
from rest_framework import serializers
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
from .. import models
|
||||
|
||||
__all__ = [
|
||||
'K8sAppPermissionSerializer', 'K8sAppPermissionListSerializer'
|
||||
]
|
||||
|
||||
|
||||
class AmountMixin:
|
||||
@classmethod
|
||||
def setup_eager_loading(cls, queryset):
|
||||
""" Perform necessary eager loading of data. """
|
||||
queryset = queryset.annotate(
|
||||
users_amount=Count('users', distinct=True), user_groups_amount=Count('user_groups', distinct=True),
|
||||
k8s_apps_amount=Count('k8s_apps', distinct=True),
|
||||
system_users_amount=Count('system_users', distinct=True)
|
||||
)
|
||||
return queryset
|
||||
|
||||
|
||||
class K8sAppPermissionSerializer(AmountMixin, BulkOrgResourceModelSerializer):
|
||||
class Meta:
|
||||
model = models.K8sAppPermission
|
||||
fields = [
|
||||
'id', 'name', 'users', 'user_groups', 'k8s_apps', 'system_users',
|
||||
'comment', 'is_active', 'date_start', 'date_expired', 'is_valid',
|
||||
'created_by', 'date_created', 'users_amount', 'user_groups_amount',
|
||||
'k8s_apps_amount', 'system_users_amount',
|
||||
]
|
||||
read_only_fields = [
|
||||
'created_by', 'date_created', 'users_amount', 'user_groups_amount',
|
||||
'k8s_apps_amount', 'system_users_amount', 'id'
|
||||
]
|
||||
extra_kwargs = {
|
||||
'is_valid': {'label': _('Is valid')},
|
||||
'users_amount': {'label': _('Users amount')},
|
||||
'user_groups_amount': {'label': _('User groups amount')},
|
||||
'system_users_amount': {'label': _('System users amount')},
|
||||
'database_apps_amount': {'label': _('Database apps amount')},
|
||||
}
|
||||
|
||||
|
||||
class K8sAppPermissionListSerializer(AmountMixin, BulkOrgResourceModelSerializer):
|
||||
is_expired = serializers.BooleanField()
|
||||
|
||||
class Meta:
|
||||
model = models.K8sAppPermission
|
||||
fields = [
|
||||
'id', 'name', 'comment', 'is_active', 'users_amount', 'user_groups_amount',
|
||||
'date_start', 'date_expired', 'is_valid', 'k8s_apps_amount', 'system_users_amount',
|
||||
'created_by', 'date_created', 'is_expired'
|
||||
]
|
||||
extra_kwargs = {
|
||||
'is_valid': {'label': _('Is valid')},
|
||||
'users_amount': {'label': _('Users amount')},
|
||||
'user_groups_amount': {'label': _('User groups amount')},
|
||||
'system_users_amount': {'label': _('System users amount')},
|
||||
'k8s_apps_amount': {'label': _('K8s apps amount')},
|
||||
}
|
@@ -1,73 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
from perms.serializers.base import PermissionAllUserSerializer
|
||||
from rest_framework import serializers
|
||||
|
||||
from common.drf.serializers import BulkModelSerializer
|
||||
|
||||
from .. import models
|
||||
|
||||
|
||||
class K8sAppPermissionUserRelationSerializer(BulkModelSerializer):
|
||||
user_display = serializers.ReadOnlyField()
|
||||
k8sapppermission_display = serializers.ReadOnlyField()
|
||||
|
||||
class Meta:
|
||||
model = models.K8sAppPermission.users.through
|
||||
fields = [
|
||||
'id', 'user', 'user_display', 'k8sapppermission',
|
||||
'k8sapppermission_display'
|
||||
]
|
||||
|
||||
|
||||
class K8sAppPermissionUserGroupRelationSerializer(BulkModelSerializer):
|
||||
usergroup_display = serializers.ReadOnlyField()
|
||||
k8sapppermission_display = serializers.ReadOnlyField()
|
||||
|
||||
class Meta:
|
||||
model = models.K8sAppPermission.user_groups.through
|
||||
fields = [
|
||||
'id', 'usergroup', 'usergroup_display', 'k8sapppermission',
|
||||
'k8sapppermission_display'
|
||||
]
|
||||
|
||||
|
||||
class K8sAppPermissionAllUserSerializer(PermissionAllUserSerializer):
|
||||
class Meta(PermissionAllUserSerializer.Meta):
|
||||
pass
|
||||
|
||||
|
||||
class K8sAppPermissionK8sAppRelationSerializer(BulkModelSerializer):
|
||||
k8sapp_display = serializers.ReadOnlyField()
|
||||
k8sapppermission_display = serializers.ReadOnlyField()
|
||||
|
||||
class Meta:
|
||||
model = models.K8sAppPermission.k8s_apps.through
|
||||
fields = [
|
||||
'id', "k8sapp", "k8sapp_display", 'k8sapppermission',
|
||||
'k8sapppermission_display'
|
||||
]
|
||||
|
||||
|
||||
class K8sAppPermissionAllK8sAppSerializer(serializers.Serializer):
|
||||
k8sapp = serializers.UUIDField(read_only=True, source='id')
|
||||
k8sapp_display = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
only_fields = ['id', 'name']
|
||||
|
||||
@staticmethod
|
||||
def get_k8sapp_display(obj):
|
||||
return str(obj)
|
||||
|
||||
|
||||
class K8sAppPermissionSystemUserRelationSerializer(BulkModelSerializer):
|
||||
systemuser_display = serializers.ReadOnlyField()
|
||||
k8sapppermission_display = serializers.ReadOnlyField()
|
||||
|
||||
class Meta:
|
||||
model = models.K8sAppPermission.system_users.through
|
||||
fields = [
|
||||
'id', 'systemuser', 'systemuser_display', 'k8sapppermission',
|
||||
'k8sapppermission_display'
|
||||
]
|
@@ -1,62 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
from rest_framework import serializers
|
||||
from django.db.models import Count
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.drf.serializers import AdaptedBulkListSerializer
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
from ..models import RemoteAppPermission
|
||||
|
||||
|
||||
__all__ = [
|
||||
'RemoteAppPermissionSerializer',
|
||||
'RemoteAppPermissionUpdateUserSerializer',
|
||||
'RemoteAppPermissionUpdateRemoteAppSerializer',
|
||||
]
|
||||
|
||||
|
||||
class RemoteAppPermissionSerializer(BulkOrgResourceModelSerializer):
|
||||
class Meta:
|
||||
model = RemoteAppPermission
|
||||
list_serializer_class = AdaptedBulkListSerializer
|
||||
mini_fields = ['id', 'name']
|
||||
small_fields = mini_fields + [
|
||||
'comment', 'is_active', 'date_start', 'date_expired', 'is_valid',
|
||||
'created_by', 'date_created'
|
||||
]
|
||||
m2m_fields = [
|
||||
'users', 'user_groups', 'remote_apps', 'system_users',
|
||||
'users_amount', 'user_groups_amount', 'remote_apps_amount',
|
||||
'system_users_amount'
|
||||
]
|
||||
fields = small_fields + m2m_fields
|
||||
read_only_fields = ['created_by', 'date_created']
|
||||
extra_kwargs = {
|
||||
'is_valid': {'label': _('Is valid')},
|
||||
'users_amount': {'label': _('Users amount')},
|
||||
'user_groups_amount': {'label': _('User groups amount')},
|
||||
'system_users_amount': {'label': _('System users amount')},
|
||||
'remote_apps_amount': {'label': _('Remote apps amount')},
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def setup_eager_loading(cls, queryset):
|
||||
""" Perform necessary eager loading of data. """
|
||||
queryset = queryset.annotate(
|
||||
users_amount=Count('users', distinct=True), user_groups_amount=Count('user_groups', distinct=True),
|
||||
remote_apps_amount=Count('remote_apps', distinct=True), system_users_amount=Count('system_users', distinct=True)
|
||||
)
|
||||
return queryset
|
||||
|
||||
|
||||
class RemoteAppPermissionUpdateUserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = RemoteAppPermission
|
||||
fields = ['id', 'users']
|
||||
|
||||
|
||||
class RemoteAppPermissionUpdateRemoteAppSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = RemoteAppPermission
|
||||
fields = ['id', 'remote_apps']
|
@@ -1,49 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
from rest_framework import serializers
|
||||
|
||||
from common.drf.serializers import AdaptedBulkListSerializer
|
||||
from ..models import RemoteAppPermission
|
||||
|
||||
|
||||
__all__ = [
|
||||
'RemoteAppPermissionRemoteAppRelationSerializer',
|
||||
'RemoteAppPermissionAllRemoteAppSerializer',
|
||||
'RemoteAppPermissionUserRelationSerializer',
|
||||
]
|
||||
|
||||
|
||||
class RemoteAppPermissionRemoteAppRelationSerializer(serializers.ModelSerializer):
|
||||
remoteapp_display = serializers.ReadOnlyField()
|
||||
remoteapppermission_display = serializers.ReadOnlyField()
|
||||
|
||||
class Meta:
|
||||
model = RemoteAppPermission.remote_apps.through
|
||||
list_serializer_class = AdaptedBulkListSerializer
|
||||
fields = [
|
||||
'id', 'remoteapp', 'remoteapp_display', 'remoteapppermission', 'remoteapppermission_display'
|
||||
]
|
||||
|
||||
|
||||
class RemoteAppPermissionAllRemoteAppSerializer(serializers.Serializer):
|
||||
remoteapp = serializers.UUIDField(read_only=True, source='id')
|
||||
remoteapp_display = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
only_fields = ['id', 'name']
|
||||
|
||||
@staticmethod
|
||||
def get_remoteapp_display(obj):
|
||||
return str(obj)
|
||||
|
||||
|
||||
class RemoteAppPermissionUserRelationSerializer(serializers.ModelSerializer):
|
||||
user_display = serializers.ReadOnlyField()
|
||||
remoteapppermission_display = serializers.ReadOnlyField()
|
||||
|
||||
class Meta:
|
||||
model = RemoteAppPermission.users.through
|
||||
list_serializer_class = AdaptedBulkListSerializer
|
||||
fields = [
|
||||
'id', 'user', 'user_display', 'remoteapppermission', 'remoteapppermission_display'
|
||||
]
|
@@ -7,11 +7,11 @@ from perms.tasks import create_rebuild_user_tree_task, \
|
||||
create_rebuild_user_tree_task_by_related_nodes_or_assets
|
||||
from users.models import User, UserGroup
|
||||
from assets.models import Asset, SystemUser
|
||||
from applications.models import Application, Category
|
||||
from applications.models import Application
|
||||
from common.utils import get_logger
|
||||
from common.exceptions import M2MReverseNotAllowed
|
||||
from common.const.signals import POST_ADD, POST_REMOVE, POST_CLEAR
|
||||
from .models import AssetPermission, RemoteAppPermission, ApplicationPermission
|
||||
from .models import AssetPermission, ApplicationPermission
|
||||
|
||||
|
||||
logger = get_logger(__file__)
|
||||
@@ -187,51 +187,6 @@ def on_asset_permission_user_groups_changed(instance, action, pk_set, model,
|
||||
system_user.groups.add(*tuple(groups))
|
||||
|
||||
|
||||
@receiver(m2m_changed, sender=RemoteAppPermission.system_users.through)
|
||||
def on_remote_app_permission_system_users_changed(sender, instance=None,
|
||||
action='', reverse=False, **kwargs):
|
||||
if action != POST_ADD or reverse:
|
||||
return
|
||||
system_users = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
||||
logger.debug("Remote app permission system_users change signal received")
|
||||
assets = instance.remote_apps.all().values_list('asset__id', flat=True)
|
||||
users = instance.users.all().values_list('id', flat=True)
|
||||
groups = instance.user_groups.all().values_list('id', flat=True)
|
||||
for system_user in system_users:
|
||||
system_user.assets.add(*tuple(assets))
|
||||
if system_user.username_same_with_user:
|
||||
system_user.groups.add(*tuple(groups))
|
||||
system_user.users.add(*tuple(users))
|
||||
|
||||
|
||||
@receiver(m2m_changed, sender=RemoteAppPermission.users.through)
|
||||
def on_remoteapps_permission_users_changed(sender, instance=None, action='',
|
||||
reverse=False, **kwargs):
|
||||
if action != POST_ADD and reverse:
|
||||
return
|
||||
logger.debug("Asset permission users change signal received")
|
||||
users = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
||||
system_users = instance.system_users.all()
|
||||
|
||||
for system_user in system_users:
|
||||
if system_user.username_same_with_user:
|
||||
system_user.users.add(*tuple(users))
|
||||
|
||||
|
||||
@receiver(m2m_changed, sender=RemoteAppPermission.user_groups.through)
|
||||
def on_remoteapps_permission_user_groups_changed(sender, instance=None, action='',
|
||||
reverse=False, **kwargs):
|
||||
if action != POST_ADD and reverse:
|
||||
return
|
||||
logger.debug("Asset permission user groups change signal received")
|
||||
groups = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
||||
system_users = instance.system_users.all()
|
||||
|
||||
for system_user in system_users:
|
||||
if system_user.username_same_with_user:
|
||||
system_user.groups.add(*tuple(groups))
|
||||
|
||||
|
||||
@receiver(m2m_changed, sender=Asset.nodes.through)
|
||||
def on_node_asset_change(action, instance, reverse, pk_set, **kwargs):
|
||||
if not need_rebuild_mapping_node(action):
|
||||
@@ -249,7 +204,7 @@ def on_node_asset_change(action, instance, reverse, pk_set, **kwargs):
|
||||
|
||||
@receiver(m2m_changed, sender=ApplicationPermission.system_users.through)
|
||||
def on_application_permission_system_users_changed(sender, instance: ApplicationPermission, action, reverse, pk_set, **kwargs):
|
||||
if instance.category != Category.remote_app:
|
||||
if not instance.category_remote_app:
|
||||
return
|
||||
|
||||
if reverse:
|
||||
@@ -277,7 +232,7 @@ def on_application_permission_system_users_changed(sender, instance: Application
|
||||
|
||||
@receiver(m2m_changed, sender=ApplicationPermission.users.through)
|
||||
def on_application_permission_users_changed(sender, instance, action, reverse, pk_set, **kwargs):
|
||||
if instance.category != Category.remote_app:
|
||||
if not instance.category_remote_app:
|
||||
return
|
||||
|
||||
if reverse:
|
||||
@@ -297,7 +252,7 @@ def on_application_permission_users_changed(sender, instance, action, reverse, p
|
||||
|
||||
@receiver(m2m_changed, sender=ApplicationPermission.user_groups.through)
|
||||
def on_application_permission_user_groups_changed(sender, instance, action, reverse, pk_set, **kwargs):
|
||||
if instance.category != Category.remote_app:
|
||||
if not instance.category_remote_app:
|
||||
return
|
||||
|
||||
if reverse:
|
||||
@@ -317,7 +272,7 @@ def on_application_permission_user_groups_changed(sender, instance, action, reve
|
||||
|
||||
@receiver(m2m_changed, sender=ApplicationPermission.applications.through)
|
||||
def on_application_permission_applications_changed(sender, instance, action, reverse, pk_set, **kwargs):
|
||||
if instance.category != Category.remote_app:
|
||||
if not instance.category_remote_app:
|
||||
return
|
||||
|
||||
if reverse:
|
||||
|
@@ -4,11 +4,7 @@ from django.urls import re_path
|
||||
from common import api as capi
|
||||
from .asset_permission import asset_permission_urlpatterns
|
||||
from .application_permission import application_permission_urlpatterns
|
||||
|
||||
from .remote_app_permission import remote_app_permission_urlpatterns
|
||||
from .database_app_permission import database_app_permission_urlpatterns
|
||||
from .system_user_permission import system_users_permission_urlpatterns
|
||||
from .k8s_app_permission import k8s_app_permission_urlpatterns
|
||||
|
||||
app_name = 'perms'
|
||||
|
||||
@@ -16,10 +12,8 @@ old_version_urlpatterns = [
|
||||
re_path('(?P<resource>user|user-group|asset-permission|remote-app-permission)/.*', capi.redirect_plural_name_api)
|
||||
]
|
||||
|
||||
urlpatterns = asset_permission_urlpatterns + \
|
||||
application_permission_urlpatterns + \
|
||||
remote_app_permission_urlpatterns + \
|
||||
database_app_permission_urlpatterns + \
|
||||
k8s_app_permission_urlpatterns + \
|
||||
old_version_urlpatterns + \
|
||||
system_users_permission_urlpatterns
|
||||
urlpatterns = []
|
||||
urlpatterns += asset_permission_urlpatterns
|
||||
urlpatterns += application_permission_urlpatterns
|
||||
urlpatterns += system_users_permission_urlpatterns
|
||||
urlpatterns += old_version_urlpatterns
|
||||
|
@@ -1,47 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from django.urls import path, include
|
||||
from rest_framework_bulk.routes import BulkRouter
|
||||
from .. import api
|
||||
|
||||
|
||||
router = BulkRouter()
|
||||
router.register('database-app-permissions', api.DatabaseAppPermissionViewSet, 'database-app-permission')
|
||||
router.register('database-app-permissions-users-relations', api.DatabaseAppPermissionUserRelationViewSet, 'database-app-permissions-users-relation')
|
||||
router.register('database-app-permissions-user-groups-relations', api.DatabaseAppPermissionUserGroupRelationViewSet, 'database-app-permissions-user-groups-relation')
|
||||
router.register('database-app-permissions-database-apps-relations', api.DatabaseAppPermissionDatabaseAppRelationViewSet, 'database-app-permissions-database-apps-relation')
|
||||
router.register('database-app-permissions-system-users-relations', api.DatabaseAppPermissionSystemUserRelationViewSet, 'database-app-permissions-system-users-relation')
|
||||
|
||||
user_permission_urlpatterns = [
|
||||
path('<uuid:pk>/database-apps/', api.UserGrantedDatabaseAppsApi.as_view(), name='user-database-apps'),
|
||||
path('database-apps/', api.UserGrantedDatabaseAppsApi.as_view(), name='my-database-apps'),
|
||||
|
||||
# DatabaseApps as tree
|
||||
path('<uuid:pk>/database-apps/tree/', api.UserGrantedDatabaseAppsAsTreeApi.as_view(), name='user-databases-apps-tree'),
|
||||
path('database-apps/tree/', api.UserGrantedDatabaseAppsAsTreeApi.as_view(), name='my-databases-apps-tree'),
|
||||
|
||||
path('<uuid:pk>/database-apps/<uuid:database_app_id>/system-users/', api.UserGrantedDatabaseAppSystemUsersApi.as_view(), name='user-database-app-system-users'),
|
||||
path('database-apps/<uuid:database_app_id>/system-users/', api.UserGrantedDatabaseAppSystemUsersApi.as_view(), name='user-database-app-system-users'),
|
||||
]
|
||||
|
||||
user_group_permission_urlpatterns = [
|
||||
path('<uuid:pk>/database-apps/', api.UserGroupGrantedDatabaseAppsApi.as_view(), name='user-group-database-apps'),
|
||||
]
|
||||
|
||||
permission_urlpatterns = [
|
||||
# 授权规则中授权的用户和数据库应用
|
||||
path('<uuid:pk>/users/all/', api.DatabaseAppPermissionAllUserListApi.as_view(), name='database-app-permission-all-users'),
|
||||
path('<uuid:pk>/database-apps/all/', api.DatabaseAppPermissionAllDatabaseAppListApi.as_view(), name='database-app-permission-all-database-apps'),
|
||||
|
||||
# 验证用户是否有某个数据库应用的权限
|
||||
path('user/validate/', api.ValidateUserDatabaseAppPermissionApi.as_view(), name='validate-user-database-app-permission'),
|
||||
]
|
||||
|
||||
database_app_permission_urlpatterns = [
|
||||
path('users/', include(user_permission_urlpatterns)),
|
||||
path('user-groups/', include(user_group_permission_urlpatterns)),
|
||||
path('database-app-permissions/', include(permission_urlpatterns))
|
||||
]
|
||||
|
||||
database_app_permission_urlpatterns += router.urls
|
@@ -1,45 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from django.urls import path, include
|
||||
from rest_framework_bulk.routes import BulkRouter
|
||||
from .. import api
|
||||
|
||||
|
||||
router = BulkRouter()
|
||||
router.register('k8s-app-permissions', api.K8sAppPermissionViewSet, 'k8s-app-permission')
|
||||
router.register('k8s-app-permissions-users-relations', api.K8sAppPermissionUserRelationViewSet, 'k8s-app-permissions-users-relation')
|
||||
router.register('k8s-app-permissions-user-groups-relations', api.K8sAppPermissionUserGroupRelationViewSet, 'k8s-app-permissions-user-groups-relation')
|
||||
router.register('k8s-app-permissions-k8s-apps-relations', api.K8sAppPermissionK8sAppRelationViewSet, 'k8s-app-permissions-k8s-apps-relation')
|
||||
router.register('k8s-app-permissions-system-users-relations', api.K8sAppPermissionSystemUserRelationViewSet, 'k8s-app-permissions-system-users-relation')
|
||||
|
||||
user_permission_urlpatterns = [
|
||||
path('<uuid:pk>/k8s-apps/', api.UserGrantedK8sAppsApi.as_view(), name='user-k8s-apps'),
|
||||
path('k8s-apps/', api.UserGrantedK8sAppsApi.as_view(), name='my-k8s-apps'),
|
||||
|
||||
# k8sApps as tree
|
||||
path('<uuid:pk>/k8s-apps/tree/', api.UserGrantedK8sAppsAsTreeApi.as_view(), name='user-k8ss-apps-tree'),
|
||||
path('k8s-apps/tree/', api.UserGrantedK8sAppsAsTreeApi.as_view(), name='my-k8ss-apps-tree'),
|
||||
|
||||
path('<uuid:pk>/k8s-apps/<uuid:k8s_app_id>/system-users/', api.UserGrantedK8sAppSystemUsersApi.as_view(), name='user-k8s-app-system-users'),
|
||||
path('k8s-apps/<uuid:k8s_app_id>/system-users/', api.UserGrantedK8sAppSystemUsersApi.as_view(), name='user-k8s-app-system-users'),
|
||||
]
|
||||
|
||||
user_group_permission_urlpatterns = [
|
||||
path('<uuid:pk>/k8s-apps/', api.UserGroupGrantedK8sAppsApi.as_view(), name='user-group-k8s-apps'),
|
||||
]
|
||||
|
||||
permission_urlpatterns = [
|
||||
path('<uuid:pk>/users/all/', api.K8sAppPermissionAllUserListApi.as_view(), name='k8s-app-permission-all-users'),
|
||||
path('<uuid:pk>/k8s-apps/all/', api.K8sAppPermissionAllK8sAppListApi.as_view(), name='k8s-app-permission-all-k8s-apps'),
|
||||
|
||||
path('user/validate/', api.ValidateUserK8sAppPermissionApi.as_view(), name='validate-user-k8s-app-permission'),
|
||||
]
|
||||
|
||||
k8s_app_permission_urlpatterns = [
|
||||
path('users/', include(user_permission_urlpatterns)),
|
||||
path('user-groups/', include(user_group_permission_urlpatterns)),
|
||||
path('k8s-app-permissions/', include(permission_urlpatterns))
|
||||
]
|
||||
|
||||
k8s_app_permission_urlpatterns += router.urls
|
@@ -1,43 +0,0 @@
|
||||
# coding:utf-8
|
||||
|
||||
from django.urls import path
|
||||
from rest_framework_bulk.routes import BulkRouter
|
||||
from .. import api
|
||||
|
||||
|
||||
router = BulkRouter()
|
||||
router.register('remote-app-permissions', api.RemoteAppPermissionViewSet, 'remote-app-permission')
|
||||
router.register('remote-app-permissions-users-relations', api.RemoteAppPermissionUserRelationViewSet, 'remote-app-permissions-users-relation')
|
||||
router.register('remote-app-permissions-remote-apps-relations', api.RemoteAppPermissionRemoteAppRelationViewSet, 'remote-app-permissions-remote-apps-relation')
|
||||
|
||||
|
||||
remote_app_permission_urlpatterns = [
|
||||
# 查询用户授权的RemoteApp
|
||||
path('users/<uuid:pk>/remote-apps/', api.UserGrantedRemoteAppsApi.as_view(), name='user-remote-apps'),
|
||||
path('users/remote-apps/', api.UserGrantedRemoteAppsApi.as_view(), name='my-remote-apps'),
|
||||
|
||||
# 获取用户授权的RemoteApp树
|
||||
path('users/<uuid:pk>/remote-apps/tree/', api.UserGrantedRemoteAppsAsTreeApi.as_view(), name='user-remote-apps-as-tree'),
|
||||
path('users/remote-apps/tree/', api.UserGrantedRemoteAppsAsTreeApi.as_view(), name='my-remote-apps-as-tree'),
|
||||
|
||||
# 查询用户组授权的RemoteApp
|
||||
path('user-groups/<uuid:pk>/remote-apps/', api.UserGroupGrantedRemoteAppsApi.as_view(), name='user-group-remote-apps'),
|
||||
|
||||
# RemoteApp System users
|
||||
path('users/<uuid:pk>/remote-apps/<uuid:remote_app_id>/system-users/', api.UserGrantedRemoteAppSystemUsersApi.as_view(), name='user-remote-app-system-users'),
|
||||
path('users/remote-apps/<uuid:remote_app_id>/system-users/', api.UserGrantedRemoteAppSystemUsersApi.as_view(), name='my-remote-app-system-users'),
|
||||
|
||||
# 校验用户对RemoteApp的权限
|
||||
path('remote-app-permissions/user/validate/', api.ValidateUserRemoteAppPermissionApi.as_view(), name='validate-user-remote-app-permission'),
|
||||
|
||||
# 用户和RemoteApp变更
|
||||
path('remote-app-permissions/<uuid:pk>/users/add/', api.RemoteAppPermissionAddUserApi.as_view(), name='remote-app-permission-add-user'),
|
||||
path('remote-app-permissions/<uuid:pk>/users/remove/', api.RemoteAppPermissionRemoveUserApi.as_view(), name='remote-app-permission-remove-user'),
|
||||
path('remote-app-permissions/<uuid:pk>/remote-apps/remove/', api.RemoteAppPermissionRemoveRemoteAppApi.as_view(), name='remote-app-permission-remove-remote-app'),
|
||||
path('remote-app-permissions/<uuid:pk>/remote-apps/add/', api.RemoteAppPermissionAddRemoteAppApi.as_view(), name='remote-app-permission-add-remote-app'),
|
||||
|
||||
path('remote-app-permissions/<uuid:pk>/remote-apps/all/', api.RemoteAppPermissionAllRemoteAppListApi.as_view(), name='remote-app-permission-all-remote-apps'),
|
||||
path('remote-app-permissions/<uuid:pk>/users/all/', api.RemoteAppPermissionAllUserListApi.as_view(), name='remote-app-permission-all-users'),
|
||||
]
|
||||
|
||||
remote_app_permission_urlpatterns += router.urls
|
@@ -1,5 +0,0 @@
|
||||
# coding:utf-8
|
||||
app_name = 'perms'
|
||||
|
||||
urlpatterns = [
|
||||
]
|
@@ -3,8 +3,3 @@
|
||||
|
||||
from .asset import *
|
||||
from .application import *
|
||||
|
||||
# TODO: 删除
|
||||
from .remote_app_permission import *
|
||||
from .database_app_permission import *
|
||||
from .k8s_app_permission import *
|
||||
|
@@ -1,100 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.db.models import Q
|
||||
|
||||
from orgs.utils import set_to_root_org
|
||||
from ..models import DatabaseAppPermission
|
||||
from common.tree import TreeNode
|
||||
from applications.models import DatabaseApp
|
||||
from assets.models import SystemUser
|
||||
|
||||
|
||||
__all__ = [
|
||||
'DatabaseAppPermissionUtil',
|
||||
'construct_database_apps_tree_root',
|
||||
'parse_database_app_to_tree_node'
|
||||
]
|
||||
|
||||
|
||||
def get_user_database_app_permissions(user, include_group=True):
|
||||
if include_group:
|
||||
groups = user.groups.all()
|
||||
arg = Q(users=user) | Q(user_groups__in=groups)
|
||||
else:
|
||||
arg = Q(users=user)
|
||||
return DatabaseAppPermission.objects.all().valid().filter(arg)
|
||||
|
||||
|
||||
def get_user_group_database_app_permission(user_group):
|
||||
return DatabaseAppPermission.objects.all().valid().filter(
|
||||
user_groups=user_group
|
||||
)
|
||||
|
||||
|
||||
class DatabaseAppPermissionUtil:
|
||||
get_permissions_map = {
|
||||
'User': get_user_database_app_permissions,
|
||||
'UserGroup': get_user_group_database_app_permission
|
||||
}
|
||||
|
||||
def __init__(self, obj):
|
||||
self.object = obj
|
||||
self.change_org_if_need()
|
||||
|
||||
@staticmethod
|
||||
def change_org_if_need():
|
||||
set_to_root_org()
|
||||
|
||||
@property
|
||||
def permissions(self):
|
||||
obj_class = self.object.__class__.__name__
|
||||
func = self.get_permissions_map[obj_class]
|
||||
_permissions = func(self.object)
|
||||
return _permissions
|
||||
|
||||
def get_database_apps(self):
|
||||
database_apps = DatabaseApp.objects.filter(
|
||||
granted_by_permissions__in=self.permissions
|
||||
).distinct()
|
||||
return database_apps
|
||||
|
||||
def get_database_app_system_users(self, database_app):
|
||||
queryset = self.permissions
|
||||
kwargs = {'database_apps': database_app}
|
||||
queryset = queryset.filter(**kwargs)
|
||||
system_users_ids = queryset.values_list('system_users', flat=True)
|
||||
system_users_ids = system_users_ids.distinct()
|
||||
system_users = SystemUser.objects.filter(id__in=system_users_ids)
|
||||
system_users = system_users.order_by('-priority')
|
||||
return system_users
|
||||
|
||||
|
||||
def construct_database_apps_tree_root(amount):
|
||||
tree_root = {
|
||||
'id': 'ID_DATABASE_APP_ROOT',
|
||||
'name': '{} ({})'.format(_('DatabaseApp'), amount),
|
||||
'title': 'DatabaseApp',
|
||||
'pId': '',
|
||||
'open': False,
|
||||
'isParent': True,
|
||||
'iconSkin': '',
|
||||
'meta': {'type': 'database_app'}
|
||||
}
|
||||
return TreeNode(**tree_root)
|
||||
|
||||
|
||||
def parse_database_app_to_tree_node(parent, database_app):
|
||||
pid = parent.id if parent else ''
|
||||
tree_node = {
|
||||
'id': database_app.id,
|
||||
'name': database_app.name,
|
||||
'title': database_app.name,
|
||||
'pId': pid,
|
||||
'open': False,
|
||||
'isParent': False,
|
||||
'iconSkin': 'file',
|
||||
'meta': {'type': 'database_app'}
|
||||
}
|
||||
return TreeNode(**tree_node)
|
@@ -1,93 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.db.models import Q
|
||||
|
||||
from orgs.utils import set_to_root_org
|
||||
from ..models import K8sAppPermission
|
||||
from common.tree import TreeNode
|
||||
from applications.models import K8sApp
|
||||
from assets.models import SystemUser
|
||||
|
||||
|
||||
def get_user_k8s_app_permissions(user, include_group=True):
|
||||
if include_group:
|
||||
groups = user.groups.all()
|
||||
arg = Q(users=user) | Q(user_groups__in=groups)
|
||||
else:
|
||||
arg = Q(users=user)
|
||||
return K8sAppPermission.objects.all().valid().filter(arg)
|
||||
|
||||
|
||||
def get_user_group_k8s_app_permission(user_group):
|
||||
return K8sAppPermission.objects.all().valid().filter(
|
||||
user_groups=user_group
|
||||
)
|
||||
|
||||
|
||||
class K8sAppPermissionUtil:
|
||||
get_permissions_map = {
|
||||
'User': get_user_k8s_app_permissions,
|
||||
'UserGroup': get_user_group_k8s_app_permission
|
||||
}
|
||||
|
||||
def __init__(self, obj):
|
||||
self.object = obj
|
||||
self.change_org_if_need()
|
||||
|
||||
@staticmethod
|
||||
def change_org_if_need():
|
||||
set_to_root_org()
|
||||
|
||||
@property
|
||||
def permissions(self):
|
||||
obj_class = self.object.__class__.__name__
|
||||
func = self.get_permissions_map[obj_class]
|
||||
_permissions = func(self.object)
|
||||
return _permissions
|
||||
|
||||
def get_k8s_apps(self):
|
||||
k8s_apps = K8sApp.objects.filter(
|
||||
granted_by_permissions__in=self.permissions
|
||||
).distinct()
|
||||
return k8s_apps
|
||||
|
||||
def get_k8s_app_system_users(self, k8s_app):
|
||||
queryset = self.permissions
|
||||
kwargs = {'k8s_apps': k8s_app}
|
||||
queryset = queryset.filter(**kwargs)
|
||||
system_users_ids = queryset.values_list('system_users', flat=True)
|
||||
system_users_ids = system_users_ids.distinct()
|
||||
system_users = SystemUser.objects.filter(id__in=system_users_ids)
|
||||
system_users = system_users.order_by('-priority')
|
||||
return system_users
|
||||
|
||||
|
||||
def construct_k8s_apps_tree_root(amount):
|
||||
tree_root = {
|
||||
'id': 'ID_K8S_APP_ROOT',
|
||||
'name': '{} ({})'.format(_('KubernetesApp'), amount),
|
||||
'title': 'K8sApp',
|
||||
'pId': '',
|
||||
'open': False,
|
||||
'isParent': True,
|
||||
'iconSkin': '',
|
||||
'meta': {'type': 'k8s_app'}
|
||||
}
|
||||
return TreeNode(**tree_root)
|
||||
|
||||
|
||||
def parse_k8s_app_to_tree_node(parent, k8s_app):
|
||||
pid = parent.id if parent else ''
|
||||
tree_node = {
|
||||
'id': k8s_app.id,
|
||||
'name': k8s_app.name,
|
||||
'title': k8s_app.name,
|
||||
'pId': pid,
|
||||
'open': False,
|
||||
'isParent': False,
|
||||
'iconSkin': 'file',
|
||||
'meta': {'type': 'k8s_app'}
|
||||
}
|
||||
return TreeNode(**tree_node)
|
@@ -1,99 +0,0 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.db.models import Q
|
||||
|
||||
from common.tree import TreeNode
|
||||
from orgs.utils import set_to_root_org
|
||||
|
||||
from ..models import RemoteAppPermission
|
||||
from ..hands import RemoteApp, SystemUser
|
||||
|
||||
__all__ = [
|
||||
'RemoteAppPermissionUtil',
|
||||
'construct_remote_apps_tree_root',
|
||||
'parse_remote_app_to_tree_node',
|
||||
]
|
||||
|
||||
|
||||
def get_user_remote_app_permissions(user, include_group=True):
|
||||
if include_group:
|
||||
groups = user.groups.all()
|
||||
arg = Q(users=user) | Q(user_groups__in=groups)
|
||||
else:
|
||||
arg = Q(users=user)
|
||||
return RemoteAppPermission.objects.all().valid().filter(arg)
|
||||
|
||||
|
||||
def get_user_group_remote_app_permissions(user_group):
|
||||
return RemoteAppPermission.objects.all().valid().filter(
|
||||
user_groups=user_group
|
||||
)
|
||||
|
||||
|
||||
class RemoteAppPermissionUtil:
|
||||
get_permissions_map = {
|
||||
"User": get_user_remote_app_permissions,
|
||||
"UserGroup": get_user_group_remote_app_permissions,
|
||||
}
|
||||
|
||||
def __init__(self, obj):
|
||||
self.object = obj
|
||||
self.change_org_if_need()
|
||||
|
||||
@staticmethod
|
||||
def change_org_if_need():
|
||||
set_to_root_org()
|
||||
|
||||
@property
|
||||
def permissions(self):
|
||||
obj_class = self.object.__class__.__name__
|
||||
func = self.get_permissions_map[obj_class]
|
||||
_permissions = func(self.object)
|
||||
return _permissions
|
||||
|
||||
def get_remote_apps(self):
|
||||
remote_apps = RemoteApp.objects.filter(
|
||||
granted_by_permissions__in=self.permissions
|
||||
).distinct()
|
||||
return remote_apps
|
||||
|
||||
def get_remote_app_system_users(self, remote_app):
|
||||
queryset = self.permissions
|
||||
kwargs = {"remote_apps": remote_app}
|
||||
queryset = queryset.filter(**kwargs)
|
||||
system_users_ids = queryset.values_list('system_users', flat=True)
|
||||
system_users_ids = system_users_ids.distinct()
|
||||
system_users = SystemUser.objects.filter(id__in=system_users_ids)
|
||||
system_users = system_users.order_by('-priority')
|
||||
return system_users
|
||||
|
||||
|
||||
def construct_remote_apps_tree_root(amount):
|
||||
tree_root = {
|
||||
'id': 'ID_REMOTE_APP_ROOT',
|
||||
'name': '{} ({})'.format(_('RemoteApp'), amount),
|
||||
'title': 'RemoteApp',
|
||||
'pId': '',
|
||||
'open': False,
|
||||
'isParent': True,
|
||||
'iconSkin': '',
|
||||
'meta': {'type': 'remote_app'}
|
||||
}
|
||||
return TreeNode(**tree_root)
|
||||
|
||||
|
||||
def parse_remote_app_to_tree_node(parent, remote_app):
|
||||
pid = parent.id if parent else ''
|
||||
tree_node = {
|
||||
'id': remote_app.id,
|
||||
'name': remote_app.name,
|
||||
'title': remote_app.name,
|
||||
'pId': pid,
|
||||
'open': False,
|
||||
'isParent': False,
|
||||
'iconSkin': 'file',
|
||||
'meta': {'type': 'remote_app'}
|
||||
}
|
||||
return TreeNode(**tree_node)
|
Reference in New Issue
Block a user