2023-03-15 06:46:32 +00:00
|
|
|
from django_filters import rest_framework as drf_filters
|
2023-10-11 08:03:05 +00:00
|
|
|
from rest_framework import status
|
2023-05-09 07:29:02 +00:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
from accounts import serializers
|
2023-08-29 06:21:06 +00:00
|
|
|
from accounts.mixins import AccountRecordViewLogMixin
|
2023-10-11 08:03:05 +00:00
|
|
|
from accounts.models import AccountTemplate
|
|
|
|
from accounts.tasks import template_sync_related_accounts
|
2023-04-21 08:58:40 +00:00
|
|
|
from assets.const import Protocol
|
2023-10-12 08:17:32 +00:00
|
|
|
from authentication.permissions import UserConfirmation, ConfirmType
|
2023-04-21 08:58:40 +00:00
|
|
|
from common.drf.filters import BaseFilterSet
|
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
|
|
|
from rbac.permissions import RBACPermission
|
2023-03-15 06:46:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AccountTemplateFilterSet(BaseFilterSet):
|
|
|
|
protocols = drf_filters.CharFilter(method='filter_protocols')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AccountTemplate
|
|
|
|
fields = ('username', 'name')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def filter_protocols(queryset, name, value):
|
|
|
|
secret_types = set()
|
|
|
|
protocols = value.split(',')
|
|
|
|
protocol_secret_type_map = Protocol.settings()
|
|
|
|
for p in protocols:
|
|
|
|
if p not in protocol_secret_type_map:
|
|
|
|
continue
|
|
|
|
_st = protocol_secret_type_map[p].get('secret_types', [])
|
|
|
|
secret_types.update(_st)
|
2023-04-21 08:58:40 +00:00
|
|
|
if not secret_types:
|
|
|
|
secret_types = ['password']
|
2023-03-15 06:46:32 +00:00
|
|
|
queryset = queryset.filter(secret_type__in=secret_types)
|
|
|
|
return queryset
|
2022-08-19 10:49:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AccountTemplateViewSet(OrgBulkModelViewSet):
|
|
|
|
model = AccountTemplate
|
2023-03-15 06:46:32 +00:00
|
|
|
filterset_class = AccountTemplateFilterSet
|
2022-08-19 10:49:00 +00:00
|
|
|
search_fields = ('username', 'name')
|
|
|
|
serializer_classes = {
|
2023-05-09 07:29:02 +00:00
|
|
|
'default': serializers.AccountTemplateSerializer,
|
2022-08-19 10:49:00 +00:00
|
|
|
}
|
2023-05-09 07:29:02 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'su_from_account_templates': 'accounts.view_accounttemplate',
|
2023-10-12 10:11:51 +00:00
|
|
|
'sync_related_accounts': 'accounts.change_account',
|
2023-05-09 07:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@action(methods=['get'], detail=False, url_path='su-from-account-templates')
|
|
|
|
def su_from_account_templates(self, request, *args, **kwargs):
|
|
|
|
pk = request.query_params.get('template_id')
|
2023-06-27 02:45:50 +00:00
|
|
|
templates = AccountTemplate.get_su_from_account_templates(pk)
|
2023-05-09 07:29:02 +00:00
|
|
|
templates = self.filter_queryset(templates)
|
|
|
|
serializer = self.get_serializer(templates, many=True)
|
|
|
|
return Response(data=serializer.data)
|
2022-11-28 07:54:16 +00:00
|
|
|
|
2023-10-11 08:03:05 +00:00
|
|
|
@action(methods=['patch'], detail=True, url_path='sync-related-accounts')
|
|
|
|
def sync_related_accounts(self, request, *args, **kwargs):
|
|
|
|
instance = self.get_object()
|
|
|
|
user_id = str(request.user.id)
|
|
|
|
task = template_sync_related_accounts.delay(str(instance.id), user_id)
|
|
|
|
return Response({'task': task.id}, status=status.HTTP_200_OK)
|
|
|
|
|
2022-11-28 07:54:16 +00:00
|
|
|
|
2023-08-29 06:21:06 +00:00
|
|
|
class AccountTemplateSecretsViewSet(AccountRecordViewLogMixin, AccountTemplateViewSet):
|
2022-11-28 07:54:16 +00:00
|
|
|
serializer_classes = {
|
|
|
|
'default': serializers.AccountTemplateSecretSerializer,
|
|
|
|
}
|
|
|
|
http_method_names = ['get', 'options']
|
2023-01-16 11:02:09 +00:00
|
|
|
permission_classes = [RBACPermission, UserConfirmation.require(ConfirmType.MFA)]
|
2022-11-28 07:54:16 +00:00
|
|
|
rbac_perms = {
|
2023-02-20 02:56:50 +00:00
|
|
|
'list': 'accounts.view_accounttemplatesecret',
|
|
|
|
'retrieve': 'accounts.view_accounttemplatesecret',
|
2022-11-28 07:54:16 +00:00
|
|
|
}
|