diff --git a/apps/assets/api/system_user.py b/apps/assets/api/system_user.py index f95303a5e..5113cc30a 100644 --- a/apps/assets/api/system_user.py +++ b/apps/assets/api/system_user.py @@ -9,9 +9,8 @@ from common.mixins.api import SuggestionMixin from orgs.mixins.api import OrgBulkModelViewSet from orgs.mixins import generics from orgs.utils import tmp_to_root_org -from ..models import SystemUser, CommandFilterRule +from ..models import SystemUser, CommandFilterRule, Account from .. import serializers -from ..serializers import SystemUserWithAuthInfoSerializer, SystemUserTempAuthSerializer from ..tasks import ( push_system_user_to_assets_manual, test_system_user_connectivity_manual, push_system_user_to_assets @@ -21,7 +20,7 @@ logger = get_logger(__file__) __all__ = [ 'SystemUserViewSet', 'SystemUserAuthInfoApi', 'SystemUserAssetAuthInfoApi', 'SystemUserCommandFilterRuleListApi', 'SystemUserTaskApi', 'SystemUserAssetsListView', - 'SystemUserTempAuthInfoApi', 'SystemUserAppAuthInfoApi', + 'SystemUserTempAuthInfoApi', 'SystemUserAppAuthInfoApi', 'SystemUserAssetAccountApi' ] @@ -77,12 +76,23 @@ class SystemUserViewSet(SuggestionMixin, OrgBulkModelViewSet): return Response(serializer.data) +class SystemUserAssetAccountApi(generics.RetrieveUpdateDestroyAPIView): + model = Account + serializer_class = serializers.AccountSerializer + + def get_object(self): + asset_id = self.kwargs.get('asset_id') + user_id = self.request.query_params.get("user_id") + system_user = super().get_object() + return system_user.get_account(user_id, asset_id) + + class SystemUserAuthInfoApi(generics.RetrieveUpdateDestroyAPIView): """ Get system user auth info """ model = SystemUser - serializer_class = SystemUserWithAuthInfoSerializer + serializer_class = serializers.SystemUserWithAuthInfoSerializer rbac_perms = { 'retrieve': 'assets.view_systemusersecret', 'list': 'assets.view_systemusersecret', @@ -99,7 +109,7 @@ class SystemUserAuthInfoApi(generics.RetrieveUpdateDestroyAPIView): class SystemUserTempAuthInfoApi(generics.CreateAPIView): model = SystemUser permission_classes = (IsValidUser,) - serializer_class = SystemUserTempAuthSerializer + serializer_class = serializers.SystemUserTempAuthSerializer def create(self, request, *args, **kwargs): serializer = super().get_serializer(data=request.data) @@ -120,7 +130,7 @@ class SystemUserAssetAuthInfoApi(generics.RetrieveAPIView): Get system user with asset auth info """ model = SystemUser - serializer_class = SystemUserWithAuthInfoSerializer + serializer_class = serializers.SystemUserWithAuthInfoSerializer def get_object(self): instance = super().get_object() @@ -136,7 +146,7 @@ class SystemUserAppAuthInfoApi(generics.RetrieveAPIView): Get system user with asset auth info """ model = SystemUser - serializer_class = SystemUserWithAuthInfoSerializer + serializer_class = serializers.SystemUserWithAuthInfoSerializer rbac_perms = { 'retrieve': 'assets.view_systemusersecret', } diff --git a/apps/assets/models/user.py b/apps/assets/models/user.py index 5affa7367..0f6b51e0f 100644 --- a/apps/assets/models/user.py +++ b/apps/assets/models/user.py @@ -9,6 +9,7 @@ from django.utils.translation import ugettext_lazy as _ from django.core.validators import MinValueValidator, MaxValueValidator from common.utils import signer +from users.models import User from .base import BaseUser from .asset import Asset @@ -203,6 +204,21 @@ class SystemUser(ProtocolMixin, BaseUser): def create_accounts_with_assets(cls, asset_ids, system_user_ids): pass + def get_manual_account(self, user_id, asset_id): + pass + + def get_auto_account(self, user_id, asset_id): + username = self.username + if self.username_same_with_user: + user = get_object_or_404(User, id=user_id) + username = user.username + + def get_account(self, user_id, asset_id): + if self.login_mode == self.LOGIN_AUTO: + return self.get_manual_account(user_id, asset_id) + else: + return self.get_auto_account(user_id, asset_id) + class Meta: ordering = ['name'] unique_together = [('name', 'org_id')] diff --git a/apps/assets/urls/api_urls.py b/apps/assets/urls/api_urls.py index e8658ec0f..5876c5030 100644 --- a/apps/assets/urls/api_urls.py +++ b/apps/assets/urls/api_urls.py @@ -49,6 +49,7 @@ urlpatterns = [ path('system-users//assets/', api.SystemUserAssetsListView.as_view(), name='system-user-assets'), path('system-users//assets//auth-info/', api.SystemUserAssetAuthInfoApi.as_view(), name='system-user-asset-auth-info'), path('system-users//applications//auth-info/', api.SystemUserAppAuthInfoApi.as_view(), name='system-user-app-auth-info'), + path('system-users//assets//account/', api.SystemUserAssetAccountApi.as_view(), name='system-user-asset-account'), path('system-users//temp-auth/', api.SystemUserTempAuthInfoApi.as_view(), name='system-user-asset-temp-info'), path('system-users//tasks/', api.SystemUserTaskApi.as_view(), name='system-user-task-create'), path('system-users//cmd-filter-rules/', api.SystemUserCommandFilterRuleListApi.as_view(), name='system-user-cmd-filter-rule-list'),