add password change view

This commit is contained in:
Jens Langhammer 2019-02-23 20:56:41 +01:00
parent e5a405bf43
commit 292fbecca0
7 changed files with 53 additions and 21 deletions

View File

@ -1,6 +1,8 @@
"""passbook core user forms""" """passbook core user forms"""
from django import forms from django import forms
from django.forms import ValidationError
from django.utils.translation import gettext_lazy as _
from passbook.core.models import User from passbook.core.models import User
@ -12,3 +14,22 @@ class UserDetailForm(forms.ModelForm):
model = User model = User
fields = ['username', 'first_name', 'last_name', 'email'] fields = ['username', 'first_name', 'last_name', 'email']
class PasswordChangeForm(forms.Form):
"""Form to update password"""
password = forms.CharField(label=_('Password'),
widget=forms.PasswordInput(attrs={'placeholder': _('New Password')}))
password_repeat = forms.CharField(label=_('Repeat Password'),
widget=forms.PasswordInput(attrs={
'placeholder': _('Repeat Password')
}))
def clean_password_repeat(self):
"""Check if Password adheres to filter and if passwords matche"""
password = self.cleaned_data.get('password')
password_repeat = self.cleaned_data.get('password_repeat')
if password != password_repeat:
raise ValidationError(_("Passwords don't match"))
# TODO: Password policy check
return self.cleaned_data.get('password_repeat')

View File

@ -40,6 +40,9 @@
<li> <li>
<a href="{% url 'passbook_core:user-settings' %}">{% trans 'User Settings' %}</a> <a href="{% url 'passbook_core:user-settings' %}">{% trans 'User Settings' %}</a>
</li> </li>
<li>
<a href="{% url 'passbook_core:user-change-password' %}">{% trans 'Change Password' %}</a>
</li>
<li class="divider"></li> <li class="divider"></li>
<li> <li>
<a href="{% url 'passbook_core:auth-logout' %}">{% trans 'Logout' %}</a> <a href="{% url 'passbook_core:auth-logout' %}">{% trans 'Logout' %}</a>

View File

@ -14,8 +14,8 @@
<i class="fa fa-desktop"></i> {% trans 'Details' %} <i class="fa fa-desktop"></i> {% trans 'Details' %}
</a> </a>
</li> </li>
<li class="{% is_active 'passbook_core:user-settings' %}"> <li class="{% is_active 'passbook_core:user-change-password' %}">
<a href="{% url 'passbook_core:user-settings' %}"> <a href="{% url 'passbook_core:user-change-password' %}">
<i class="pficon pficon-locked"></i> {% trans 'Change Password' %} <i class="pficon pficon-locked"></i> {% trans 'Change Password' %}
</a> </a>
</li> </li>

View File

@ -1,14 +0,0 @@
{% extends "user/base.html" %}
{% load i18n %}
{% block page %}
<h1>{% trans 'Change Password' %}</h1>
<form action="" method="post" class="form-horizontal">
{% csrf_token %}
{% include 'partials/form.html' %}
<input class="btn btn-primary" type="submit" value="{% trans 'Update' %}">
<a class="btn btn-danger"
href="{% url 'passbook_core:user-delete' %}?back={{ request.get_full_path }}">{% trans 'Delete user' %}</a>
</form>
{% endblock %}

View File

@ -25,6 +25,8 @@ core_urls = [
# User views # User views
path('user/', user.UserSettingsView.as_view(), name='user-settings'), path('user/', user.UserSettingsView.as_view(), name='user-settings'),
path('user/delete/', user.UserDeleteView.as_view(), name='user-delete'), path('user/delete/', user.UserDeleteView.as_view(), name='user-delete'),
path('user/change_password/', user.UserChangePasswordView.as_view(),
name='user-change-password'),
# Overview # Overview
path('', overview.OverviewView.as_view(), name='overview'), path('', overview.OverviewView.as_view(), name='overview'),
] ]

View File

@ -13,7 +13,6 @@ class OverviewView(LoginRequiredMixin, TemplateView):
template_name = 'overview/index.html' template_name = 'overview/index.html'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
print(self.request.session.keys())
kwargs['applications'] = self.request.user.applications.all() kwargs['applications'] = self.request.user.applications.all()
if self.request.user.is_superuser: if self.request.user.is_superuser:
kwargs['applications'] = Application.objects.all() kwargs['applications'] = Application.objects.all()

View File

@ -1,11 +1,12 @@
"""passbook core user views""" """passbook core user views"""
from django.contrib import messages from django.contrib import messages
from django.contrib.auth import logout from django.contrib.auth import logout, update_session_auth_hash
from django.urls import reverse from django.shortcuts import redirect, reverse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.views.generic import DeleteView, UpdateView from django.views.generic import DeleteView, FormView, UpdateView
from passbook.core.forms.users import UserDetailForm from passbook.core.forms.users import PasswordChangeForm, UserDetailForm
from passbook.lib.config import CONFIG
class UserSettingsView(UpdateView): class UserSettingsView(UpdateView):
@ -28,3 +29,23 @@ class UserDeleteView(DeleteView):
messages.success(self.request, _('Successfully deleted user.')) messages.success(self.request, _('Successfully deleted user.'))
logout(self.request) logout(self.request)
return reverse('passbook_core:auth-login') return reverse('passbook_core:auth-login')
class UserChangePasswordView(FormView):
"""View for users to update their password"""
form_class = PasswordChangeForm
template_name = 'login/form_with_user.html'
def form_valid(self, form: PasswordChangeForm):
self.request.user.set_password(form.cleaned_data.get('password'))
self.request.user.save()
update_session_auth_hash(self.request, self.request.user)
messages.success(self.request, _('Successfully changed password'))
return redirect('passbook_core:overview')
def get_context_data(self, **kwargs):
kwargs['config'] = CONFIG.get('passbook')
kwargs['is_login'] = True
kwargs['title'] = _('Change Password')
kwargs['primary_action'] = _('Change')
return super().get_context_data(**kwargs)