2019-09-17 04:34:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import uuid
|
2021-05-07 11:40:04 +00:00
|
|
|
import time
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404
|
2020-08-16 15:08:58 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
2019-09-17 04:34:47 +00:00
|
|
|
from rest_framework.views import APIView, Response
|
2021-05-07 11:40:04 +00:00
|
|
|
from rest_framework import status
|
2019-09-17 04:34:47 +00:00
|
|
|
from rest_framework.generics import (
|
2020-05-26 10:56:03 +00:00
|
|
|
ListAPIView, get_object_or_404, RetrieveAPIView, DestroyAPIView
|
2019-09-17 04:34:47 +00:00
|
|
|
)
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2021-05-07 11:40:04 +00:00
|
|
|
from perms.utils.asset.permission import get_asset_system_user_ids_with_actions_by_user, validate_permission
|
2020-08-16 15:08:58 +00:00
|
|
|
from common.permissions import IsOrgAdminOrAppUser, IsOrgAdmin, IsValidUser
|
|
|
|
from common.utils import get_logger, lazyproperty
|
|
|
|
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms.hands import User, Asset, SystemUser
|
|
|
|
from perms import serializers
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'RefreshAssetPermissionCacheApi',
|
2020-08-16 15:08:58 +00:00
|
|
|
'UserGrantedAssetSystemUsersForAdminApi',
|
2019-09-17 04:34:47 +00:00
|
|
|
'ValidateUserAssetPermissionApi',
|
|
|
|
'GetUserAssetPermissionActionsApi',
|
2020-05-26 10:56:03 +00:00
|
|
|
'UserAssetPermissionsCacheApi',
|
2020-08-16 15:08:58 +00:00
|
|
|
'MyGrantedAssetSystemUsersApi',
|
2019-09-17 04:34:47 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-02-03 04:01:18 +00:00
|
|
|
@method_decorator(tmp_to_root_org(), name='get')
|
2020-08-16 15:08:58 +00:00
|
|
|
class GetUserAssetPermissionActionsApi(RetrieveAPIView):
|
2019-09-17 04:34:47 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
serializer_class = serializers.ActionsSerializer
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
def get_user(self):
|
2019-09-17 04:34:47 +00:00
|
|
|
user_id = self.request.query_params.get('user_id', '')
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
|
|
|
return user
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
asset_id = self.request.query_params.get('asset_id', '')
|
|
|
|
system_id = self.request.query_params.get('system_user_id', '')
|
|
|
|
|
|
|
|
try:
|
|
|
|
asset_id = uuid.UUID(asset_id)
|
|
|
|
system_id = uuid.UUID(system_id)
|
|
|
|
except ValueError:
|
|
|
|
return Response({'msg': False}, status=403)
|
|
|
|
|
|
|
|
asset = get_object_or_404(Asset, id=asset_id)
|
|
|
|
system_user = get_object_or_404(SystemUser, id=system_id)
|
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
system_users_actions = get_asset_system_user_ids_with_actions_by_user(self.get_user(), asset)
|
2020-03-12 08:24:38 +00:00
|
|
|
actions = system_users_actions.get(system_user.id)
|
2019-09-17 04:34:47 +00:00
|
|
|
return {"actions": actions}
|
|
|
|
|
|
|
|
|
2021-02-03 04:01:18 +00:00
|
|
|
@method_decorator(tmp_to_root_org(), name='get')
|
2020-08-16 15:08:58 +00:00
|
|
|
class ValidateUserAssetPermissionApi(APIView):
|
2019-09-17 04:34:47 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
|
2019-12-25 02:29:58 +00:00
|
|
|
def get_cache_policy(self):
|
|
|
|
return 0
|
|
|
|
|
2019-09-17 04:34:47 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2021-05-07 11:40:04 +00:00
|
|
|
user_id = self.request.query_params.get('user_id', '')
|
2019-09-17 04:34:47 +00:00
|
|
|
asset_id = request.query_params.get('asset_id', '')
|
|
|
|
system_id = request.query_params.get('system_user_id', '')
|
|
|
|
action_name = request.query_params.get('action_name', '')
|
|
|
|
|
2021-12-22 09:35:32 +00:00
|
|
|
data = {
|
|
|
|
'has_permission': False,
|
|
|
|
'expire_at': int(time.time()),
|
|
|
|
'actions': []
|
|
|
|
}
|
|
|
|
|
2021-05-07 11:40:04 +00:00
|
|
|
if not all((user_id, asset_id, system_id, action_name)):
|
2021-12-22 09:35:32 +00:00
|
|
|
return Response(data)
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2021-05-07 11:40:04 +00:00
|
|
|
user = User.objects.get(id=user_id)
|
|
|
|
asset = Asset.objects.valid().get(id=asset_id)
|
|
|
|
system_user = SystemUser.objects.get(id=system_id)
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2021-12-22 09:35:32 +00:00
|
|
|
has_perm, actions, expire_at = validate_permission(user, asset, system_user, action_name)
|
|
|
|
status_code = status.HTTP_200_OK if has_perm else status.HTTP_403_FORBIDDEN
|
|
|
|
data = {
|
|
|
|
'has_permission': has_perm,
|
|
|
|
'actions': actions,
|
|
|
|
'expire_at': int(expire_at)
|
|
|
|
}
|
|
|
|
return Response(data, status=status_code)
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
# TODO 删除
|
2019-09-17 04:34:47 +00:00
|
|
|
class RefreshAssetPermissionCacheApi(RetrieveAPIView):
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
return Response({'msg': True}, status=200)
|
|
|
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
class UserGrantedAssetSystemUsersForAdminApi(ListAPIView):
|
2019-09-17 04:34:47 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
serializer_class = serializers.AssetSystemUserSerializer
|
|
|
|
only_fields = serializers.AssetSystemUserSerializer.Meta.only_fields
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
@lazyproperty
|
|
|
|
def user(self):
|
|
|
|
user_id = self.kwargs.get('pk')
|
|
|
|
return User.objects.get(id=user_id)
|
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
def get_asset_system_user_ids_with_actions(self, asset):
|
|
|
|
return get_asset_system_user_ids_with_actions_by_user(self.user, asset)
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2019-09-17 04:34:47 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
asset_id = self.kwargs.get('asset_id')
|
2021-03-19 09:42:59 +00:00
|
|
|
asset = get_object_or_404(Asset, id=asset_id, is_active=True)
|
2021-03-08 02:08:51 +00:00
|
|
|
system_users_with_actions = self.get_asset_system_user_ids_with_actions(asset)
|
|
|
|
system_user_ids = system_users_with_actions.keys()
|
|
|
|
system_users = SystemUser.objects.filter(id__in=system_user_ids)\
|
2020-03-12 08:24:38 +00:00
|
|
|
.only(*self.serializer_class.Meta.only_fields) \
|
2021-03-11 07:05:40 +00:00
|
|
|
.order_by('name')
|
2020-03-12 08:24:38 +00:00
|
|
|
system_users = list(system_users)
|
|
|
|
for system_user in system_users:
|
|
|
|
actions = system_users_with_actions.get(system_user.id, 0)
|
2019-09-17 04:34:47 +00:00
|
|
|
system_user.actions = actions
|
|
|
|
return system_users
|
2020-02-15 12:49:20 +00:00
|
|
|
|
2020-05-26 10:56:03 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
@method_decorator(tmp_to_root_org(), name='list')
|
|
|
|
class MyGrantedAssetSystemUsersApi(UserGrantedAssetSystemUsersForAdminApi):
|
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def user(self):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
|
|
|
|
# TODO 删除
|
|
|
|
class UserAssetPermissionsCacheApi(DestroyAPIView):
|
2020-05-26 10:56:03 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
return Response(status=204)
|