2022-12-07 11:26:25 +00:00
|
|
|
|
from django.shortcuts import get_object_or_404
|
2022-02-28 11:28:58 +00:00
|
|
|
|
from rest_framework.decorators import action
|
2022-11-01 07:04:13 +00:00
|
|
|
|
from rest_framework.generics import CreateAPIView, ListAPIView
|
2022-12-21 10:36:15 +00:00
|
|
|
|
from rest_framework.response import Response
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from accounts import serializers
|
2023-02-07 11:45:12 +00:00
|
|
|
|
from accounts.filters import AccountFilterSet
|
2023-02-15 12:16:01 +00:00
|
|
|
|
from accounts.models import Account
|
2023-02-20 10:00:29 +00:00
|
|
|
|
from accounts.tasks import verify_accounts_connectivity_task, push_accounts_to_assets_task
|
2023-02-15 12:16:01 +00:00
|
|
|
|
from assets.models import Asset
|
2022-12-21 10:36:15 +00:00
|
|
|
|
from authentication.const import ConfirmType
|
|
|
|
|
from common.permissions import UserConfirmation
|
2023-02-07 11:45:12 +00:00
|
|
|
|
from common.views.mixins import RecordViewLogMixin
|
2022-12-21 10:36:15 +00:00
|
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2022-12-07 11:30:26 +00:00
|
|
|
|
__all__ = [
|
2023-02-16 10:32:04 +00:00
|
|
|
|
'AccountViewSet', 'AccountSecretsViewSet',
|
2023-02-20 10:00:29 +00:00
|
|
|
|
'AccountsTaskCreateAPI', 'AccountHistoriesSecretAPI'
|
2022-12-07 11:30:26 +00:00
|
|
|
|
]
|
2021-07-12 07:55:29 +00:00
|
|
|
|
|
2022-12-21 10:36:15 +00:00
|
|
|
|
from rbac.permissions import RBACPermission
|
|
|
|
|
|
2021-07-12 07:55:29 +00:00
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
class AccountViewSet(OrgBulkModelViewSet):
|
2022-07-14 02:56:09 +00:00
|
|
|
|
model = Account
|
2022-09-29 06:44:27 +00:00
|
|
|
|
search_fields = ('username', 'asset__address', 'name')
|
2021-07-12 07:55:29 +00:00
|
|
|
|
filterset_class = AccountFilterSet
|
2021-07-08 06:23:18 +00:00
|
|
|
|
serializer_classes = {
|
|
|
|
|
'default': serializers.AccountSerializer,
|
|
|
|
|
}
|
2022-02-22 07:37:56 +00:00
|
|
|
|
rbac_perms = {
|
2023-02-16 11:39:10 +00:00
|
|
|
|
'partial_update': ['accounts.change_account'],
|
2023-02-14 03:37:57 +00:00
|
|
|
|
'su_from_accounts': 'accounts.view_account',
|
2022-02-22 07:37:56 +00:00
|
|
|
|
}
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2022-12-20 08:13:44 +00:00
|
|
|
|
@action(methods=['get'], detail=False, url_path='su-from-accounts')
|
2022-12-06 09:32:48 +00:00
|
|
|
|
def su_from_accounts(self, request, *args, **kwargs):
|
2022-12-20 08:13:44 +00:00
|
|
|
|
account_id = request.query_params.get('account')
|
|
|
|
|
asset_id = request.query_params.get('asset')
|
|
|
|
|
if account_id:
|
|
|
|
|
account = get_object_or_404(Account, pk=account_id)
|
|
|
|
|
accounts = account.get_su_from_accounts()
|
|
|
|
|
elif asset_id:
|
|
|
|
|
asset = get_object_or_404(Asset, pk=asset_id)
|
|
|
|
|
accounts = asset.accounts.all()
|
|
|
|
|
else:
|
|
|
|
|
accounts = []
|
2022-12-06 09:32:48 +00:00
|
|
|
|
serializer = serializers.AccountSerializer(accounts, many=True)
|
|
|
|
|
return Response(data=serializer.data)
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2022-05-05 06:42:09 +00:00
|
|
|
|
class AccountSecretsViewSet(RecordViewLogMixin, AccountViewSet):
|
2021-07-08 06:23:18 +00:00
|
|
|
|
"""
|
|
|
|
|
因为可能要导出所有账号,所以单独建立了一个 viewset
|
|
|
|
|
"""
|
|
|
|
|
serializer_classes = {
|
2022-11-01 03:13:18 +00:00
|
|
|
|
'default': serializers.AccountSecretSerializer,
|
2021-07-08 06:23:18 +00:00
|
|
|
|
}
|
2022-10-31 09:37:54 +00:00
|
|
|
|
http_method_names = ['get', 'options']
|
2023-02-17 04:30:11 +00:00
|
|
|
|
permission_classes = [RBACPermission, UserConfirmation.require(ConfirmType.MFA)]
|
2022-02-17 12:13:31 +00:00
|
|
|
|
rbac_perms = {
|
2023-02-14 03:29:04 +00:00
|
|
|
|
'list': 'accounts.view_accountsecret',
|
|
|
|
|
'retrieve': 'accounts.view_accountsecret',
|
2022-02-17 12:13:31 +00:00
|
|
|
|
}
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2022-11-01 07:04:13 +00:00
|
|
|
|
|
|
|
|
|
class AccountHistoriesSecretAPI(RecordViewLogMixin, ListAPIView):
|
|
|
|
|
model = Account.history.model
|
|
|
|
|
serializer_class = serializers.AccountHistorySerializer
|
|
|
|
|
http_method_names = ['get', 'options']
|
2022-12-21 10:37:05 +00:00
|
|
|
|
permission_classes = [RBACPermission, UserConfirmation.require(ConfirmType.MFA)]
|
2022-11-01 07:04:13 +00:00
|
|
|
|
rbac_perms = {
|
2023-02-20 02:56:50 +00:00
|
|
|
|
'list': 'accounts.view_accountsecret',
|
2022-11-01 07:04:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
return self.model.objects.filter(id=self.kwargs.get('pk'))
|
2022-10-20 08:39:11 +00:00
|
|
|
|
|
2021-07-13 04:18:19 +00:00
|
|
|
|
|
2023-02-20 10:00:29 +00:00
|
|
|
|
class AccountsTaskCreateAPI(CreateAPIView):
|
2021-07-13 04:18:19 +00:00
|
|
|
|
serializer_class = serializers.AccountTaskSerializer
|
|
|
|
|
search_fields = AccountViewSet.search_fields
|
|
|
|
|
filterset_class = AccountViewSet.filterset_class
|
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
|
def check_permissions(self, request):
|
|
|
|
|
return request.user.has_perm('assets.test_assetconnectivity')
|
|
|
|
|
|
2021-07-13 04:18:19 +00:00
|
|
|
|
def get_accounts(self):
|
2022-07-14 02:56:09 +00:00
|
|
|
|
queryset = Account.objects.all()
|
2021-07-13 04:18:19 +00:00
|
|
|
|
queryset = self.filter_queryset(queryset)
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
2023-02-20 10:00:29 +00:00
|
|
|
|
data = serializer.validated_data
|
2023-02-20 15:50:33 +00:00
|
|
|
|
accounts = data.get('accounts')
|
2022-11-02 09:27:47 +00:00
|
|
|
|
account_ids = accounts.values_list('id', flat=True)
|
|
|
|
|
asset_ids = [account.asset_id for account in accounts]
|
2023-02-20 10:00:29 +00:00
|
|
|
|
|
|
|
|
|
if data['action'] == 'push':
|
|
|
|
|
task = push_accounts_to_assets_task.delay(account_ids, asset_ids)
|
|
|
|
|
else:
|
|
|
|
|
task = verify_accounts_connectivity_task.delay(account_ids, asset_ids)
|
|
|
|
|
|
2021-07-13 04:18:19 +00:00
|
|
|
|
data = getattr(serializer, '_data', {})
|
|
|
|
|
data["task"] = task.id
|
|
|
|
|
setattr(serializer, '_data', data)
|
|
|
|
|
return task
|
|
|
|
|
|
|
|
|
|
def get_exception_handler(self):
|
|
|
|
|
def handler(e, context):
|
|
|
|
|
return Response({"error": str(e)}, status=400)
|
2022-12-21 10:36:15 +00:00
|
|
|
|
|
2021-07-13 04:18:19 +00:00
|
|
|
|
return handler
|