diff --git a/passbook/core/forms/users.py b/passbook/core/forms/users.py
index ae57f206d..ad437d149 100644
--- a/passbook/core/forms/users.py
+++ b/passbook/core/forms/users.py
@@ -1,6 +1,8 @@
"""passbook core user forms"""
from django import forms
+from django.forms import ValidationError
+from django.utils.translation import gettext_lazy as _
from passbook.core.models import User
@@ -12,3 +14,22 @@ class UserDetailForm(forms.ModelForm):
model = User
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')
diff --git a/passbook/core/templates/overview/base.html b/passbook/core/templates/overview/base.html
index 70dc116a0..f4c7366ae 100644
--- a/passbook/core/templates/overview/base.html
+++ b/passbook/core/templates/overview/base.html
@@ -40,6 +40,9 @@
{% trans 'User Settings' %}
+
+ {% trans 'Change Password' %}
+
{% trans 'Logout' %}
diff --git a/passbook/core/templates/user/base.html b/passbook/core/templates/user/base.html
index f62f99432..5195c50f9 100644
--- a/passbook/core/templates/user/base.html
+++ b/passbook/core/templates/user/base.html
@@ -14,8 +14,8 @@
{% trans 'Details' %}
-
-
+
+
{% trans 'Change Password' %}
diff --git a/passbook/core/templates/user/change_password.html b/passbook/core/templates/user/change_password.html
deleted file mode 100644
index 5be49f559..000000000
--- a/passbook/core/templates/user/change_password.html
+++ /dev/null
@@ -1,14 +0,0 @@
-{% extends "user/base.html" %}
-
-{% load i18n %}
-
-{% block page %}
-{% trans 'Change Password' %}
-
-{% endblock %}
diff --git a/passbook/core/urls.py b/passbook/core/urls.py
index 6b2092e46..ff73bde90 100644
--- a/passbook/core/urls.py
+++ b/passbook/core/urls.py
@@ -25,6 +25,8 @@ core_urls = [
# User views
path('user/', user.UserSettingsView.as_view(), name='user-settings'),
path('user/delete/', user.UserDeleteView.as_view(), name='user-delete'),
+ path('user/change_password/', user.UserChangePasswordView.as_view(),
+ name='user-change-password'),
# Overview
path('', overview.OverviewView.as_view(), name='overview'),
]
diff --git a/passbook/core/views/overview.py b/passbook/core/views/overview.py
index a5436510d..07b55ace8 100644
--- a/passbook/core/views/overview.py
+++ b/passbook/core/views/overview.py
@@ -13,7 +13,6 @@ class OverviewView(LoginRequiredMixin, TemplateView):
template_name = 'overview/index.html'
def get_context_data(self, **kwargs):
- print(self.request.session.keys())
kwargs['applications'] = self.request.user.applications.all()
if self.request.user.is_superuser:
kwargs['applications'] = Application.objects.all()
diff --git a/passbook/core/views/user.py b/passbook/core/views/user.py
index d051f7a05..fafcd3158 100644
--- a/passbook/core/views/user.py
+++ b/passbook/core/views/user.py
@@ -1,11 +1,12 @@
"""passbook core user views"""
from django.contrib import messages
-from django.contrib.auth import logout
-from django.urls import reverse
+from django.contrib.auth import logout, update_session_auth_hash
+from django.shortcuts import redirect, reverse
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):
@@ -28,3 +29,23 @@ class UserDeleteView(DeleteView):
messages.success(self.request, _('Successfully deleted user.'))
logout(self.request)
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)