This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/core/views/user.py

72 lines
2.6 KiB
Python
Raw Normal View History

"""passbook core user views"""
from django.contrib import messages
2019-02-23 19:56:41 +00:00
from django.contrib.auth import logout, update_session_auth_hash
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.forms.utils import ErrorList
2019-02-23 19:56:41 +00:00
from django.shortcuts import redirect, reverse
from django.urls import reverse_lazy
from django.utils.translation import gettext as _
2019-02-23 19:56:41 +00:00
from django.views.generic import DeleteView, FormView, UpdateView
2019-02-23 19:56:41 +00:00
from passbook.core.forms.users import PasswordChangeForm, UserDetailForm
from passbook.lib.config import CONFIG
class UserSettingsView(SuccessMessageMixin, LoginRequiredMixin, UpdateView):
"""Update User settings"""
2019-02-26 09:57:05 +00:00
2019-12-31 11:51:16 +00:00
template_name = "user/settings.html"
form_class = UserDetailForm
2019-12-31 11:51:16 +00:00
success_message = _("Successfully updated user.")
success_url = reverse_lazy("passbook_core:user-settings")
def get_object(self):
return self.request.user
class UserDeleteView(LoginRequiredMixin, DeleteView):
"""Delete user account"""
2019-12-31 11:51:16 +00:00
template_name = "generic/delete.html"
def get_object(self):
return self.request.user
def get_success_url(self):
2019-12-31 11:51:16 +00:00
messages.success(self.request, _("Successfully deleted user."))
logout(self.request)
2019-12-31 11:51:16 +00:00
return reverse("passbook_core:auth-login")
2019-02-23 19:56:41 +00:00
class UserChangePasswordView(LoginRequiredMixin, FormView):
2019-02-23 19:56:41 +00:00
"""View for users to update their password"""
form_class = PasswordChangeForm
2019-12-31 11:51:16 +00:00
template_name = "login/form_with_user.html"
2019-02-23 19:56:41 +00:00
def form_valid(self, form: PasswordChangeForm):
# TODO: Rewrite to flow
try:
# user.set_password checks against Policies so we don't need to manually do it here
2019-12-31 11:51:16 +00:00
self.request.user.set_password(form.cleaned_data.get("password"))
self.request.user.save()
update_session_auth_hash(self.request, self.request.user)
2019-12-31 11:51:16 +00:00
messages.success(self.request, _("Successfully changed password"))
except ValueError:
# Manually inject error into form
# pylint: disable=protected-access
2019-12-31 11:51:16 +00:00
errors = form._errors.setdefault("password_repeat", ErrorList(""))
# pylint: disable=protected-access
errors = form._errors.setdefault("password", ErrorList())
errors.append("foo")
return self.form_invalid(form)
2019-12-31 11:51:16 +00:00
return redirect("passbook_core:overview")
2019-02-23 19:56:41 +00:00
def get_context_data(self, **kwargs):
2019-12-31 11:51:16 +00:00
kwargs["config"] = CONFIG.y("passbook")
kwargs["title"] = _("Change Password")
kwargs["primary_action"] = _("Change")
2019-02-23 19:56:41 +00:00
return super().get_context_data(**kwargs)