1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/accounts/middleware.py
catborise 24896aaecd feat(admin): a password an administrator sets can be one the user must change
The administrator's password form has "The user must change it at their
next login". With it, the user is sent to the change form until they
choose their own, as the generated admin password already does.
2026-10-25 22:38:40 +03:00

26 lines
943 B
Python

from django.shortcuts import redirect
from django.urls import Resolver404, resolve
from accounts.models import UserAttributes
ALLOWED_VIEWS = {"accounts:change_password", "accounts:logout", "accounts:login"}
class ForcePasswordChangeMiddleware:
"""Send users who must change their password (the generated admin one, or one
an administrator set) to the change form."""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if request.user.is_authenticated and UserAttributes.objects.filter(
user=request.user, must_change_password=True
).exists():
try:
view_name = resolve(request.path_info).view_name
except Resolver404:
view_name = None
if view_name not in ALLOWED_VIEWS:
return redirect("accounts:change_password")
return self.get_response(request)