From 150366a524523eecc381f489ba192ed60aff0aed Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 24 Jan 2015 16:26:32 +0100 Subject: [PATCH] plug users and hosting packages together - document new feature in changelog - add autogenerated documentation for osusers.urls and osusers.views - add osuser URLs to gnuviechadmin.urls - implement get_absolute_url in hostingpackages.models.CustomerHostingPackage - use set_ldap_user_password instead of create_ldap_user for existing OS users in osusers.models.User.set_password - add URL pattern set_osuser_password in osusers.urls - implement osusers.views.SetOsUserPassword to set the password of an existing operating system user - link to hosting package detail view on user dashboard - add template hostingpackages/customerhostingpackage_detail.html - add template osusers/user_setpassword.html --- docs/changelog.rst | 2 + docs/code/osusers.rst | 13 +++++ gnuviechadmin/gnuviechadmin/urls.py | 1 + gnuviechadmin/hostingpackages/models.py | 7 +++ gnuviechadmin/osusers/models.py | 22 +++++++-- gnuviechadmin/osusers/urls.py | 16 +++++++ gnuviechadmin/osusers/views.py | 45 +++++++++++++++++ .../templates/dashboard/user_dashboard.html | 2 +- .../customerhostingpackage_detail.html | 48 +++++++++++++++++++ gnuviechadmin/templates/osusers/base.html | 1 + .../templates/osusers/user_setpassword.html | 30 ++++++++++++ 11 files changed, 181 insertions(+), 6 deletions(-) create mode 100644 gnuviechadmin/osusers/urls.py create mode 100644 gnuviechadmin/osusers/views.py create mode 100644 gnuviechadmin/templates/hostingpackages/customerhostingpackage_detail.html create mode 100644 gnuviechadmin/templates/osusers/base.html create mode 100644 gnuviechadmin/templates/osusers/user_setpassword.html diff --git a/docs/changelog.rst b/docs/changelog.rst index 99e4d05..d457f95 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,8 @@ Changelog ========= +* :feature:`-` add frontend functionality to set an os users' sftp password + (needs gvaldap >= 0.4.0 on the LDAP side) * :support:`-` remove unused dashboard.views.LogoutView and the corresponding URL in dashboard.urls * :feature:`-` add new task stub to set an ldap user's password diff --git a/docs/code/osusers.rst b/docs/code/osusers.rst index 79b7e05..3353239 100644 --- a/docs/code/osusers.rst +++ b/docs/code/osusers.rst @@ -30,3 +30,16 @@ .. automodule:: osusers.models :members: + + +:py:mod:`urls ` +----------------------------- + +.. automodule:: osusers.urls + + +:py:mod:`views ` +------------------------------- + +.. automodule:: osusers.views + :members: diff --git a/gnuviechadmin/gnuviechadmin/urls.py b/gnuviechadmin/gnuviechadmin/urls.py index 974ba1f..d3356b0 100644 --- a/gnuviechadmin/gnuviechadmin/urls.py +++ b/gnuviechadmin/gnuviechadmin/urls.py @@ -11,6 +11,7 @@ urlpatterns = patterns( url(r'', include('dashboard.urls')), url(r'^accounts/', include('allauth.urls')), url(r'^hosting/', include('hostingpackages.urls')), + url(r'^osuser/', include('osusers.urls')), url(r'^admin/', include(admin.site.urls)), ) diff --git a/gnuviechadmin/hostingpackages/models.py b/gnuviechadmin/hostingpackages/models.py index b665162..021eb95 100644 --- a/gnuviechadmin/hostingpackages/models.py +++ b/gnuviechadmin/hostingpackages/models.py @@ -5,6 +5,7 @@ This module contains the hosting package models. from __future__ import absolute_import, unicode_literals from django.conf import settings +from django.core.urlresolvers import reverse from django.db import transaction from django.db import models from django.utils.encoding import python_2_unicode_compatible @@ -218,6 +219,12 @@ class CustomerHostingPackage(HostingPackageBase): name=self.name, customer=self.customer ) + def get_absolute_url(self): + return reverse('hosting_package_details', kwargs={ + 'user': self.customer.username, + 'pk': self.id, + }) + def copy_template_attributes(self): """ Copy the attributes of the hosting package's template to the package. diff --git a/gnuviechadmin/osusers/models.py b/gnuviechadmin/osusers/models.py index c5c25a6..5f64a12 100644 --- a/gnuviechadmin/osusers/models.py +++ b/gnuviechadmin/osusers/models.py @@ -29,6 +29,7 @@ from ldaptasks.tasks import ( delete_ldap_group, delete_ldap_user, remove_ldap_user_from_group, + set_ldap_user_password, ) from fileservertasks.tasks import ( @@ -245,15 +246,26 @@ class User(TimeStampedModel, models.Model): """ if hasattr(self, 'shadow'): self.shadow.set_password(password) + success = set_ldap_user_password.delay( + self.username, password).get() + if success: + _LOGGER.info( + "successfully set LDAP password for %s", self.username) + else: + _LOGGER.error( + "setting the LDAP password for %s failed", self.username) + return success else: self.shadow = Shadow.objects.create_shadow( user=self, password=password ) - dn = create_ldap_user.delay( - self.username, self.uid, self.group.gid, self.gecos, - self.homedir, self.shell, password - ).get() - logging.info("set LDAP password for %s", dn) + dn = create_ldap_user.delay( + self.username, self.uid, self.group.gid, self.gecos, + self.homedir, self.shell, password + ).get() + _LOGGER.info("set LDAP password for %s", dn) + return True + @transaction.atomic def save(self, *args, **kwargs): diff --git a/gnuviechadmin/osusers/urls.py b/gnuviechadmin/osusers/urls.py new file mode 100644 index 0000000..7b62056 --- /dev/null +++ b/gnuviechadmin/osusers/urls.py @@ -0,0 +1,16 @@ +""" +This module defines the URL patterns for operating system user related views. + +""" +from __future__ import absolute_import, unicode_literals + +from django.conf.urls import patterns, url + +from .views import SetOsUserPassword + + +urlpatterns = patterns( + '', + url(r'^(?P[\w0-9@.+-_]+)/setpassword$', SetOsUserPassword.as_view(), + name='set_osuser_password'), +) diff --git a/gnuviechadmin/osusers/views.py b/gnuviechadmin/osusers/views.py new file mode 100644 index 0000000..a78067a --- /dev/null +++ b/gnuviechadmin/osusers/views.py @@ -0,0 +1,45 @@ +""" +This module defines the views for gnuviechadmin operating system user handling. + +""" +from __future__ import unicode_literals, absolute_import + +from django.shortcuts import redirect +from django.views.generic import UpdateView +from django.utils.translation import ugettext as _ +from django.contrib import messages + +from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin + +from .forms import ChangeOsUserPasswordForm +from .models import User + + +class SetOsUserPassword(StaffOrSelfLoginRequiredMixin, UpdateView): + """ + This view is used for setting a new operating system user password. + + """ + model = User + slug_field = 'username' + template_name_suffix = '_setpassword' + context_object_name = 'osuser' + form_class = ChangeOsUserPasswordForm + + def get_customer_object(self): + return self.get_object().customer + + def get_context_data(self, *args, **kwargs): + context = super(SetOsUserPassword, self).get_context_data( + *args, **kwargs) + context['customer'] = self.get_customer_object() + return context + + def form_valid(self, form): + osuser = form.save() + messages.success( + self.request, + _("New password for {username} has been set successfully.").format( + username=osuser.username + )) + return redirect(osuser.customerhostingpackage) diff --git a/gnuviechadmin/templates/dashboard/user_dashboard.html b/gnuviechadmin/templates/dashboard/user_dashboard.html index fc2509a..929a088 100644 --- a/gnuviechadmin/templates/dashboard/user_dashboard.html +++ b/gnuviechadmin/templates/dashboard/user_dashboard.html @@ -22,7 +22,7 @@ {% for package in hosting_packages %} - {{ package.name }} + {{ package.name }} {% with diskspace=package.get_disk_space %} {{ diskspace|filesizeformat }} diff --git a/gnuviechadmin/templates/hostingpackages/customerhostingpackage_detail.html b/gnuviechadmin/templates/hostingpackages/customerhostingpackage_detail.html new file mode 100644 index 0000000..daa06b5 --- /dev/null +++ b/gnuviechadmin/templates/hostingpackages/customerhostingpackage_detail.html @@ -0,0 +1,48 @@ +{% extends "hostingpackages/base.html" %} +{% load i18n %} + +{% block title %}{{ block.super }} - {% spaceless %} +{% if user == customer %} + {% blocktrans with package=hostingpackage.name %}Details for your Hosting Package {{ package }}{% endblocktrans %} +{% else %} + {% blocktrans with package=hostingpackage.name full_name=customer.get_full_name %}Details for Hosting Package {{ package }} of {{ full_name }}{% endblocktrans %} +{% endif %} +{% endspaceless %}{% endblock title %} + +{% block page_title %}{% blocktrans with package=hostingpackage.name %}Details of Hosting Package {{ package }}{% endblocktrans %}{% endblock page_title %} + +{% block content %} +
+
+
+
+ {% trans "Hosting Package Information" %}
+
+
+
{% trans "Name" %}
+
{{ hostingpackage.name }}
+
{% trans "Description" %}
+
{{ hostingpackage.description|default:"-" }}
+
{% trans "Disk space" %}
+ {% with diskspace=hostingpackage.get_disk_space %} +
{{ diskspace|filesizeformat }}
+ {% endwith %} +
{% trans "Mailboxes" %}
+
{% blocktrans with num=hostingpackage.get_used_mailboxes total=hostingpackage.get_mailboxes %}{{ num }} of {{ total }} in use{% endblocktrans %}
+
{% if hostingpackage.osuser.is_sftp_user %}{% trans "SFTP username" %}{% else %}{% trans "SSH/SFTP username" %}{% endif %}
+
{{ hostingpackage.osuser.username }}
+
{% trans "Upload server" %}
+
{{ uploadserver }}
+
+
+
+ +
+{% endblock content %} diff --git a/gnuviechadmin/templates/osusers/base.html b/gnuviechadmin/templates/osusers/base.html new file mode 100644 index 0000000..94d9808 --- /dev/null +++ b/gnuviechadmin/templates/osusers/base.html @@ -0,0 +1 @@ +{% extends "base.html" %} diff --git a/gnuviechadmin/templates/osusers/user_setpassword.html b/gnuviechadmin/templates/osusers/user_setpassword.html new file mode 100644 index 0000000..86fa844 --- /dev/null +++ b/gnuviechadmin/templates/osusers/user_setpassword.html @@ -0,0 +1,30 @@ +{% extends "osusers/base.html" %} +{% load i18n crispy_forms_tags %} +{% block title %}{{ block.super }} - {% spaceless %} +{% if customer == user %} + {% blocktrans with osuser=osuser.username %}Set new password for user {{ osuser }}{% endblocktrans %} +{% else %} + {% blocktrans with osuser=osuser.username full_name=customer.get_full_name %}Set new password for user {{ osuser }} of customer {{ full_name }}{% endblocktrans %} +{% endif %} +{% endspaceless %}{% endblock title %} + +{% block page_title %}{% spaceless %} +{% if customer == user %} + {% blocktrans with osuser=osuser.username %}Set new password for user {{ osuser }}{% endblocktrans %} +{% else %} + {% blocktrans with osuser=osuser.username full_name=customer.get_full_name %}Set new password for user {{ osuser }} of customer {{ full_name }}{% endblocktrans %} +{% endif %} +{% endspaceless %}{% endblock page_title %} + +{% block content %} +{% crispy form %} +{% endblock content %} + +{% block extra_js %} + +{% endblock extra_js %}