Files
wekan/server/methods/lockedUsers.js
T
Lauri Ojansivu 6e208c6152 Admin Panel / People shows who is locked again, and why.
Moving the lockout counter to one per (user, source address) broke three readers
that were still looking at the flat field it replaced: the People table's lock
icon, its unlock click handler, and the lockedUsers methods behind them. Every
account would have shown as unlocked, and no admin could have unlocked one.

That is the "and nowhere else" half of the rule I had just written into
CLAUDE.md, missed on the very next fix. The negative test now fails if anything
reads the flat field again.

models/lib/accountLockout.js is the one place that knows the shape - locked or
not, since when, how many addresses and how many failures between them - so the
client helper, the click handler and the server methods cannot drift apart
again. Admin Panel / People / People answers WHY as well as whether: how many
addresses were locked out of the account and how many failures they made.
WHICH addresses is in Admin Panel -> Problems, where it is a tally rather than a
field on every user document.

Mongo cannot ask "which accounts are locked" over dynamically-named
subdocuments without an aggregation, so a DISPLAY field - lockedUntil - is
written beside the per-address state for that query alone. It is not the
counter: the decision stays per address, and a test fails if the decision or the
scope ever reads it, because a field that says "this ACCOUNT is locked" is the
vulnerability that was just fixed. Unlocking one address clears it only when no
other address is still locked.

Thanks to daniais and xet7 !
2026-08-16 15:05:00 +03:00

117 lines
3.3 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { ReactiveCache } from '/imports/reactiveCache';
const { lockSummary } = require('/models/lib/accountLockout');
// Method to find locked users and release them if needed
Meteor.methods({
async getLockedUsers() {
// Check if user has admin rights
const userId = this.userId;
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user');
}
const user = await ReactiveCache.getUser(userId);
if (!user || !user.isAdmin) {
throw new Meteor.Error('error-not-allowed', 'Not allowed');
}
// Current time to check against unlockTime
const currentTime = Number(new Date());
// Find users that are locked (known users)
const lockedUsers = await Meteor.users.find(
{
'services.accounts-lockout.lockedUntil': {
$gt: currentTime,
}
},
{
fields: {
_id: 1,
username: 1,
emails: 1,
'services.accounts-lockout': 1
}
}
).fetchAsync();
// Format the results for the UI
return lockedUsers.map(user => {
const email = user.emails && user.emails.length > 0 ? user.emails[0].address : 'No email';
const summary = lockSummary(user, currentTime);
const remainingLockTime = summary.secondsRemaining || 0;
return {
_id: user._id,
username: user.username || 'No username',
email,
failedAttempts: summary.failedAttempts || 0,
// WHY it is locked: how many addresses were locked out of this account
// and how many failures they made between them. Which addresses is in
// Admin Panel -> Problems, where it is a tally rather than a field on
// every user document.
lockedAddresses: summary.addresses || 0,
unlockTime: summary.unlockTime,
remainingLockTime // in seconds
};
});
},
async unlockUser(userId) {
check(userId, String);
// Check if user has admin rights
const adminId = this.userId;
if (!adminId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user');
}
const admin = await ReactiveCache.getUser(adminId);
if (!admin || !admin.isAdmin) {
throw new Meteor.Error('error-not-allowed', 'Not allowed');
}
// Make sure the user to unlock exists
const userToUnlock = await Meteor.users.findOneAsync(userId);
if (!userToUnlock) {
throw new Meteor.Error('error-user-not-found', 'User not found');
}
// Unlock the user
await Meteor.users.updateAsync(
{ _id: userId },
{
$unset: {
'services.accounts-lockout': 1
}
}
);
return true;
},
async unlockAllUsers() {
// Check if user has admin rights
const adminId = this.userId;
if (!adminId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user');
}
const admin = await ReactiveCache.getUser(adminId);
if (!admin || !admin.isAdmin) {
throw new Meteor.Error('error-not-allowed', 'Not allowed');
}
// Unlock all users
await Meteor.users.updateAsync(
{ 'services.accounts-lockout.lockedUntil': { $exists: true } },
{
$unset: {
'services.accounts-lockout': 1
}
},
{ multi: true }
);
return true;
}
});