1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 09:26:03 +00:00
webvirtcloud/accounts/checks.py
catborise 7359fbb147 fix(accounts): a page whose session ended says so; a form sent then is not lost in a 405
A session that ended while a page was open went unnoticed: the VM list,
the VM page and the job panel kept asking, were sent to the sign-in
page, and showed what they had as if it were current. A button pressed
then sent the user to sign in and, after it, to the form's own address
by GET, where its view answered an empty 405 and the action was lost.

The panel's LoginRequiredMiddleware (webvirtcloud.middleware) answers a
page's script 401, upon which session_end.js says the user was signed
out, with a link to sign in and come back, and the page stops asking
and reloading. A form sent after the end (or with a token a sign-in
elsewhere replaced) goes back, after the sign-in, to the page it was on,
and the sign-in page says it was not done. accounts.E002 asks for this
middleware; Django's own is a warning (W002), so an update keeps a
panel whose local settings name it running.
2026-11-21 23:48:34 +03:00

64 lines
2.6 KiB
Python

from django.conf import settings
from django.core.checks import Error, Tags, Warning, register
@register(Tags.security)
def password_change_middleware_check(app_configs, **kwargs):
middleware = list(settings.MIDDLEWARE)
required = "accounts.middleware.ForcePasswordChangeMiddleware"
authentication = "django.contrib.auth.middleware.AuthenticationMiddleware"
if required not in middleware or (
authentication in middleware
and middleware.index(required) < middleware.index(authentication)
):
return [
Error(
"ForcePasswordChangeMiddleware must follow AuthenticationMiddleware.",
hint=f"data/local_settings.py sets MIDDLEWARE: keep '{required}' after AuthenticationMiddleware, as webvirtcloud/settings.py has it.",
id="accounts.E001",
)
]
return []
@register(Tags.security)
def login_required_middleware_check(app_configs, **kwargs):
middleware = list(settings.MIDDLEWARE)
required = "webvirtcloud.middleware.LoginRequiredMiddleware"
django_s = "django.contrib.auth.middleware.LoginRequiredMiddleware"
old = "login_required.middleware.LoginRequiredMiddleware"
if django_s in middleware and old not in middleware:
# it signs in as well; a page whose session ended does not say so, and a form sent then is lost
return [
Warning(
"MIDDLEWARE uses Django's LoginRequiredMiddleware, not the panel's.",
hint=f"data/local_settings.py sets MIDDLEWARE: replace '{django_s}' with '{required}', "
"as webvirtcloud/settings.py has it.",
id="accounts.W002",
)
]
if old in middleware or required not in middleware:
return [
Error(
"MIDDLEWARE must use the panel's LoginRequiredMiddleware.",
hint=f"data/local_settings.py sets MIDDLEWARE: replace '{old}' with '{required}' "
"and delete LOGIN_REQUIRED_IGNORE_VIEW_NAMES, as webvirtcloud/settings.py has it.",
id="accounts.E002",
)
]
return []
@register()
def obsolete_settings_check(app_configs, **kwargs):
if hasattr(settings, "SHOW_PROFILE_EDIT_PASSWORD"):
return [
Warning(
"SHOW_PROFILE_EDIT_PASSWORD is no longer used.",
hint="Remove it from data/local_settings.py. Whether a user may change their own "
"password is the 'Can change password' permission, per user or group.",
id="accounts.W001",
)
]
return []