Commit d1f37d4b authored by ibuler's avatar ibuler

[Update] 修改 remote app api

parent 6cb9ae69
...@@ -12,7 +12,7 @@ from ..utils import ( ...@@ -12,7 +12,7 @@ from ..utils import (
RemoteAppPermissionUtil, construct_remote_apps_tree_root, RemoteAppPermissionUtil, construct_remote_apps_tree_root,
parse_remote_app_to_tree_node, parse_remote_app_to_tree_node,
) )
from ..hands import User, RemoteApp, RemoteAppSerializer, UserGroup from ..hands import User, RemoteAppSerializer, UserGroup
from ..mixins import RemoteAppFilterMixin from ..mixins import RemoteAppFilterMixin
...@@ -25,6 +25,7 @@ __all__ = [ ...@@ -25,6 +25,7 @@ __all__ = [
class UserGrantedRemoteAppsApi(RemoteAppFilterMixin, ListAPIView): class UserGrantedRemoteAppsApi(RemoteAppFilterMixin, ListAPIView):
permission_classes = (IsOrgAdminOrAppUser,) permission_classes = (IsOrgAdminOrAppUser,)
serializer_class = RemoteAppSerializer serializer_class = RemoteAppSerializer
filter_fields = ['id']
def get_object(self): def get_object(self):
user_id = self.kwargs.get('pk', '') user_id = self.kwargs.get('pk', '')
...@@ -37,7 +38,6 @@ class UserGrantedRemoteAppsApi(RemoteAppFilterMixin, ListAPIView): ...@@ -37,7 +38,6 @@ class UserGrantedRemoteAppsApi(RemoteAppFilterMixin, ListAPIView):
def get_queryset(self): def get_queryset(self):
util = RemoteAppPermissionUtil(self.get_object()) util = RemoteAppPermissionUtil(self.get_object())
queryset = util.get_remote_apps() queryset = util.get_remote_apps()
queryset = list(queryset)
return queryset return queryset
def get_permissions(self): def get_permissions(self):
...@@ -46,36 +46,23 @@ class UserGrantedRemoteAppsApi(RemoteAppFilterMixin, ListAPIView): ...@@ -46,36 +46,23 @@ class UserGrantedRemoteAppsApi(RemoteAppFilterMixin, ListAPIView):
return super().get_permissions() return super().get_permissions()
class UserGrantedRemoteAppsAsTreeApi(ListAPIView): class UserGrantedRemoteAppsAsTreeApi(UserGrantedRemoteAppsApi):
serializer_class = TreeNodeSerializer serializer_class = TreeNodeSerializer
permission_classes = (IsOrgAdminOrAppUser,) permission_classes = (IsOrgAdminOrAppUser,)
def get_object(self): def get_serializer(self, *args, **kwargs):
user_id = self.kwargs.get('pk', '') only_remote_app = self.request.query_params.get('only', '0') == '1'
if not user_id: tree_root = None
user = self.request.user data = []
else: if not only_remote_app:
user = get_object_or_404(User, id=user_id) tree_root = construct_remote_apps_tree_root()
return user data.append(tree_root)
queryset = super().get_queryset()
def get_queryset(self): for remote_app in queryset:
queryset = []
tree_root = construct_remote_apps_tree_root()
queryset.append(tree_root)
util = RemoteAppPermissionUtil(self.get_object())
remote_apps = util.get_remote_apps()
for remote_app in remote_apps:
node = parse_remote_app_to_tree_node(tree_root, remote_app) node = parse_remote_app_to_tree_node(tree_root, remote_app)
queryset.append(node) data.append(node)
data.sort()
queryset = sorted(queryset) return super().get_serializer(data, many=True)
return queryset
def get_permissions(self):
if self.kwargs.get('pk') is None:
self.permission_classes = (IsValidUser,)
return super().get_permissions()
class ValidateUserRemoteAppPermissionApi(APIView): class ValidateUserRemoteAppPermissionApi(APIView):
...@@ -84,14 +71,13 @@ class ValidateUserRemoteAppPermissionApi(APIView): ...@@ -84,14 +71,13 @@ class ValidateUserRemoteAppPermissionApi(APIView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
user_id = request.query_params.get('user_id', '') user_id = request.query_params.get('user_id', '')
remote_app_id = request.query_params.get('remote_app_id', '') remote_app_id = request.query_params.get('remote_app_id', '')
user = get_object_or_404(User, id=user_id)
remote_app = get_object_or_404(RemoteApp, id=remote_app_id)
user = get_object_or_404(User, id=user_id)
util = RemoteAppPermissionUtil(user) util = RemoteAppPermissionUtil(user)
remote_apps = util.get_remote_apps() remote_app = util.get_remote_apps().filter(id=remote_app_id).exists()
if remote_app not in remote_apps: if remote_app:
return Response({'msg': False}, status=403) return Response({'msg': True}, status=200)
return Response({'msg': True}, status=200) return Response({'msg': False}, status=403)
# RemoteApp permission # RemoteApp permission
......
...@@ -7,6 +7,7 @@ from common.tree import TreeNode ...@@ -7,6 +7,7 @@ from common.tree import TreeNode
from orgs.utils import set_to_root_org from orgs.utils import set_to_root_org
from ..models import RemoteAppPermission from ..models import RemoteAppPermission
from ..hands import RemoteApp
__all__ = [ __all__ = [
...@@ -53,9 +54,9 @@ class RemoteAppPermissionUtil: ...@@ -53,9 +54,9 @@ class RemoteAppPermissionUtil:
return _permissions return _permissions
def get_remote_apps(self): def get_remote_apps(self):
remote_apps = set() remote_apps = RemoteApp.objects.filter(
for perm in self.permissions: granted_by_permissions__in=self.permissions
remote_apps.update(list(perm.remote_apps.all())) )
return remote_apps return remote_apps
...@@ -82,11 +83,12 @@ def parse_remote_app_to_tree_node(parent, remote_app): ...@@ -82,11 +83,12 @@ def parse_remote_app_to_tree_node(parent, remote_app):
'protocol': system_user.protocol, 'protocol': system_user.protocol,
'login_mode': system_user.login_mode, 'login_mode': system_user.login_mode,
} }
pid = parent.id if parent else ''
tree_node = { tree_node = {
'id': remote_app.id, 'id': remote_app.id,
'name': remote_app.name, 'name': remote_app.name,
'title': remote_app.name, 'title': remote_app.name,
'pId': parent.id, 'pId': pid,
'open': False, 'open': False,
'isParent': False, 'isParent': False,
'iconSkin': 'file', 'iconSkin': 'file',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment