jumpserver/apps/perms/api.py

380 lines
13 KiB
Python
Raw Normal View History

2016-08-09 09:27:37 +00:00
# ~*~ coding: utf-8 ~*~
#
2017-01-17 08:34:47 +00:00
from django.shortcuts import get_object_or_404
2016-10-19 11:30:55 +00:00
from rest_framework.views import APIView, Response
2016-11-15 11:33:04 +00:00
from rest_framework.generics import ListAPIView, get_object_or_404
2016-11-09 16:18:57 +00:00
from rest_framework import viewsets
2017-10-22 09:53:46 +00:00
from users.permissions import IsValidUser, IsSuperUser, IsAppUser, IsSuperUserOrAppUser
2016-11-10 08:59:50 +00:00
from common.utils import get_object_or_none
2017-01-17 08:34:47 +00:00
from .utils import get_user_granted_assets, get_user_granted_asset_groups, \
get_user_asset_permissions, get_user_group_asset_permissions, \
get_user_group_granted_assets, get_user_group_granted_asset_groups
2016-11-04 10:33:16 +00:00
from .models import AssetPermission
2017-01-17 08:34:47 +00:00
from .hands import AssetGrantedSerializer, User, UserGroup, AssetGroup, Asset, \
AssetGroup, AssetGroupGrantedSerializer, SystemUser, MyAssetGroupGrantedSerializer
2016-11-04 10:33:16 +00:00
from . import serializers
from .utils import associate_system_users_and_assets
2016-11-04 10:33:16 +00:00
2016-11-09 16:18:57 +00:00
class AssetPermissionViewSet(viewsets.ModelViewSet):
"""
资产授权列表的增删改查api
"""
2016-11-04 10:33:16 +00:00
queryset = AssetPermission.objects.all()
serializer_class = serializers.AssetPermissionSerializer
permission_classes = (IsSuperUser,)
2016-10-19 11:30:55 +00:00
2016-11-09 16:18:57 +00:00
def get_queryset(self):
queryset = super(AssetPermissionViewSet, self).get_queryset()
user_id = self.request.query_params.get('user', '')
2016-11-16 09:38:03 +00:00
user_group_id = self.request.query_params.get('user_group', '')
2016-11-15 11:33:04 +00:00
2016-11-10 08:59:50 +00:00
if user_id and user_id.isdigit():
2016-11-15 11:33:04 +00:00
user = get_object_or_404(User, id=int(user_id))
queryset = get_user_asset_permissions(user)
if user_group_id:
user_group = get_object_or_404(UserGroup, id=user_group_id)
queryset = get_user_group_asset_permissions(user_group)
2016-11-09 16:18:57 +00:00
return queryset
# Todo: 忘记为何要重写get_serializer_class了
2016-11-10 08:59:50 +00:00
def get_serializer_class(self):
if getattr(self, 'user_id', ''):
return serializers.UserAssetPermissionSerializer
return serializers.AssetPermissionSerializer
def associate_system_users_and_assets(self, serializer):
assets = serializer.validated_data.get('assets', [])
asset_groups = serializer.validated_data.get('asset_groups', [])
system_users = serializer.validated_data.get('system_users', [])
if serializer.partial:
instance = self.get_object()
assets.extend(list(instance.assets.all()))
asset_groups.extend(list(instance.asset_groups.all()))
system_users.extend(list(instance.system_users.all()))
associate_system_users_and_assets(system_users, assets, asset_groups)
def perform_create(self, serializer):
self.associate_system_users_and_assets(serializer)
return super(AssetPermissionViewSet, self).perform_create(serializer)
def perform_update(self, serializer):
self.associate_system_users_and_assets(serializer)
return super(AssetPermissionViewSet, self).perform_update(serializer)
2016-11-10 08:59:50 +00:00
class RevokeUserAssetPermission(APIView):
"""
将用户从授权中移除Detail页面会调用
"""
2016-11-10 08:59:50 +00:00
permission_classes = (IsSuperUser,)
def put(self, request, *args, **kwargs):
permission_id = str(request.data.get('id', ''))
user_id = str(request.data.get('user_id', ''))
if permission_id and user_id and permission_id.isdigit() and user_id.isdigit():
2016-11-15 11:33:04 +00:00
asset_permission = get_object_or_404(AssetPermission, id=int(permission_id))
user = get_object_or_404(User, id=int(user_id))
2016-11-10 08:59:50 +00:00
if asset_permission and user:
asset_permission.users.remove(user)
return Response({'msg': 'success'})
return Response({'msg': 'failed'}, status=404)
2016-10-19 11:30:55 +00:00
class RemoveSystemUserAssetPermission(APIView):
"""
将系统用户从授权中移除, Detail页面会调用
"""
permission_classes = (IsSuperUser,)
def put(self, request, *args, **kwargs):
response = []
asset_permission_id = kwargs.pop('pk')
system_users_id = request.data.get('system_users')
print(system_users_id)
asset_permission = get_object_or_404(
AssetPermission, id=asset_permission_id)
if not isinstance(system_users_id, list):
system_users_id = [system_users_id]
for system_user_id in system_users_id:
system_user = get_object_or_none(SystemUser, id=system_user_id)
if system_user:
asset_permission.system_users.remove(system_user)
response.append(system_user.to_json())
return Response(response, status=200)
2016-11-15 11:33:04 +00:00
class RevokeUserGroupAssetPermission(APIView):
"""
将用户组从授权中删除
"""
2016-11-15 11:33:04 +00:00
permission_classes = (IsSuperUser,)
def put(self, request, *args, **kwargs):
permission_id = str(request.data.get('id', ''))
user_group_id = str(request.data.get('user_group_id', ''))
if permission_id and user_group_id and permission_id.isdigit() and user_group_id.isdigit():
asset_permission = get_object_or_404(AssetPermission, id=int(permission_id))
user_group = get_object_or_404(UserGroup, id=int(user_group_id))
if asset_permission and user_group:
asset_permission.user_groups.remove(user_group)
return Response({'msg': 'success'})
return Response({'msg': 'failed'}, status=404)
class UserGrantedAssetsApi(ListAPIView):
"""
用户授权的所有资产
"""
2017-10-22 09:53:46 +00:00
permission_classes = (IsSuperUserOrAppUser,)
2016-11-15 11:33:04 +00:00
serializer_class = AssetGrantedSerializer
def get_queryset(self):
user_id = self.kwargs.get('pk', '')
2017-01-17 08:34:47 +00:00
queryset = []
2016-11-15 11:33:04 +00:00
if user_id:
user = get_object_or_404(User, id=user_id)
2017-01-17 08:34:47 +00:00
for k, v in get_user_granted_assets(user).items():
k.system_users_granted = v
queryset.append(k)
2016-11-15 11:33:04 +00:00
return queryset
class UserGrantedAssetGroupsApi(APIView):
permission_classes = (IsValidUser,)
def get(self, request, *args, **kwargs):
asset_groups = {}
user_id = kwargs.get('pk', '')
user = get_object_or_404(User, id=user_id)
assets = get_user_granted_assets(user)
for asset in assets:
for asset_group in asset.groups.all():
if asset_group.id in asset_groups:
asset_groups[asset_group.id]['assets_amount'] += 1
else:
asset_groups[asset_group.id] = {
'id': asset_group.id,
'name': asset_group.name,
'comment': asset_group.comment,
'assets_amount': 1
}
asset_groups_json = asset_groups.values()
return Response(asset_groups_json, status=200)
class UserGrantedAssetGroupsWithAssetsApi(ListAPIView):
"""
授权用户的资产组这里的资产组并非是授权列表中授权的
而是把所有资产取出来然后反查出所有资产组然后合并得到
结果里也包含资产组下授权的资产
数据结构如下
[
{
"id": 1,
"name": "资产组1",
... 其它属性
"assets_granted": [
{
"id": 1,
"hostname": "testserver",
"ip": "192.168.1.1",
"port": 22,
"system_users_granted": [
"id": 1,
"name": "web",
"username": "web",
"protocol": "ssh",
]
}
]
}
]
"""
2017-10-22 09:53:46 +00:00
permission_classes = (IsSuperUserOrAppUser,)
2017-11-14 01:44:16 +00:00
serializer_class = AssetGroupGrantedSerializer
2016-11-15 11:33:04 +00:00
def get_queryset(self):
user_id = self.kwargs.get('pk', '')
2017-11-01 15:23:11 +00:00
if not user_id:
return []
user = get_object_or_404(User, id=user_id)
2017-11-14 01:44:16 +00:00
asset_groups = get_user_granted_asset_groups(user)
2017-11-01 15:23:11 +00:00
2017-11-14 01:44:16 +00:00
queryset = []
for asset_group, assets_system_users in asset_groups.items():
assets = []
for asset, system_users in assets_system_users:
asset.system_users_granted = system_users
assets.append(asset)
asset_group.assets_granted = assets
queryset.append(asset_group)
2016-11-15 11:33:04 +00:00
return queryset
class MyGrantedAssetsApi(ListAPIView):
"""
用户自己查询授权的资产列表
2017-01-17 08:34:47 +00:00
"""
2016-10-19 11:30:55 +00:00
permission_classes = (IsValidUser,)
2016-11-10 14:06:23 +00:00
serializer_class = AssetGrantedSerializer
2016-10-19 11:30:55 +00:00
2016-11-10 14:06:23 +00:00
def get_queryset(self):
2017-01-17 08:34:47 +00:00
queryset = []
2016-11-10 14:06:23 +00:00
user = self.request.user
2016-10-19 11:30:55 +00:00
if user:
2017-01-17 08:34:47 +00:00
for asset, system_users in get_user_granted_assets(user).items():
asset.system_users_granted = system_users
queryset.append(asset)
2016-11-15 11:33:04 +00:00
return queryset
2016-10-19 11:30:55 +00:00
2016-11-04 10:33:16 +00:00
class MyGrantedAssetGroupsApi(APIView):
2017-11-01 15:23:11 +00:00
"""
授权的所有资产组并非是授权列表中的而是经过计算得来的
2017-11-01 15:23:11 +00:00
"""
2016-11-04 10:33:16 +00:00
permission_classes = (IsValidUser,)
def get(self, request, *args, **kwargs):
asset_groups = {}
user = request.user
if user:
assets = get_user_granted_assets(user)
for asset in assets:
for asset_group in asset.groups.all():
if asset_group.id in asset_groups:
2016-12-28 16:29:59 +00:00
asset_groups[asset_group.id]['assets_amount'] += 1
2016-11-04 10:33:16 +00:00
else:
asset_groups[asset_group.id] = {
'id': asset_group.id,
'name': asset_group.name,
2016-11-05 15:34:45 +00:00
'comment': asset_group.comment,
2016-11-15 11:44:08 +00:00
'assets_amount': 1
2016-11-04 10:33:16 +00:00
}
asset_groups_json = asset_groups.values()
2016-11-05 15:34:45 +00:00
return Response(asset_groups_json, status=200)
class MyGrantedAssetGroupsWithAssetsApi(ListAPIView):
"""
授权当前用户的资产组这里的资产组并非是授权列表中授权的
而是把所有资产取出来然后反查出所有资产组然后合并得到
结果里也包含资产组下授权的资产
数据结构如下
[
{
"id": 1,
"name": "资产组1",
... 其它属性
"assets_granted": [
{
"id": 1,
"hostname": "testserver",
"system_users_granted": [
"id": 1,
"name": "web",
"username": "web",
"protocol": "ssh",
]
}
]
}
]
"""
permission_classes = (IsValidUser,)
serializer_class = MyAssetGroupGrantedSerializer
def get_queryset(self):
user = self.request.user
asset_groups = get_user_granted_asset_groups(user)
queryset = []
for asset_group, assets_system_users in asset_groups.items():
assets = []
for asset, system_users in assets_system_users:
asset.system_users_granted = system_users
assets.append(asset)
asset_group.assets_granted = assets
queryset.append(asset_group)
return queryset
class MyAssetGroupOfAssetsApi(ListAPIView):
2017-01-17 08:34:47 +00:00
"""授权用户资产组下的资产列表, 非该资产组的所有资产,而是被授权的"""
2016-11-05 15:34:45 +00:00
permission_classes = (IsValidUser,)
2016-11-10 14:06:23 +00:00
serializer_class = AssetGrantedSerializer
2016-11-05 15:34:45 +00:00
2016-11-10 14:06:23 +00:00
def get_queryset(self):
queryset = []
asset_group_id = self.kwargs.get('pk', -1)
user = self.request.user
asset_group = get_object_or_none(AssetGroup, id=asset_group_id)
2016-11-05 15:34:45 +00:00
2016-11-10 14:06:23 +00:00
if user and asset_group:
2016-11-05 15:34:45 +00:00
assets = get_user_granted_assets(user)
2017-01-17 08:34:47 +00:00
for asset in asset_group.assets.all():
if asset in assets:
asset.system_users_granted = assets[asset]
2016-11-10 14:06:23 +00:00
queryset.append(asset)
return queryset
2016-11-15 15:09:36 +00:00
class UserGroupGrantedAssetsApi(ListAPIView):
permission_classes = (IsSuperUser,)
serializer_class = AssetGrantedSerializer
def get_queryset(self):
user_group_id = self.kwargs.get('pk', '')
if user_group_id:
2016-11-16 09:38:03 +00:00
user_group = get_object_or_404(UserGroup, id=user_group_id)
2016-11-15 15:09:36 +00:00
queryset = get_user_group_granted_assets(user_group)
else:
queryset = []
return queryset
class UserGroupGrantedAssetGroupsApi(ListAPIView):
permission_classes = (IsSuperUser,)
2017-11-14 01:44:16 +00:00
serializer_class = AssetGroupGrantedSerializer
2016-11-15 15:09:36 +00:00
def get_queryset(self):
user_group_id = self.kwargs.get('pk', '')
if user_group_id:
2016-11-16 09:38:03 +00:00
user_group = get_object_or_404(UserGroup, id=user_group_id)
2016-11-15 15:09:36 +00:00
queryset = get_user_group_granted_asset_groups(user_group)
else:
queryset = []
2017-01-17 08:34:47 +00:00
return queryset
class ValidateUserAssetPermissionView(APIView):
2017-01-17 08:34:47 +00:00
permission_classes = (IsAppUser,)
@staticmethod
def get(request):
2017-01-20 05:59:53 +00:00
user_id = request.query_params.get('user_id', '')
asset_id = request.query_params.get('asset_id', '')
system_id = request.query_params.get('system_user_id', '')
2017-01-17 08:34:47 +00:00
user = get_object_or_404(User, id=user_id)
asset = get_object_or_404(Asset, id=asset_id)
system_user = get_object_or_404(SystemUser, id=system_id)
2017-01-17 08:34:47 +00:00
assets_granted = get_user_granted_assets(user)
if system_user in assets_granted.get(asset, []):
return Response({'msg': True}, status=200)
else:
return Response({'msg': False}, status=403)