diff --git a/apps/applications/api.py b/apps/applications/api.py index 5ba5d82bb..a54c382b9 100644 --- a/apps/applications/api.py +++ b/apps/applications/api.py @@ -23,17 +23,20 @@ class TerminalRegisterView(ListCreateAPIView): def create(self, request, *args, **kwargs): name = request.data.get('name', '') - remote_addr = request.META.get('X-Real-IP') or request.META.get('REMOTE_ADDR') - serializer = self.serializer_class(data={'name': name, 'remote_addr': remote_addr}) + remote_addr = request.META.get('X-Real-IP') or \ + request.META.get('REMOTE_ADDR') + serializer = self.serializer_class( + data={'name': name, 'remote_addr': remote_addr}) if get_object_or_none(Terminal, name=name): - return Response({'msg': 'Already register, Need administrator active it'}, status=200) + return Response({'msg': 'Already register, Need ' + 'administrator active it'}, status=200) if serializer.is_valid(): terminal = serializer.save() app_user, access_key = terminal.create_related_app_user() data = {} - data['applications'] = copy.deepcopy(serializer.data) + data['terminal'] = copy.deepcopy(serializer.data) data['user'] = app_user.to_json() data['access_key_id'] = access_key.id data['access_key_secret'] = access_key.secret @@ -54,11 +57,11 @@ class TerminalViewSet(viewsets.ModelViewSet): def create(self, request, *args, **kwargs): return Response({'msg': 'Use register view except that'}, status=404) - def destroy(self, request, *args, **kwargs): - instance = self.get_object() - if instance.user is not None: - instance.user.delete() - return super(TerminalViewSet, self).destroy(request, *args, **kwargs) + # def destroy(self, request, *args, **kwargs): + # instance = self.get_object() + # if instance.user is not None: + # instance.user.delete() + # return super(TerminalViewSet, self).destroy(request, *args, **kwargs) class TerminalHeatbeatViewSet(viewsets.ModelViewSet): diff --git a/apps/applications/serializers.py b/apps/applications/serializers.py index 31d67418d..56f05e67c 100644 --- a/apps/applications/serializers.py +++ b/apps/applications/serializers.py @@ -14,8 +14,9 @@ class TerminalSerializer(serializers.ModelSerializer): class Meta: model = Terminal - fields = ['id', 'name', 'remote_addr', 'type', 'url', 'comment', 'is_accepted', - 'is_active', 'get_type_display', 'proxy_online', 'is_alive'] + fields = ['id', 'name', 'remote_addr', 'type', 'url', 'comment', + 'is_accepted', 'is_active', 'get_type_display', + 'proxy_online', 'is_alive'] @staticmethod def get_proxy_online(obj): @@ -31,6 +32,7 @@ class TerminalSerializer(serializers.ModelSerializer): class TerminalHeatbeatSerializer(serializers.ModelSerializer): + date_start = serializers.DateTimeField class Meta: model = TerminalHeatbeat diff --git a/apps/assets/forms.py b/apps/assets/forms.py index ef9427e48..602a68c7c 100644 --- a/apps/assets/forms.py +++ b/apps/assets/forms.py @@ -214,23 +214,9 @@ class AdminUserForm(forms.ModelForm): class SystemUserForm(forms.ModelForm): # Admin user assets define, let user select, save it in form not in view - assets = forms.ModelMultipleChoiceField(queryset=Asset.objects.all(), - label=_('Asset'), - required=False, - widget=forms.SelectMultiple( - attrs={'class': 'select2', 'data-placeholder': _('Select assets')}) - ) - asset_groups = forms.ModelMultipleChoiceField(queryset=AssetGroup.objects.all(), - label=_('Asset group'), - required=False, - widget=forms.SelectMultiple( - attrs={'class': 'select2', - 'data-placeholder': _('Select asset groups')}) - ) - auto_generate_key = forms.BooleanField(initial=True) + auto_generate_key = forms.BooleanField(initial=True, required=False) # Form field name can not start with `_`, so redefine it, - password = forms.CharField(widget=forms.PasswordInput, max_length=100, min_length=8, strip=True, - help_text=_('If also set private key, use that first'), required=False) + password = forms.CharField(widget=forms.PasswordInput, max_length=100, min_length=8, strip=True) # Need use upload private key file except paste private key content private_key_file = forms.FileField(required=False) @@ -242,24 +228,15 @@ class SystemUserForm(forms.ModelForm): initial['asset_groups'] = kwargs['instance'].asset_groups.all() super(SystemUserForm, self).__init__(*args, **kwargs) - def _save_m2m(self): - # Save assets relation with admin user - super(SystemUserForm, self)._save_m2m() - assets = self.cleaned_data['assets'] - asset_groups = self.cleaned_data['asset_groups'] - self.instance.assets.clear() - self.instance.assets.add(*tuple(assets)) - self.instance.asset_groups.clear() - self.instance.asset_groups.add(*tuple(asset_groups)) - def save(self, commit=True): # Because we define custom field, so we need rewrite :method: `save` system_user = super(SystemUserForm, self).save(commit=commit) password = self.cleaned_data['password'] private_key_file = self.cleaned_data['private_key_file'] - if password: - system_user.password = password + if system_user.auth_method == 'P': + if password: + system_user.password = password print(password) # Todo: Validate private key file, and generate public key # Todo: Auto generate private key and public key @@ -268,11 +245,30 @@ class SystemUserForm(forms.ModelForm): system_user.save() return self.instance + # Todo: check valid + # def clean_private_key_file(self): + # if not self.cleaned_data['auto_generate_key']: + # if not self.cleaned_data['private_key_file']: + # raise forms.ValidationError(_('Private key required')) + + # def clean_password(self): + # if self.cleaned_data['auth_method'] == 'P': + # if not self.cleaned_data['password']: + # raise forms.ValidationError(_('Password required')) + # return self.cleaned_data['password'] + + # def clean(self): + # password = self.cleaned_data['password'] + # private_key_file = self.cleaned_data.get('private_key_file', '') + # + # if not (password or private_key_file): + # raise forms.ValidationError(_('Password and private key file must be input one')) + class Meta: model = SystemUser fields = [ 'name', 'username', 'protocol', 'auto_generate_key', 'password', 'private_key_file', 'auth_method', - 'auto_push', 'auto_update', 'sudo', 'comment', 'shell', 'home', 'uid', + 'auto_push', 'sudo', 'comment', 'shell', 'home', 'uid', ] widgets = { 'name': forms.TextInput(attrs={'placeholder': _('Name')}), @@ -282,7 +278,6 @@ class SystemUserForm(forms.ModelForm): 'name': '* required', 'username': '* required', 'auto_push': 'Auto push system user to asset', - 'auto_update': 'Auto update system user ssh key', } diff --git a/apps/assets/serializers.py b/apps/assets/serializers.py index 87fec5767..a9c9c41a8 100644 --- a/apps/assets/serializers.py +++ b/apps/assets/serializers.py @@ -99,6 +99,12 @@ class SystemUserSerializer(serializers.ModelSerializer): return fields +class AssetSystemUserSerializer(serializers.ModelSerializer): + class Meta: + model = SystemUser + fields = ('id', 'name', 'username', 'protocol', 'auth_method', 'comment') + + class SystemUserUpdateAssetsSerializer(serializers.ModelSerializer): assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all()) @@ -145,13 +151,13 @@ class AssetSerializer(BulkSerializerMixin, serializers.ModelSerializer): class AssetGrantedSerializer(serializers.ModelSerializer): - system_users = SystemUserSerializer(many=True, read_only=True) + system_users_granted = AssetSystemUserSerializer(many=True, read_only=True) is_inherited = serializers.SerializerMethodField() system_users_join = serializers.SerializerMethodField() class Meta(object): model = Asset - fields = ("id", "hostname", "ip", "port", "system_users", "is_inherited", + fields = ("id", "hostname", "ip", "port", "system_users_granted", "is_inherited", "is_active", "system_users_join", "comment") @staticmethod @@ -163,7 +169,7 @@ class AssetGrantedSerializer(serializers.ModelSerializer): @staticmethod def get_system_users_join(obj): - return ', '.join([system_user.username for system_user in obj.system_users.all()]) + return ', '.join([system_user.username for system_user in obj.system_users_granted]) class IDCSerializer(BulkSerializerMixin, serializers.ModelSerializer): diff --git a/apps/assets/templates/assets/system_user_create_update.html b/apps/assets/templates/assets/system_user_create_update.html index ea8d466d9..f07109f91 100644 --- a/apps/assets/templates/assets/system_user_create_update.html +++ b/apps/assets/templates/assets/system_user_create_update.html @@ -27,23 +27,31 @@