1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

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.
This commit is contained in:
catborise 2026-11-21 23:30:47 +03:00
parent 2373d73b37
commit 7359fbb147
17 changed files with 189 additions and 15 deletions

View file

@ -105,10 +105,8 @@
button.disabled = true;
var url = text.url + "?" + new URLSearchParams({q: form.elements.q.value});
fetch(url, {headers: {"X-Requested-With": "XMLHttpRequest"}}).then(function (response) {
if (response.redirected) {
// signed out meanwhile: reloading goes to the login form, then back here
location.reload();
return new Promise(function () {});
if (response.status === 401) {
return {error: sessionEnd()}; // signed out meanwhile (session_end.js)
}
return response.json();
}).catch(function () {

View file

@ -14,8 +14,8 @@ function pollWhileVisible(interval, request, onData, firstDelay) {
function run() {
timer = null;
if (stopped || document.hidden) {
return;
if (stopped || document.hidden || window.sessionEnded) {
return; // a page whose session ended asks nothing more (session_end.js)
}
running = true;
request()
@ -58,5 +58,6 @@ function pageInUse() {
|| !!(active && active.matches && active.matches("input, select, textarea"))
|| !!(window.xmlEditUnsaved && window.xmlEditUnsaved())
|| (typeof bulkPowerChosen === "function" && bulkPowerChosen()) // the VM list's bulk form in use (bulk_power.js)
|| !!document.querySelector("button[data-busy]"); // a form being sent (submit_busy.js)
|| !!document.querySelector("button[data-busy]") // a form being sent (submit_busy.js)
|| !!window.sessionEnded; // signed out: the page keeps its notice (session_end.js)
}

25
static/js/session_end.js Normal file
View file

@ -0,0 +1,25 @@
// A session that ends while the page is open: a script's request is answered 401
// (webvirtcloud.middleware.LoginRequiredMiddleware). sessionEnd() says so, with a link to sign
// in and come back here, and the page stops asking and reloading (poll.js, pageInUse): what it
// shows is no longer updated. A jQuery request's 401 calls it; a fetch's caller does. Gives
// the words it shows.
function sessionEnd() {
var notice = document.getElementById("session-ended");
if (notice && !window.sessionEnded) {
window.sessionEnded = true;
var link = document.createElement("a");
link.className = "alert-link";
link.href = notice.dataset.href;
link.textContent = notice.dataset.link;
notice.className = "alert alert-warning";
notice.setAttribute("role", "alert");
notice.replaceChildren(notice.dataset.text + " ", link);
notice.hidden = false;
}
return notice ? notice.dataset.text : "";
}
$(document).ajaxError(function (event, xhr) {
if (xhr.status === 401) {
sessionEnd();
}
});

View file

@ -44,6 +44,9 @@
return fetch(form.action, {
method: "POST", body: data, redirect: "manual", headers: {"X-Requested-With": "XMLHttpRequest"},
}).then(function (response) {
if (response.status === 401) {
return {error: sessionEnd()}; // signed out meanwhile (session_end.js): the edit stays
}
return response.json();
}).then(function (answer) {
return {diff: answer.diff, saved: answer.saved, error: answer.error || answer.detail};