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
|
2023-02-21 05:00:04 +00:00
|
|
|
|
from rest_framework.generics import 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
|
|
|
|
|
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-21 05:00:04 +00:00
|
|
|
|
'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')
|
2023-02-21 05:00:04 +00:00
|
|
|
|
|
2022-12-20 08:13:44 +00:00
|
|
|
|
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 = []
|
2023-02-21 13:09:28 +00:00
|
|
|
|
accounts = self.filter_queryset(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
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 09:58:00 +00:00
|
|
|
|
def get_object(self):
|
|
|
|
|
return get_object_or_404(Account, pk=self.kwargs.get('pk'))
|
|
|
|
|
|
2022-11-01 07:04:13 +00:00
|
|
|
|
def get_queryset(self):
|
2023-02-22 09:58:00 +00:00
|
|
|
|
account = self.get_object()
|
|
|
|
|
histories = account.history.all()
|
|
|
|
|
last_history = account.history.first()
|
|
|
|
|
if not last_history:
|
|
|
|
|
return histories
|
|
|
|
|
|
|
|
|
|
if account.secret == last_history.secret \
|
|
|
|
|
and account.secret_type == last_history.secret_type:
|
|
|
|
|
histories = histories.exclude(history_id=last_history.history_id)
|
|
|
|
|
return histories
|