diff --git a/docs/changelog.rst b/docs/changelog.rst index 55cbdb4..c1e5e4c 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,8 @@ Changelog ========= +* :feature:`-` add ability to add, list and delete SSH public keys assigned to + a hosting package's operating system user and change their comments * :feature:`-` add ability to add SSH public keys for operating system users * :support:`-` make tests in osusers.tests work again * :support:`-` minor HTML improvements diff --git a/gnuviechadmin/hostingpackages/views.py b/gnuviechadmin/hostingpackages/views.py index 75e7cb8..de6b029 100644 --- a/gnuviechadmin/hostingpackages/views.py +++ b/gnuviechadmin/hostingpackages/views.py @@ -121,6 +121,7 @@ class CustomerHostingPackageDetails(StaffOrSelfLoginRequiredMixin, DetailView): 'domains': context['hostingpackage'].domains.all(), 'mailboxes': context['hostingpackage'].mailboxes, }) + context['sshkeys'] = context['osuser'].sshpublickey_set.all() return context diff --git a/gnuviechadmin/osusers/forms.py b/gnuviechadmin/osusers/forms.py index 89fb10c..ed1a6c1 100644 --- a/gnuviechadmin/osusers/forms.py +++ b/gnuviechadmin/osusers/forms.py @@ -110,3 +110,25 @@ class AddSshPublicKeyForm(forms.ModelForm): self.instance.data = keydata self.instance.comment = comment return super(AddSshPublicKeyForm, self).save(commit) + + +class EditSshPublicKeyCommentForm(forms.ModelForm): + """ + A form for editing :py:class:`SSH public key + ` comment fields. + + """ + class Meta: + model = SshPublicKey + fields = ['comment'] + + def __init__(self, *args, **kwargs): + hosting_package = kwargs.pop('hostingpackage') + self.osuser = hosting_package.osuser + super(EditSshPublicKeyCommentForm, self).__init__(*args, **kwargs) + self.fields['comment'].widget = forms.TextInput() + self.helper = FormHelper() + self.helper.form_action = reverse( + 'edit_ssh_key_comment', + kwargs={'package': hosting_package.id, 'pk': self.instance.id}) + self.helper.add_input(Submit('submit', _('Change Comment'))) diff --git a/gnuviechadmin/osusers/urls.py b/gnuviechadmin/osusers/urls.py index c2cacf6..52c2657 100644 --- a/gnuviechadmin/osusers/urls.py +++ b/gnuviechadmin/osusers/urls.py @@ -8,6 +8,9 @@ from django.conf.urls import patterns, url from .views import ( AddSshPublicKey, + DeleteSshPublicKey, + EditSshPublicKeyComment, + ListSshPublicKeys, SetOsUserPassword, ) @@ -16,6 +19,12 @@ urlpatterns = patterns( '', url(r'^(?P[\w0-9@.+-_]+)/setpassword$', SetOsUserPassword.as_view(), name='set_osuser_password'), - url(r'^(?P\d+)/add-ssh-key$', AddSshPublicKey.as_view(), + url(r'^(?P\d+)/ssh-keys/$', ListSshPublicKeys.as_view(), + name='list_ssh_keys'), + url(r'^(?P\d+)/ssh-keys/add$', AddSshPublicKey.as_view(), name='add_ssh_key'), + url(r'^(?P\d+)/ssh-keys/(?P\d+)/edit-comment$', + EditSshPublicKeyComment.as_view(), name='edit_ssh_key_comment'), + url(r'^(?P\d+)/ssh-keys/(?P\d+)/delete$', + DeleteSshPublicKey.as_view(), name='delete_ssh_key'), ) diff --git a/gnuviechadmin/osusers/views.py b/gnuviechadmin/osusers/views.py index 8d1098a..46fe4e8 100644 --- a/gnuviechadmin/osusers/views.py +++ b/gnuviechadmin/osusers/views.py @@ -4,10 +4,13 @@ This module defines the views for gnuviechadmin operating system user handling. """ from __future__ import unicode_literals, absolute_import +from django.core.urlresolvers import reverse from django.shortcuts import redirect from django.views.generic import ( - UpdateView, CreateView, + DeleteView, + ListView, + UpdateView, ) from django.utils.translation import ugettext as _ from django.contrib import messages @@ -18,6 +21,7 @@ from gvawebcore.views import HostingPackageAndCustomerMixin from .forms import ( AddSshPublicKeyForm, ChangeOsUserPasswordForm, + EditSshPublicKeyCommentForm, ) from .models import ( SshPublicKey, @@ -89,3 +93,70 @@ class AddSshPublicKey( algorithm=key.algorithm) ) return redirect(self.get_hosting_package()) + + +class ListSshPublicKeys( + HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, ListView +): + model = SshPublicKey + context_object_name = 'keys' + + def get_context_data(self, **kwargs): + context = super(ListSshPublicKeys, self).get_context_data(**kwargs) + context.update({ + 'hostingpackage': self.get_hosting_package(), + 'customer': self.get_customer_object(), + 'osuser': self.get_hosting_package().osuser.username, + }) + return context + + +class DeleteSshPublicKey( + HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, DeleteView +): + model = SshPublicKey + context_object_name = 'key' + + def get_context_data(self, **kwargs): + context = super(DeleteSshPublicKey, self).get_context_data(**kwargs) + context.update({ + 'hostingpackage': self.get_hosting_package(), + 'customer': self.get_customer_object(), + 'osuser': self.get_hosting_package().osuser.username, + }) + return context + + def get_success_url(self): + return reverse( + 'list_ssh_keys', kwargs={'package': self.get_hosting_package().id} + ) + + +class EditSshPublicKeyComment( + HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, UpdateView +): + model = SshPublicKey + context_object_name = 'key' + fields = ['comment'] + template_name_suffix = '_edit_comment' + form_class = EditSshPublicKeyCommentForm + + def get_form_kwargs(self): + kwargs = super(EditSshPublicKeyComment, self).get_form_kwargs() + kwargs['hostingpackage'] = self.get_hosting_package() + return kwargs + + def get_context_data(self, **kwargs): + context = super(EditSshPublicKeyComment, self).get_context_data( + **kwargs) + context.update({ + 'hostingpackage': self.get_hosting_package(), + 'customer': self.get_customer_object(), + 'osuser': self.get_hosting_package().osuser.username, + }) + return context + + def get_success_url(self): + return reverse( + 'list_ssh_keys', kwargs={'package': self.get_hosting_package().id} + ) diff --git a/gnuviechadmin/templates/hostingpackages/customerhostingpackage_detail.html b/gnuviechadmin/templates/hostingpackages/customerhostingpackage_detail.html index 2f88bed..0818016 100644 --- a/gnuviechadmin/templates/hostingpackages/customerhostingpackage_detail.html +++ b/gnuviechadmin/templates/hostingpackages/customerhostingpackage_detail.html @@ -39,7 +39,7 @@
{% blocktrans with num=hostingpackage.used_mailbox_count total=hostingpackage.mailbox_count %}{{ num }} of {{ total }} in use{% endblocktrans %}
{% if osuser.is_sftp_user %}{% trans "SFTP username" %}{% else %}{% trans "SSH/SFTP username" %}{% endif %}
-
{{ osuser.username }}
+
{{ osuser.username }}{% if sshkeys %} {{ sshkeys|length }}{% endif %}
{% trans "Upload server" %}
{{ uploadserver }}
diff --git a/gnuviechadmin/templates/osusers/sshpublickey_confirm_delete.html b/gnuviechadmin/templates/osusers/sshpublickey_confirm_delete.html new file mode 100644 index 0000000..022346b --- /dev/null +++ b/gnuviechadmin/templates/osusers/sshpublickey_confirm_delete.html @@ -0,0 +1,35 @@ +{% extends "osusers/base.html" %} +{% load i18n crispy_forms_tags %} + +{% block title %}{{ block.super }} - {% spaceless %} +{% if user == customer %} +{% blocktrans %}Delete SSH Public Key for Operating System User {{ osuser }}{% endblocktrans %} +{% else %} +{% blocktrans with full_name=customer.get_full_name %}Delete SSH Public Key for Operating System User {{ osuser }} of Customer {{ full_name }}{% endblocktrans %} +{% endif %} +{% endspaceless %}{% endblock title %} + +{% block page_title %}{% spaceless %} +{% if user == customer %} +{% blocktrans %}Delete SSH Public Key for Operating System User {{ osuser }}{% endblocktrans %} +{% else %} +{% blocktrans with full_name=customer.get_full_name %}Delete SSH Public Key for Operating System User {{ osuser }} of Customer {{ full_name }}{% endblocktrans %} +{% endif %} +{% endspaceless %}{% endblock page_title %} + +{% block content %} +
+
+ {% blocktrans with algorithm=key.algorithm %}Do you really want to delete the {{ algorithm }} SSH public key?{% endblocktrans %} +
+
+

