mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 11:16:47 +00:00
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.
26 lines
943 B
Python
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)
|