implement database user password change

- implement userdbs.forms.ChangeDatabaseUserPasswordForm
- implement userdbs.views.ChangeDatabaseUserPassword
- add URL pattern 'change_dbuser_password' to userdbs.urls
- add template userdbs/databaseuser_setpassword.html
- link from hostingpackage detail template to 'change_dbuser_password'
- add changelog entry
This commit is contained in:
Jan Dittberner 2015-01-26 12:39:42 +01:00
parent 486c07d27d
commit fd6449dff1
6 changed files with 100 additions and 1 deletions

View file

@ -1,6 +1,7 @@
Changelog
=========
* :feature:`-` implement password changes for database users
* :feature:`-` implement setup of user databases
* :support:`-` performance improvement for hosting package detail view
* :support:`-` move HostingPackageAndCustomerMixin from managemails.views to

View file

@ -177,7 +177,7 @@
<td>{% include "userdbs/snippets/db_type.html" with db_type=database.db_user.db_type %}</td>
<td>{{ database.db_name }}</td>
<td>{{ database.db_user.name }}</td>
<td></td>
<td><a href="{% url 'change_dbuser_password' package=hostingpackage.id slug=database.db_user.name %}" title="{% trans "Set database user password" %}"><i class="fa fa-user-secret"></i><span class="sr-only"> {% trans "Set database user password" %}</span></a></td>
</tr>
{% endfor %}
</tbody>

View file

@ -0,0 +1,31 @@
{% extends "userdbs/base.html" %}
{% load i18n crispy_forms_tags %}
{% block title %}{{ block.super }} - {% spaceless %}
{% if customer == user %}
{% blocktrans with dbuser=dbuser.name %}Set Database User Password for {{ dbuser }}{% endblocktrans %}
{% else %}
{% blocktrans with dbuser=dbuser.name full_name=customer.get_full_name %}Set Database User Password for {{ dbuser }} of Customer {{ full_name }}{% endblocktrans %}
{% endif %}
{% endspaceless %}{% endblock title %}
{% block page_title %}{% spaceless %}
{% if customer == user %}
{% blocktrans with dbuser=dbuser.name %}Set Database User Password <small>for {{ dbuser }}</small>{% endblocktrans %}
{% else %}
{% blocktrans with dbuser=dbuser.name full_name=customer.get_full_name %}Set Database User Password <small>for {{ dbuser }} of Customer {{ full_name }}</small>{% endblocktrans %}
{% endif %}
{% endspaceless %}{% endblock page_title %}
{% block content %}
<p>{% if customer == user %}{% trans "Please specify the new password for your database user." %}{% else %}{% trans "Please specify the new password of the database user." %}{% endif %}
{% crispy form %}
{% endblock content %}
{% block extra_js %}
<script type="text/javascript">
$(document).ready(function() {
$('input[type=password]').val('').first().focus();
});
</script>
{% endblock extra_js %}

View file

@ -15,6 +15,7 @@ from crispy_forms.layout import (
from .models import (
DB_TYPES,
DatabaseUser,
UserDatabase,
)
from gvawebcore.forms import PasswordModelFormMixin
@ -64,3 +65,28 @@ class AddUserDatabaseForm(forms.ModelForm, PasswordModelFormMixin):
data['db_type'], self.hosting_package.osuser,
password=data['password1'], commit=commit)
return super(AddUserDatabaseForm, self).save(commit)
class ChangeDatabaseUserPasswordForm(forms.ModelForm, PasswordModelFormMixin):
"""
This form is used to change the password of a database user.
"""
class Meta:
model = DatabaseUser
fields = []
def __init__(self, *args, **kwargs):
self.hosting_package = kwargs.pop('hostingpackage')
super(ChangeDatabaseUserPasswordForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_action = reverse(
'change_dbuser_password', kwargs={
'slug': self.instance.name,
'package': self.hosting_package.id,
})
self.helper.add_input(Submit('submit', _('Set password')))
def save(self, commit=True):
self.instance.set_password(self.cleaned_data['password1'])
return super(ChangeDatabaseUserPasswordForm, self).save()

View file

@ -8,10 +8,13 @@ from django.conf.urls import patterns, url
from .views import (
AddUserDatabase,
ChangeDatabaseUserPassword,
)
urlpatterns = patterns(
'',
url(r'^(?P<package>\d+)/create$',
AddUserDatabase.as_view(), name='add_userdatabase'),
url(r'^(?P<package>\d+)/(?P<slug>[\w0-9]+)/setpassword',
ChangeDatabaseUserPassword.as_view(), name='change_dbuser_password'),
)

View file

@ -8,6 +8,7 @@ from django.shortcuts import redirect
from django.utils.translation import ugettext as _
from django.views.generic.edit import (
CreateView,
UpdateView,
)
from django.contrib import messages
@ -16,9 +17,11 @@ from gvawebcore.views import HostingPackageAndCustomerMixin
from .forms import (
AddUserDatabaseForm,
ChangeDatabaseUserPasswordForm,
)
from .models import (
DB_TYPES,
DatabaseUser,
UserDatabase,
)
@ -62,3 +65,38 @@ class AddUserDatabase(
dbname=userdatabase.db_name, db_user=userdatabase.db_user)
)
return redirect(self.get_hosting_package())
class ChangeDatabaseUserPassword(
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, UpdateView
):
"""
This view is used to change a database user's password.
"""
model = DatabaseUser
slug_field = 'name'
context_object_name = 'dbuser'
template_name_suffix = '_setpassword'
form_class = ChangeDatabaseUserPasswordForm
def get_form_kwargs(self):
kwargs = super(ChangeDatabaseUserPassword, self).get_form_kwargs()
kwargs['hostingpackage'] = self.get_hosting_package()
return kwargs
def get_context_data(self, **kwargs):
context = super(ChangeDatabaseUserPassword, self).get_context_data(
**kwargs)
context['hostingpackage'] = self.get_hosting_package()
context['customer'] = self.get_customer_object()
return context
def form_valid(self, form):
db_user = form.save()
messages.success(
self.request,
_('Successfully changed password of database user {dbuser}'
).format(dbuser=db_user.name)
)
return redirect(self.get_hosting_package())