Files
wekan/server/methods/loginOffices.js
T
Lauri Ojansivu ea95943687 CSV exports for access reviews and incidents, and one's own sign-in history.
docs/Compliance/README.md WP-10 (rows 3.8, 4.3 a and 5.6): Export CSV on
Admin Panel / People, Problems / Boards, Problems / Security and the Audit
log gives the whole list, not the page on screen, and People and Boards
also give Boards and roles as CSV - one row per board member with the
role, for every board, the access review itself.
server/routes/adminCsvExport.js serves them to a site administrator only,
streamed from a cursor, and records every download - a refused one too -
in the Audit log as exportAdminCsv. Each dataset
(models/lib/adminCsvExport.js) reads only the fields it names, so no
password, token, two-factor secret or key can reach a file, and a cell a
spreadsheet would run as a formula is written as text.

Member Settings / Privacy and my data lists Where I signed in from: each
address of the account, how often, first and last time
(getMyLoginHistory, one's own only).

Three new strings, translated in every language. Low confidence: ace, ak,
ay, bi, bm, bo, bua, chr, csb, cv, dz, ee, ff, fj, fo, fur, gn, gv, haw, ba,
bho, ary, co, an, ast-ES, iu, kl, kw, nah, lld, rup, nap, scn, sc, szl, ks,
kok, mai, sah, se, om, qu, rm, pap, nd, ss, nso, sm, sn, rn, lg, ny, mg,
tig, tlh, to, tpi, ts, tn, uz-AR, ve-CC, ve-PP, ve, vo, wa-RR, wa, wal, wo,
zgh, tk_TM, ug, tt. The wrong-language values the translators saw are
listed in TODO.md.

Thanks to xet7 !
2026-11-24 03:44:59 +02:00

270 lines
11 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import LoginAddresses from '/models/loginAddresses';
import * as tenantAdmin from '/models/lib/tenantAdmin';
const {
officeSummary, tallyList, loginLocationsByCountry,
} = require('/models/lib/loginTally');
// The legacy address-grouped endpoint still uses this for loginOffice().
async function withIdentities(summary) {
if (!summary || !summary.users.length) return summary;
const names = summary.users.map(u => u.value);
const docs = await Meteor.users.find(
{ username: { $in: names } },
{ fields: { username: 1, 'profile.initials': 1, 'profile.fullname': 1,
'profile.avatarUrl': 1, loginDisabled: 1 } },
).fetchAsync();
const byName = new Map(docs.map(d => [d.username, d]));
summary.users = summary.users.map(entry => {
const doc = byName.get(entry.value);
if (!doc) return entry; // a deleted account still counts
const profile = doc.profile || {};
// The same rule models/users.js getInitials() uses, so an account looks the
// same here as everywhere else: an explicit initials field, else the initials
// of the full name, else the first letter of the username.
const initials = profile.initials
|| (profile.fullname || '').split(/\s+/).filter(Boolean).map(w => w[0]).join('').toUpperCase()
|| (doc.username || '').slice(0, 1).toUpperCase();
return {
...entry,
userId: doc._id,
fullname: profile.fullname || '',
initials,
avatarUrl: profile.avatarUrl || '',
loginDisabled: !!doc.loginDisabled,
};
});
return summary;
}
function initialsFor(user) {
const profile = user.profile || {};
return profile.initials
|| (profile.fullname || '').split(/\s+/).filter(Boolean)
.map(word => word[0]).join('').toUpperCase()
|| (user.username || '').slice(0, 1).toUpperCase();
}
// One person with one child row per address. The per-user tally is the source
// of that person's count; LoginAddresses supplies the location last reported
// for the same address by a CDN or trusted reverse proxy.
function personSummary(user, byAddress) {
const addresses = tallyList(user.loginAddresses);
return {
userId: user._id,
username: user.username || user._id,
fullname: (user.profile && user.profile.fullname) || '',
initials: initialsFor(user),
avatarUrl: (user.profile && user.profile.avatarUrl) || '',
loginDisabled: !!user.loginDisabled,
addresses: addresses.map(entry => {
const doc = byAddress.get(entry.value) || {};
return {
address: entry.value,
ipv4: doc.ipv4 || (entry.family === 'ipv4' ? entry.value : ''),
ipv6: doc.ipv6 || (entry.family === 'ipv6' ? entry.value : ''),
location: doc.location || entry.location || null,
locationLabel: doc.locationLabel || '',
logins: entry.count || 0,
firstAt: entry.firstAt,
at: entry.at,
};
}),
moreAddresses: (user.loginAddresses && user.loginAddresses.overflow) || 0,
};
}
async function peopleSummaries(users) {
const values = [...new Set(users.flatMap(user =>
tallyList(user.loginAddresses).map(entry => entry.value)))];
const docs = values.length
? await LoginAddresses.find(
{ address: { $in: values } },
{ fields: { address: 1, ipv4: 1, ipv6: 1, location: 1, locationLabel: 1 } },
).fetchAsync()
: [];
const byAddress = new Map(docs.map(doc => [doc.address, doc]));
return users.map(user => personSummary(user, byAddress));
}
// Rows written before per-user loginAddresses existed still contain the same
// history in LoginAddresses.users. Reconstruct the person-first view from that
// side when no new-format people match, so an upgrade does not make every
// existing office disappear until everybody happens to log in again.
async function legacyPeopleSummaries({ search = '', limit = 25, skip = 0 } = {}) {
const addressDocs = await LoginAddresses.find({}, {
fields: { address: 1, ipv4: 1, ipv6: 1, location: 1, locationLabel: 1,
users: 1 },
}).fetchAsync();
const byUsername = new Map();
for (const address of addressDocs) {
for (const entry of tallyList(address.users, 200)) {
const username = entry.value;
if (!username) continue;
if (!byUsername.has(username)) byUsername.set(username, []);
byUsername.get(username).push({
address: address.address,
ipv4: address.ipv4 || '',
ipv6: address.ipv6 || '',
location: address.location || null,
locationLabel: address.locationLabel || '',
logins: entry.count || 0,
firstAt: entry.firstAt,
at: entry.at,
});
}
}
const needle = String(search || '').toLocaleLowerCase();
const names = [...byUsername.keys()].filter(username => {
if (!needle) return true;
if (username.toLocaleLowerCase().includes(needle)) return true;
return byUsername.get(username).some(row =>
String(row.address || '').toLocaleLowerCase().includes(needle)
|| String(row.locationLabel || '').toLocaleLowerCase().includes(needle));
}).sort((a, b) => a.localeCompare(b));
const pageNames = names.slice(skip, skip + limit);
const users = pageNames.length ? await Meteor.users.find(
{ username: { $in: pageNames } },
{ fields: { username: 1, profile: 1, loginDisabled: 1 } },
).fetchAsync() : [];
const userByName = new Map(users.map(user => [user.username, user]));
return {
total: names.length,
people: pageNames.map(username => {
const user = userByName.get(username);
return {
userId: user?._id || '',
username,
fullname: user?.profile?.fullname || '',
initials: user ? initialsFor(user) : username.slice(0, 1).toUpperCase(),
avatarUrl: user?.profile?.avatarUrl || '',
loginDisabled: !!user?.loginDisabled,
addresses: byUsername.get(username),
moreAddresses: 0,
};
}),
};
}
// Admin Panel → Problems → Offices: people first, with each person's addresses
// kept together and the successful-login count shown for every address.
//
// It is also the evidence behind WeKan not blocking addresses: a security event
// blocks the ACCOUNT that caused it, because the alternative would take everyone
// behind a shared address off WeKan at once.
async function requireAdmin(context) {
const user = context.userId && await Meteor.users.findOneAsync(context.userId);
if (!user || !user.isAdmin) {
throw new Meteor.Error('not-authorized', 'Admin only');
}
}
async function visiblePeople(context, userIds) {
const caller = context.userId && await Meteor.users.findOneAsync(
context.userId, { fields: { isAdmin: 1, orgs: 1 } });
if (!tenantAdmin.canOpenAdminPanel(caller)) {
throw new Meteor.Error('not-authorized', 'Admin access required');
}
return Meteor.users.find(
tenantAdmin.peopleScopeSelector(caller, { _id: { $in: userIds } }),
{ fields: { username: 1, loginAddresses: 1 } },
).fetchAsync();
}
async function locationReports(users) {
const addresses = [...new Set(users.flatMap(user =>
tallyList(user.loginAddresses).map(entry => entry.value)))];
const docs = addresses.length ? await LoginAddresses.find(
{ address: { $in: addresses } },
{ fields: { address: 1, ipv4: 1, ipv6: 1, location: 1 } },
).fetchAsync() : [];
return users.map(user => ({
userId: user._id,
username: user.username || user._id,
countries: loginLocationsByCountry(user, docs),
}));
}
if (Meteor.isServer) {
Meteor.methods({
// Country counters for the current People page, and the rows behind each
// counter. Returning one authorized batch avoids one method call per user.
async peopleLoginLocations(userIds) {
check(userIds, [String]);
if (userIds.length > 200) {
throw new Meteor.Error('too-many-users', 'At most 200 users per page');
}
const users = await visiblePeople(this, [...new Set(userIds)]);
return locationReports(users);
},
// People first, with all of each person's addresses kept together.
async loginOffices(options) {
check(options, Match.Optional({
limit: Match.Optional(Number),
skip: Match.Optional(Number),
search: Match.Optional(String),
}));
await requireAdmin(this);
const opts = options || {};
const selector = { 'loginAddresses.entries': { $exists: true } };
if (opts.search) {
const safe = String(opts.search).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const rx = new RegExp(safe, 'i');
const matchingAddresses = await LoginAddresses.find(
{ $or: [{ address: rx }, { locationLabel: rx }] },
{ fields: { users: 1 } },
).fetchAsync();
const names = new Set();
matchingAddresses.forEach(row => {
Object.values((row.users && row.users.entries) || {})
.forEach(entry => names.add(entry.value));
});
selector.$or = [
{ username: rx },
{ 'profile.fullname': rx },
...(names.size ? [{ username: { $in: [...names] } }] : []),
];
}
const limit = Math.min(Math.max(opts.limit || 25, 1), 200);
const skip = Math.max(opts.skip || 0, 0);
const total = await Meteor.users.find(selector).countAsync();
if (total === 0) {
return legacyPeopleSummaries({ search: opts.search, limit, skip });
}
const users = await Meteor.users.find(selector, {
fields: { username: 1, profile: 1, loginDisabled: 1, loginAddresses: 1 },
sort: { username: 1 }, limit, skip,
}).fetchAsync();
return { total, people: await peopleSummaries(users) };
},
// One address, in full: who logs in from it and how often.
async loginOffice(address) {
check(address, String);
await requireAdmin(this);
const row = await LoginAddresses.findOneAsync({ address });
return row ? withIdentities(officeSummary(row)) : null;
},
});
}
// Member Settings / Privacy and my data (WP-10): the signed-in person's own
// sign-in addresses - where from, how often, first and last time - from the
// same tally Admin Panel / People / Login locations reads. Only one's own.
Meteor.methods({
async getMyLoginHistory() {
if (!this.userId) throw new Meteor.Error('not-authorized');
const user = await Meteor.users.findOneAsync(this.userId, { fields: { username: 1, profile: 1, loginDisabled: 1, loginAddresses: 1 } });
if (!user) return [];
const [person] = await peopleSummaries([user]);
return person.addresses.map(a => ({
address: a.address, locationLabel: a.locationLabel, logins: a.logins, firstAt: a.firstAt, at: a.at,
}));
},
});