Files
wekan/server/disableInactiveUsers.js
T
Lauri Ojansivu b933a69cd4 People shows everyone's Last active; inactive users can be disabled; LDAP sync honours its search filter.
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.
2026-10-19 17:35:51 +03:00

94 lines
3.8 KiB
JavaScript

// USERS_DISABLE_INACTIVE_AFTER_DAYS: once a day, disable accounts that have not
// used WeKan for that many days. Off unless set. The decisions are in
// models/lib/inactiveUsers.js (pure, tested); this file only reads and writes.
//
// Disabling goes through the same Users.before.update hook as an admin
// disabling a user by hand (server/models/users.js), so the account's login
// tokens are revoked and open sessions closed. Re-enabling it - by an admin in
// Admin Panel > People, or the REST API - restarts its inactivity clock there.
import { Meteor } from 'meteor/meteor';
import { SyncedCron } from '/server/cron/syncedCron';
import { authEnv, authConfigReady } from '/server/lib/authConfig';
const {
REASON_INACTIVE, normalizeInactiveDays, inactivityAction, inactiveCandidateSelector,
} = require('/models/lib/inactiveUsers');
export async function disableInactiveUsers(days, now = new Date()) {
if (!days) return { disabled: 0, clocksStarted: 0 };
const candidates = await Meteor.users.find(inactiveCandidateSelector(days, now), {
fields: {
_id: 1, username: 1, isAdmin: 1, loginDisabled: 1, anonymized: 1,
lastConnectionDate: 1, inactivityClockStart: 1,
},
}).fetchAsync();
let disabled = 0;
let clocksStarted = 0;
for (const user of candidates) {
const action = inactivityAction(user, days, now);
try {
if (action === 'start-clock') {
// eslint-disable-next-line no-await-in-loop
clocksStarted += await Meteor.users.updateAsync(
{ _id: user._id, lastConnectionDate: { $in: [null] }, inactivityClockStart: { $in: [null] } },
{ $set: { inactivityClockStart: now } },
);
} else if (action === 'disable') {
// Re-check the condition in the write itself: a user who logs in while
// the job runs must not be disabled on stale data.
// eslint-disable-next-line no-await-in-loop
const n = await Meteor.users.updateAsync(
{ _id: user._id, ...inactiveCandidateSelector(days, now) },
{ $set: { loginDisabled: true, loginDisabledReason: REASON_INACTIVE } },
);
if (n) {
disabled += n;
// eslint-disable-next-line no-console
console.log(`disableInactiveUsers: disabled ${user.username} (no activity for ${days} days)`);
}
}
} catch (e) {
// One account that cannot be written must not stop the rest.
// eslint-disable-next-line no-console
console.error('disableInactiveUsers: could not update user', user._id, e.message);
}
}
return { disabled, clocksStarted };
}
// The setting is resolved on every run, Admin Panel / People / Login first and
// the environment variable second (models/lib/authConfigCatalog.js), so
// switching it on, off or to another number there needs no restart.
let lastWarned = null;
export function inactiveDaysSetting(env = authEnv) {
const raw = env('USERS_DISABLE_INACTIVE_AFTER_DAYS');
const days = normalizeInactiveDays(raw);
if (!days && raw !== undefined && raw !== null && String(raw).trim() !== '' && String(raw).trim() !== '0') {
if (lastWarned !== raw) {
// eslint-disable-next-line no-console
console.warn(`USERS_DISABLE_INACTIVE_AFTER_DAYS ${JSON.stringify(raw)} is not a positive whole number of days; inactive users are not disabled`);
lastWarned = raw;
}
}
return days;
}
Meteor.startup(() => {
try {
SyncedCron.add({
name: 'wekan-disable-inactive-users',
schedule(parser) {
return parser.text('at 3:15 am');
},
async job() {
await authConfigReady;
return disableInactiveUsers(inactiveDaysSetting(), new Date());
},
});
} catch (e) {
// eslint-disable-next-line no-console
console.error('disableInactiveUsers: failed to register cron job', e);
}
});