{% blocktrans %}When you confirm the deletion of this key you will no longer be able to use the corresponding private key for authentication.{% endblocktrans %}

+
{{ key }}
+
+ {% csrf_token %} + + {% trans "Cancel" %} +
+
+
+{% endblock content %} diff --git a/gnuviechadmin/templates/osusers/sshpublickey_create.html b/gnuviechadmin/templates/osusers/sshpublickey_create.html index 06911ed..bfda9df 100644 --- a/gnuviechadmin/templates/osusers/sshpublickey_create.html +++ b/gnuviechadmin/templates/osusers/sshpublickey_create.html @@ -3,7 +3,7 @@ {% block title %}{{ block.super }} - {% spaceless %} {% if user == customer %} -{% blocktrans %}Add new SSH Public Key{% endblocktrans %} +{% blocktrans %}Add new SSH Public Key for Operating System User {{ osuser }}{% endblocktrans %} {% else %} {% blocktrans with full_name=customer.get_full_name %}Add a new SSH Public Key for Operating System User {{ osuser }} of Customer {{ full_name }}{% endblocktrans %} {% endif %} @@ -11,7 +11,7 @@ {% block page_title %}{% spaceless %} {% if user == customer %} -{% blocktrans %}Add new SSH Public Key{% endblocktrans %} +{% blocktrans %}Add new SSH Public Key for Operating System User {{ osuser }}{% endblocktrans %} {% else %} {% blocktrans with full_name=customer.get_full_name %}Add a new SSH Public Key for Operating System User {{ osuser }} of Customer {{ full_name }}{% endblocktrans %} {% endif %} diff --git a/gnuviechadmin/templates/osusers/sshpublickey_edit_comment.html b/gnuviechadmin/templates/osusers/sshpublickey_edit_comment.html new file mode 100644 index 0000000..dcdac74 --- /dev/null +++ b/gnuviechadmin/templates/osusers/sshpublickey_edit_comment.html @@ -0,0 +1,30 @@ +{% extends "osusers/base.html" %} +{% load i18n crispy_forms_tags %} + +{% block title %}{{ block.super }} - {% spaceless %} +{% if user == customer %} +{% blocktrans %}Edit Comment of SSH Public Key for Operating System User {{ osuser }}{% endblocktrans %} +{% else %} +{% blocktrans with full_name=customer.get_full_name %}Edit Comment of SSH Public Key for Operating System User {{ osuser }} of Customer {{ full_name }}{% endblocktrans %} +{% endif %} +{% endspaceless %}{% endblock title %} + +{% block page_title %}{% spaceless %} +{% if user == customer %} +{% blocktrans %}Edit Comment of Public Key for Operating System User {{ osuser }}{% endblocktrans %} +{% else %} +{% blocktrans with full_name=customer.get_full_name %}Edit Comment of SSH Public Key for Operating System User {{ osuser }} of Customer {{ full_name }}{% endblocktrans %} +{% endif %} +{% endspaceless %}{% endblock page_title %} + +{% block content %} +{% crispy form %} +{% endblock content %} + +{% block extra_js %} + +{% endblock extra_js %} diff --git a/gnuviechadmin/templates/osusers/sshpublickey_list.html b/gnuviechadmin/templates/osusers/sshpublickey_list.html new file mode 100644 index 0000000..e94d47b --- /dev/null +++ b/gnuviechadmin/templates/osusers/sshpublickey_list.html @@ -0,0 +1,47 @@ +{% extends "osusers/base.html" %} +{% load i18n crispy_forms_tags %} + +{% block title %}{{ block.super }} - {% spaceless %} +{% if user == customer %} +{% blocktrans %}SSH Public Keys for Operating System User {{ osuser }}{% endblocktrans %} +{% else %} +{% blocktrans with full_name=customer.get_full_name %}SSH Public Keys for Operating System User {{ osuser }} of Customer {{ full_name }}{% endblocktrans %} +{% endif %} +{% endspaceless %}{% endblock title %} + +{% block page_title %}{% spaceless %} +{% if user == customer %} +{% blocktrans %}SSH Public Keys for Operating System User {{ osuser }}{% endblocktrans %} +{% else %} +{% blocktrans with full_name=customer.get_full_name %}SSH Public Keys for Operating System User {{ osuser }} of Customer {{ full_name }}{% endblocktrans %} +{% endif %} +{% endspaceless %}{% endblock page_title %} + +{% block content %} +{% if keys %} + + + + + + + + + + {% for key in keys %} + + + + + + {% endfor %} + +
{% trans "Algorithm" %}{% trans "Comment" %}{% trans "Actions" %}
{{ key.algorithm }}{{ key.comment }} + {% trans "Delete" %} + {% trans "Edit Comment" %} +
+{% else %} +

{% trans "There are now SSH public keys set for this operating system user yet." %}

+{% endif %} +

{% trans "Add SSH public key" %}

+{% endblock content %}