mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Admin Panel / People showed "Last active" as "-" for every user but the administrator: the people publication did not carry lastConnectionDate. USERS_DISABLE_INACTIVE_AFTER_DAYS disables, once a night, accounts that have not used WeKan for that many days, and revokes their logins. Admins are never disabled. An account with no recorded activity starts its count on the first run rather than from createdAt, and re-enabling an account restarts it. The setting is in Admin Panel / People / Login as well as in the environment, and the job resolves it every night, so a change there needs no restart. Every platform's settings file lists it. LDAP background sync looked a user up by id without LDAP_USER_SEARCH_FILTER, so an account the filter excludes - one disabled in Active Directory - could not log in but was still "found". It was never disabled, and a manual disable was undone on the next run. The filter is now ANDed into the lookup. An account disabled for inactivity is not re-enabled by the sync. From the patch by Raymond-Voogt, with the setting moved into the Admin Panel's login settings. Tests: tests/inactiveUsers.test.cjs, tests/ldapBackgroundSync.test.cjs, tests/ldapUserIdFilter.test.cjs, tests/authConfigCatalog.test.cjs and tests/playwright/specs/inactive-users-last-active.e2e.js. Thanks to Raymond-Voogt and xet7 ! Fixes #6783, Fixes #6784, Fixes #6785.
94 lines
3.8 KiB
JavaScript
94 lines
3.8 KiB
JavaScript
'use strict';
|
|
|
|
// Disable accounts nobody has used for a while (USERS_DISABLE_INACTIVE_AFTER_DAYS).
|
|
// Pure decisions only, so they are testable without a server or a database -
|
|
// see tests/inactiveUsers.test.cjs. The nightly job is server/disableInactiveUsers.js.
|
|
//
|
|
// "Last used" is the newest of:
|
|
// lastConnectionDate every login, refreshed by the open-session heartbeat
|
|
// (server/lastActiveOnLogin.js, client/lastActiveHeartbeat.js)
|
|
// inactivityClockStart when the clock (re)started: stamped by the job for an
|
|
// account it has never seen used, and on every re-enable
|
|
// (server/models/users.js), so an admin who re-enables a
|
|
// user does not see them disabled again the next night.
|
|
//
|
|
// createdAt is deliberately NOT a fallback. lastConnectionDate has only been
|
|
// recorded since the presence feature; every account older than that has none,
|
|
// and measuring those from createdAt would disable almost the whole instance on
|
|
// the first run. Instead the first run starts their clock, and they are judged
|
|
// N days later like everybody else.
|
|
|
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
|
|
// Why an account is disabled, so that one mechanism does not undo another:
|
|
// LDAP background sync re-enables users it finds in the directory, but not
|
|
// one disabled for inactivity (packages/wekan-ldap/server/presenceSync.js).
|
|
const REASON_INACTIVE = 'inactive';
|
|
const REASON_LDAP = 'ldap';
|
|
|
|
// Accepts a number or a string of digits; anything else, 0 or negative is off.
|
|
function normalizeInactiveDays(value) {
|
|
if (value === undefined || value === null) return 0;
|
|
const text = String(value).trim().replace(/^(['"])(.*)\1$/, '$2').trim();
|
|
if (!/^\d+$/.test(text)) return 0;
|
|
const days = Number(text);
|
|
return Number.isSafeInteger(days) && days > 0 ? days : 0;
|
|
}
|
|
|
|
function toTime(value) {
|
|
if (!value) return NaN;
|
|
const date = value instanceof Date ? value : new Date(value);
|
|
return date.getTime();
|
|
}
|
|
|
|
function lastUsedTime(user) {
|
|
const times = [toTime(user.lastConnectionDate), toTime(user.inactivityClockStart)]
|
|
.filter(t => !Number.isNaN(t));
|
|
return times.length ? Math.max(...times) : NaN;
|
|
}
|
|
|
|
/**
|
|
* What the nightly job should do with one account:
|
|
* 'disable' unused for at least `days` days
|
|
* 'start-clock' never seen used: start counting from `now`
|
|
* null leave it alone
|
|
*/
|
|
function inactivityAction(user, days, now = new Date()) {
|
|
if (!user || !days) return null;
|
|
if (user.loginDisabled === true) return null;
|
|
// Never lock the instance out: administrators are not disabled automatically.
|
|
if (user.isAdmin === true) return null;
|
|
// Anonymised accounts are already disabled; this is belt and braces. Import
|
|
// placeholders (server/lib/importPlaceholderUsers.js) are created disabled.
|
|
if (user.anonymized === true) return null;
|
|
const last = lastUsedTime(user);
|
|
if (Number.isNaN(last)) return 'start-clock';
|
|
const nowTime = toTime(now);
|
|
if (Number.isNaN(nowTime)) return null;
|
|
return nowTime - last >= days * DAY_MS ? 'disable' : null;
|
|
}
|
|
|
|
// Selector for the candidates, so the job reads only accounts that could
|
|
// possibly be disabled. inactivityAction() still decides per account.
|
|
function inactiveCandidateSelector(days, now = new Date()) {
|
|
const cutoff = new Date(toTime(now) - days * DAY_MS);
|
|
return {
|
|
loginDisabled: { $ne: true },
|
|
isAdmin: { $ne: true },
|
|
$and: [
|
|
{ $or: [{ lastConnectionDate: { $exists: false } }, { lastConnectionDate: null }, { lastConnectionDate: { $lte: cutoff } }] },
|
|
{ $or: [{ inactivityClockStart: { $exists: false } }, { inactivityClockStart: null }, { inactivityClockStart: { $lte: cutoff } }] },
|
|
],
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
DAY_MS,
|
|
REASON_INACTIVE,
|
|
REASON_LDAP,
|
|
normalizeInactiveDays,
|
|
lastUsedTime,
|
|
inactivityAction,
|
|
inactiveCandidateSelector,
|
|
};
|