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
|
2023-03-23 03:20:48 +00:00
|
|
|
|
from rest_framework.status import HTTP_200_OK
|
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-27 06:20:58 +00:00
|
|
|
|
from assets.models import Asset, Node
|
2023-02-23 03:27:03 +00:00
|
|
|
|
from common.permissions import UserConfirmation, ConfirmType
|
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
|
2023-02-23 03:27:03 +00:00
|
|
|
|
from rbac.permissions import RBACPermission
|
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
|
|
|
|
|
|
|
|
|
|
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',
|
2023-02-27 06:20:58 +00:00
|
|
|
|
'username_suggestions': 'accounts.view_account',
|
2023-03-23 03:20:48 +00:00
|
|
|
|
'remove_secret': 'accounts.change_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:
|
2023-03-08 02:58:37 +00:00
|
|
|
|
accounts = Account.objects.none()
|
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)
|
|
|
|
|
|
2023-02-27 06:20:58 +00:00
|
|
|
|
@action(methods=['get'], detail=False, url_path='username-suggestions')
|
|
|
|
|
def username_suggestions(self, request, *args, **kwargs):
|
|
|
|
|
asset_ids = request.query_params.get('assets')
|
|
|
|
|
node_keys = request.query_params.get('keys')
|
|
|
|
|
username = request.query_params.get('username')
|
|
|
|
|
|
|
|
|
|
assets = Asset.objects.all()
|
|
|
|
|
if asset_ids:
|
|
|
|
|
assets = assets.filter(id__in=asset_ids.split(','))
|
|
|
|
|
if node_keys:
|
|
|
|
|
patten = Node.get_node_all_children_key_pattern(node_keys.split(','))
|
|
|
|
|
assets = assets.filter(nodes__key__regex=patten)
|
|
|
|
|
|
|
|
|
|
accounts = Account.objects.filter(asset__in=assets)
|
|
|
|
|
if username:
|
|
|
|
|
accounts = accounts.filter(username__icontains=username)
|
|
|
|
|
usernames = list(accounts.values_list('username', flat=True).distinct()[:10])
|
|
|
|
|
usernames.sort()
|
|
|
|
|
common = [i for i in usernames if i in usernames if i.lower() in ['root', 'admin', 'administrator']]
|
|
|
|
|
others = [i for i in usernames if i not in common]
|
|
|
|
|
usernames = common + others
|
|
|
|
|
return Response(data=usernames)
|
|
|
|
|
|
2023-03-23 03:20:48 +00:00
|
|
|
|
@action(methods=['patch'], detail=False, url_path='remove-secret')
|
|
|
|
|
def remove_secret(self, request, *args, **kwargs):
|
|
|
|
|
account_ids = request.data.get('account_ids', [])
|
|
|
|
|
self.model.objects.filter(id__in=account_ids).update(secret=None)
|
|
|
|
|
return Response(status=HTTP_200_OK)
|
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-23 03:27:03 +00:00
|
|
|
|
'GET': '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
|