2019-09-17 04:34:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import uuid
|
2021-05-07 11:40:04 +00:00
|
|
|
import time
|
2022-09-28 10:40:33 +00:00
|
|
|
from collections import defaultdict
|
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 (
|
2022-08-17 03:54:18 +00:00
|
|
|
ListAPIView, get_object_or_404, RetrieveAPIView
|
2019-09-17 04:34:47 +00:00
|
|
|
)
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2022-08-23 02:26:43 +00:00
|
|
|
from perms.utils.permission import (
|
|
|
|
get_asset_system_user_ids_with_actions_by_user, validate_permission
|
|
|
|
)
|
2022-02-17 12:13:31 +00:00
|
|
|
from common.permissions import IsValidUser
|
2020-08-16 15:08:58 +00:00
|
|
|
from common.utils import get_logger, lazyproperty
|
|
|
|
|
2022-09-23 07:59:37 +00:00
|
|
|
from perms.hands import User, Asset, Account
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms import serializers
|
2022-09-28 10:40:33 +00:00
|
|
|
from perms.models import AssetPermission, Action
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'ValidateUserAssetPermissionApi',
|
|
|
|
'GetUserAssetPermissionActionsApi',
|
2022-09-29 08:18:12 +00:00
|
|
|
'UserGrantedAssetAccountsApi',
|
|
|
|
'MyGrantedAssetAccountsApi',
|
|
|
|
'UserGrantedAssetSpecialAccountsApi',
|
|
|
|
'MyGrantedAssetSpecialAccountsApi',
|
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
|
|
|
serializer_class = serializers.ActionsSerializer
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2022-02-28 11:28:58 +00:00
|
|
|
'retrieve': 'perms.view_userassets',
|
|
|
|
'GET': 'perms.view_userassets',
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
2019-09-17 04:34:47 +00:00
|
|
|
|
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', '')
|
2022-08-17 03:54:18 +00:00
|
|
|
account = self.request.query_params.get('account', '')
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
asset_id = uuid.UUID(asset_id)
|
|
|
|
except ValueError:
|
|
|
|
return Response({'msg': False}, status=403)
|
|
|
|
|
|
|
|
asset = get_object_or_404(Asset, id=asset_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)
|
2022-08-17 03:54:18 +00:00
|
|
|
# actions = system_users_actions.get(system_user.id)
|
|
|
|
actions = system_users_actions.get(account)
|
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):
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'GET': 'perms.view_userassets'
|
|
|
|
}
|
2019-12-25 02:29:58 +00:00
|
|
|
|
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', '')
|
2022-08-17 03:54:18 +00:00
|
|
|
account = request.query_params.get('account', '')
|
2019-09-17 04:34:47 +00:00
|
|
|
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': []
|
|
|
|
}
|
|
|
|
|
2022-08-17 03:54:18 +00:00
|
|
|
if not all((user_id, asset_id, account, 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)
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2022-08-17 03:54:18 +00:00
|
|
|
has_perm, actions, expire_at = validate_permission(user, asset, account, action_name)
|
2021-12-22 09:35:32 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2022-09-29 08:18:12 +00:00
|
|
|
class UserGrantedAssetAccountsApi(ListAPIView):
|
2022-09-15 02:46:57 +00:00
|
|
|
serializer_class = serializers.AccountsGrantedSerializer
|
|
|
|
rbac_perms = {
|
|
|
|
'list': 'perms.view_userassets'
|
|
|
|
}
|
|
|
|
|
|
|
|
@lazyproperty
|
2022-09-23 07:59:37 +00:00
|
|
|
def user(self) -> User:
|
2022-09-15 02:46:57 +00:00
|
|
|
user_id = self.kwargs.get('pk')
|
|
|
|
return User.objects.get(id=user_id)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def asset(self):
|
|
|
|
asset_id = self.kwargs.get('asset_id')
|
|
|
|
kwargs = {'id': asset_id, 'is_active': True}
|
|
|
|
asset = get_object_or_404(Asset, **kwargs)
|
|
|
|
return asset
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-09-29 08:18:12 +00:00
|
|
|
accounts = AssetPermission.get_perm_asset_accounts(user=self.user, asset=self.asset)
|
2022-09-28 10:40:33 +00:00
|
|
|
return accounts
|
2022-09-29 08:18:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MyGrantedAssetAccountsApi(UserGrantedAssetAccountsApi):
|
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def user(self):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
|
|
|
|
class UserGrantedAssetSpecialAccountsApi(ListAPIView):
|
|
|
|
serializer_class = serializers.AccountsGrantedSerializer
|
|
|
|
rbac_perms = {
|
|
|
|
'list': 'perms.view_userassets'
|
|
|
|
}
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def user(self):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-09-15 02:46:57 +00:00
|
|
|
# 构造默认包含的账号,如: @INPUT @USER
|
2022-09-29 08:18:12 +00:00
|
|
|
accounts = [
|
|
|
|
Account.get_input_account(),
|
|
|
|
Account.get_user_account(self.user.username)
|
|
|
|
]
|
|
|
|
for account in accounts:
|
|
|
|
account.actions = Action.ALL
|
|
|
|
return accounts
|
2022-09-15 02:46:57 +00:00
|
|
|
|
|
|
|
|
2022-09-29 08:18:12 +00:00
|
|
|
class MyGrantedAssetSpecialAccountsApi(UserGrantedAssetSpecialAccountsApi):
|
2022-09-15 02:46:57 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def user(self):
|
|
|
|
return self.request.user
|