mirror of https://github.com/jumpserver/jumpserver
parent
4847b7a680
commit
1d550cbe64
@ -1,4 +1,5 @@
|
||||
from .application import *
|
||||
from .mixin import *
|
||||
from .remote_app import *
|
||||
from .database_app import *
|
||||
from .k8s_app import *
|
||||
|
@ -0,0 +1,49 @@
|
||||
|
||||
class SerializeApplicationToTreeNodeMixin:
|
||||
|
||||
@staticmethod
|
||||
def _serialize_db(db):
|
||||
return {
|
||||
'id': db.id,
|
||||
'name': db.name,
|
||||
'title': db.name,
|
||||
'pId': '',
|
||||
'open': False,
|
||||
'iconSkin': 'database',
|
||||
'meta': {'type': 'database_app'}
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _serialize_remote_app(remote_app):
|
||||
return {
|
||||
'id': remote_app.id,
|
||||
'name': remote_app.name,
|
||||
'title': remote_app.name,
|
||||
'pId': '',
|
||||
'open': False,
|
||||
'isParent': False,
|
||||
'iconSkin': 'chrome',
|
||||
'meta': {'type': 'remote_app'}
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _serialize_cloud(cloud):
|
||||
return {
|
||||
'id': cloud.id,
|
||||
'name': cloud.name,
|
||||
'title': cloud.name,
|
||||
'pId': '',
|
||||
'open': False,
|
||||
'isParent': False,
|
||||
'iconSkin': 'k8s',
|
||||
'meta': {'type': 'k8s_app'}
|
||||
}
|
||||
|
||||
def dispatch_serialize(self, application):
|
||||
method_name = f'_serialize_{application.category}'
|
||||
data = getattr(self, method_name)(application)
|
||||
return data
|
||||
|
||||
def serialize_applications(self, applications):
|
||||
data = [self.dispatch_serialize(application) for application in applications]
|
||||
return data
|
@ -0,0 +1,31 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
|
||||
from django.db.models import Q
|
||||
from rest_framework.generics import ListAPIView
|
||||
|
||||
from common.permissions import IsOrgAdminOrAppUser
|
||||
from applications.models import Application
|
||||
from perms import serializers
|
||||
|
||||
__all__ = [
|
||||
'UserGroupGrantedApplicationsApi'
|
||||
]
|
||||
|
||||
|
||||
class UserGroupGrantedApplicationsApi(ListAPIView):
|
||||
"""
|
||||
获取用户组直接授权的资产
|
||||
"""
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
serializer_class = serializers.ApplicationGrantedSerializer
|
||||
only_fields = serializers.ApplicationGrantedSerializer.Meta.only_fields
|
||||
filter_fields = ['id', 'name', 'comment']
|
||||
search_fields = ['name', 'comment']
|
||||
|
||||
def get_queryset(self):
|
||||
user_group_id = self.kwargs.get('pk', '')
|
||||
queryset = Application.objects\
|
||||
.filter(Q(granted_by_permissions__user_groups__id=user_group_id))\
|
||||
.distinct().only(*self.only_fields)
|
||||
return queryset
|
@ -0,0 +1,2 @@
|
||||
from .user_permission_applications import *
|
||||
from .common import *
|
@ -0,0 +1,75 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
import uuid
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework.views import APIView, Response
|
||||
from rest_framework.generics import (
|
||||
ListAPIView, get_object_or_404
|
||||
)
|
||||
|
||||
from applications.models import Application
|
||||
from perms.utils.application_permission import (
|
||||
get_application_system_users_id
|
||||
)
|
||||
from perms.api.user_permission.mixin import ForAdminMixin, ForUserMixin
|
||||
from common.permissions import IsOrgAdminOrAppUser
|
||||
from ...hands import User, SystemUser
|
||||
from ... import serializers
|
||||
|
||||
|
||||
__all__ = [
|
||||
'UserGrantedApplicationSystemUsersApi',
|
||||
'MyGrantedApplicationSystemUsersApi',
|
||||
'ValidateUserApplicationPermissionApi'
|
||||
]
|
||||
|
||||
|
||||
class GrantedApplicationSystemUsersMixin(ListAPIView):
|
||||
serializer_class = serializers.ApplicationSystemUserSerializer
|
||||
only_fields = serializers.ApplicationSystemUserSerializer.Meta.only_fields
|
||||
user: None
|
||||
|
||||
def get_application_system_users_id(self, application):
|
||||
return get_application_system_users_id(self.user, application)
|
||||
|
||||
def get_queryset(self):
|
||||
application_id = self.kwargs.get('application_id')
|
||||
application = get_object_or_404(Application, id=application_id)
|
||||
system_users_id = self.get_application_system_users_id(application)
|
||||
system_users = SystemUser.objects.filter(id__in=system_users_id)\
|
||||
.only(*self.only_fields).order_by('priority')
|
||||
return system_users
|
||||
|
||||
|
||||
class UserGrantedApplicationSystemUsersApi(ForAdminMixin, GrantedApplicationSystemUsersMixin):
|
||||
pass
|
||||
|
||||
|
||||
class MyGrantedApplicationSystemUsersApi(ForUserMixin, GrantedApplicationSystemUsersMixin):
|
||||
pass
|
||||
|
||||
|
||||
class ValidateUserApplicationPermissionApi(APIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
user_id = request.query_params.get('user_id', '')
|
||||
application_id = request.query_params.get('application_id', '')
|
||||
system_user_id = request.query_params.get('system_user_id', '')
|
||||
|
||||
try:
|
||||
user_id = uuid.UUID(user_id)
|
||||
application_id = uuid.UUID(application_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)
|
||||
application = get_object_or_404(Application, id=application_id)
|
||||
system_user = get_object_or_404(SystemUser, id=system_user_id)
|
||||
|
||||
system_users_id = get_application_system_users_id(user, application)
|
||||
if system_user.id in system_users_id:
|
||||
return Response({'msg': True}, status=200)
|
||||
|
||||
return Response({'msg': False}, status=403)
|
@ -0,0 +1,65 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from rest_framework.generics import ListAPIView
|
||||
from rest_framework.response import Response
|
||||
|
||||
from applications.api.mixin import SerializeApplicationToTreeNodeMixin
|
||||
from perms import serializers
|
||||
from perms.api.user_permission.mixin import ForAdminMixin, ForUserMixin
|
||||
from perms.utils.user_application_permission import (
|
||||
get_user_granted_all_applications
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
'UserAllGrantedApplicationsApi',
|
||||
'MyAllGrantedApplicationsApi',
|
||||
'UserAllGrantedApplicationsAsTreeApi',
|
||||
'MyAllGrantedApplicationsAsTreeApi',
|
||||
]
|
||||
|
||||
|
||||
class AllGrantedApplicationsMixin(ListAPIView):
|
||||
only_fields = serializers.ApplicationGrantedSerializer.Meta.only_fields
|
||||
serializer_class = serializers.ApplicationGrantedSerializer
|
||||
filter_fields = ['id', 'name', 'comment']
|
||||
search_fields = ['name', 'comment']
|
||||
user: None
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = get_user_granted_all_applications(self.user)
|
||||
return queryset.only(*self.only_fields)
|
||||
|
||||
|
||||
class UserAllGrantedApplicationsApi(ForAdminMixin, AllGrantedApplicationsMixin):
|
||||
only_fields = serializers.ApplicationGrantedSerializer.Meta.only_fields
|
||||
serializer_class = serializers.ApplicationGrantedSerializer
|
||||
filter_fields = ['id', 'name', 'comment']
|
||||
search_fields = ['name', 'comment']
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = get_user_granted_all_applications(self.user)
|
||||
return queryset.only(*self.only_fields)
|
||||
|
||||
|
||||
class MyAllGrantedApplicationsApi(ForUserMixin, AllGrantedApplicationsMixin):
|
||||
pass
|
||||
|
||||
|
||||
class ApplicationsAsTreeMixin(SerializeApplicationToTreeNodeMixin):
|
||||
"""
|
||||
将应用序列化成树的结构返回
|
||||
"""
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
queryset = self.filter_queryset(self.get_queryset())
|
||||
data = self.serialize_applications(queryset)
|
||||
return Response(data=data)
|
||||
|
||||
|
||||
class UserAllGrantedApplicationsAsTreeApi(ApplicationsAsTreeMixin, UserAllGrantedApplicationsApi):
|
||||
pass
|
||||
|
||||
|
||||
class MyAllGrantedApplicationsAsTreeApi(ApplicationsAsTreeMixin, MyAllGrantedApplicationsApi):
|
||||
pass
|
@ -0,0 +1,14 @@
|
||||
from django.db.models import Q
|
||||
|
||||
from common.utils import get_logger
|
||||
from ..models import ApplicationPermission
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
|
||||
def get_application_system_users_id(user, application):
|
||||
queryset = ApplicationPermission.objects\
|
||||
.filter(Q(users=user) | Q(user_groups__users=user), Q(applications=application))\
|
||||
.valid()\
|
||||
.values_list('system_users', flat=True)
|
||||
return queryset
|
@ -0,0 +1,21 @@
|
||||
from perms.models import ApplicationPermission
|
||||
from applications.models import Application
|
||||
|
||||
|
||||
def get_user_all_applicationpermission_ids(user):
|
||||
application_perm_ids = set()
|
||||
application_perm_ids.update(
|
||||
ApplicationPermission.objects.valid().filter(users=user).distinct().values_list('id', flat=True)
|
||||
)
|
||||
application_perm_ids.update(
|
||||
ApplicationPermission.objects.valid().filter(user_groups__users=user).distinct().values_list('id', flat=True)
|
||||
)
|
||||
return application_perm_ids
|
||||
|
||||
|
||||
def get_user_granted_all_applications(user):
|
||||
application_perm_ids = get_user_all_applicationpermission_ids(user)
|
||||
applications = Application.objects.filter(
|
||||
granted_by_permissions__id__in=application_perm_ids
|
||||
).distinct()
|
||||
return applications
|
Loading…
Reference in new issue