// 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, anonymizeDueSelector, undatedInactiveSelector, } = 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, loginDisabledAt: now } }, ); 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 }; } // USERS_ANONYMIZE_INACTIVE_AFTER_DAYS (models/lib/inactiveUsers.js // anonymizeDueSelector): the accounts disabled for inactivity that many days // ago are anonymised, as Member Settings / Anonymize does - the person's data // elsewhere first (server/lib/userErasureRun.js), then the account itself. export async function anonymizeInactiveUsers(days, now = new Date()) { if (!days) return { anonymized: 0, dated: 0 }; const dated = await Meteor.users.updateAsync(undatedInactiveSelector(), { $set: { loginDisabledAt: now } }, { multi: true }); const due = await Meteor.users.find(anonymizeDueSelector(days, now), { fields: { _id: 1, username: 1, emails: 1 } }).fetchAsync(); const { runUserErasure } = require('/server/lib/userErasureRun'); const { buildAnonymizeUpdate } = require('/models/lib/userAnonymization'); const { Random } = require('meteor/random'); let anonymized = 0; for (const user of due) { try { // eslint-disable-next-line no-await-in-loop await runUserErasure(await Meteor.users.findOneAsync(user._id), 'anonymize'); // Re-check in the write: an account enabled meanwhile is left alone. // eslint-disable-next-line no-await-in-loop anonymized += await Meteor.users.updateAsync({ _id: user._id, ...anonymizeDueSelector(days, now) }, buildAnonymizeUpdate(`deleted-user-${Random.id(8).toLowerCase()}`)); // eslint-disable-next-line no-console console.log(`anonymizeInactiveUsers: anonymised ${user._id} (disabled ${days} days ago)`); } catch (e) { // eslint-disable-next-line no-console console.error('anonymizeInactiveUsers: could not anonymise', user._id, e.message); } } return { anonymized, dated }; } function anonymizeDaysSetting() { return normalizeInactiveDays(process.env.USERS_ANONYMIZE_INACTIVE_AFTER_DAYS); } // 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; const now = new Date(); const result = await disableInactiveUsers(inactiveDaysSetting(), now); return { ...result, ...(await anonymizeInactiveUsers(anonymizeDaysSetting(), now)) }; }, }); } catch (e) { // eslint-disable-next-line no-console console.error('disableInactiveUsers: failed to register cron job', e); } });