Files
wekan/server/propagateOrgTeamMembers.js
T
Lauri Ojansivu 8e89262d00 Fix #6559: the Propagate Members To Boards checkbox passed a selector where an id was wanted.
Ticking "Propagate Members To Boards" for a team in Admin Panel > People > Teams
stored the flag and added nobody. The members only appeared later, when the LDAP
sync cron ran the propagation by its other, correct route.

`setTeamPropagateMembersToBoards(team, value)` receives `team` as the SELECTOR the
client sent - `{ _id: … }`, the same value it hands to `Team.updateAsync` on the
line above - and passed it whole:

  await propagateGroupMembersToBoards('team', team);

`propagateGroupMembersToBoards(kind, groupId)` puts that straight into the member
lookup, so it ran

  Meteor.users.find({ 'teams.teamId': { _id: 'abc' } })

comparing a string field against an object. It matched nobody, the function
returned early with "0 boards updated, 0 members added", and nothing threw. The
checkbox went green and did nothing.

The org column beside it was identical - `setOrgPropagateMembersToBoards` passed
`org` the same way - and is fixed with it, though the report was about teams.

Both call sites pass the id now, and `propagateGroupMembersToBoards` normalises
whatever it is given at the one point every caller goes through: a plain id, or a
document/selector carrying one. Anything it cannot turn into an id is REFUSED AND
LOGGED rather than quietly treated as a group with no members, because a silent
"0 members added" is exactly how this survived a release. A missing group stays
quiet - propagateAllFlaggedGroupsToBoards may legitimately have nothing to pass.

The select-all checkbox in the column header is the same promise to the admin, and
it did not propagate at all, not even wrongly: setAllTeamsFeature /
setAllOrgsFeature set the flag on every row and stopped. They now propagate when
that field is the one being turned ON, and only for their own kind, so ticking the
team column does not act on the org column beside it.
propagateAllFlaggedGroupsToBoards takes an optional kind for this; the default is
unchanged, so the LDAP cron and the propagateOrgTeamMembersToBoards method still
do both.

The two wiring tests in tests/propagateMembers.test.cjs REQUIRED the buggy calls -
they asserted `propagateGroupMembersToBoards('team', team)` verbatim, pinning that
a call happened without asking whether its argument was right, and so held the bug
in place. They require the id now and fail if the whole selector comes back, and
the normaliser is exercised for real: every shape a caller might hold an id in
resolves to it, a selector with no id resolves to null AND warns, and an absent
group is quiet.

Thanks to ChristianMa97 and xet7 !

Fixes #6559,
2026-07-31 01:39:20 +03:00

133 lines
5.4 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { ReactiveCache } from '/imports/reactiveCache';
import Org from '/models/org';
import Team from '/models/team';
import Boards from '/models/boards';
import { membersToAddToBoard } from '/models/lib/propagateMembers';
// #4737 / #5850: implement the per-org/team "Propagate Members To Boards" flag
// (orgPropagateMembersToBoards / teamPropagateMembersToBoards). Until now the
// flag was stored and displayed in the Admin Panel but never acted on, AND the
// propagation method had no caller at all (dead code). This is the
// ldap-sync.py-style propagation: for each org/team that has the flag on, the
// org/team's member users are added as members of the regular boards that list
// that org/team. The update is strictly add-only — existing board members
// (assigned manually or from another source) are never removed or modified —
// and template boards (type 'template-board'/'template-container') are
// explicitly skipped, since those are group-only and must not gain per-user
// members.
//
// Membership is read from the USER docs (user.orgs[].orgId / user.teams[].teamId),
// matching how setUserOrgsTeamsFromLdap maintains it.
// The id of the org/team to propagate, from whatever the caller had to hand.
//
// #6559: this used to be taken on trust, and both flag-setters passed the whole
// `{ _id: … }` selector their Meteor method received instead of the id inside it.
// `Meteor.users.find({ 'teams.teamId': { _id: 'abc' } })` compares a string field
// against an object, so it matched nobody, and the function returned "0 members
// added" without an error — the Admin Panel checkbox went green and did nothing.
// Normalising here rather than at the call sites is deliberate: this is the one
// place every caller passes through, so a third caller cannot make the same
// mistake, and a value that is NOT an id says so in the log instead of looking
// like a group that happens to have no members.
function groupIdOf(kind, group) {
if (typeof group === 'string') return group;
if (group && typeof group === 'object' && typeof group._id === 'string') {
return group._id;
}
if (group) {
console.warn(
`[propagate] cannot propagate ${kind} members: expected an id (or a document ` +
'with one) and got', group,
);
}
return null;
}
// Propagate a SINGLE org or team's members to the boards that list it. `kind` is
// 'org' or 'team'; `group` is its id, or a document/selector carrying one.
// Add-only; template boards skipped. Returns { boardsUpdated, membersAdded }.
// Exported so the flag-setters (org/team) can run it immediately when the flag is
// turned on.
export async function propagateGroupMembersToBoards(kind, group) {
const field = kind === 'org' ? 'orgs' : 'teams';
const idField = kind === 'org' ? 'orgId' : 'teamId';
let boardsUpdated = 0;
let membersAdded = 0;
const groupId = groupIdOf(kind, group);
if (!groupId) return { boardsUpdated, membersAdded };
const memberUsers = await Meteor.users
.find({ [`${field}.${idField}`]: groupId }, { fields: { _id: 1 } })
.fetchAsync();
const memberUserIds = memberUsers.map(u => u._id);
if (memberUserIds.length === 0) {
return { boardsUpdated, membersAdded };
}
// Regular boards only (type 'board'); template boards are group-only and must
// never gain per-user members, so they are excluded here.
const boards = await Boards.find({
type: 'board',
archived: { $ne: true },
[field]: { $elemMatch: { [idField]: groupId, isActive: true } },
}).fetchAsync();
for (const board of boards) {
const newMembers = membersToAddToBoard(board.members, memberUserIds);
if (newMembers.length === 0) continue;
await Boards.updateAsync(
{ _id: board._id },
{ $push: { members: { $each: newMembers } } },
);
boardsUpdated += 1;
membersAdded += newMembers.length;
}
return { boardsUpdated, membersAdded };
}
// Propagate EVERY org/team that has its flag on. `kind` limits it to one of them
// ('org' or 'team'); the default - every kind - is what the LDAP cron and the
// `propagateOrgTeamMembersToBoards` method run.
export async function propagateAllFlaggedGroupsToBoards(kind = null) {
let boardsUpdated = 0;
let membersAdded = 0;
if (kind === null || kind === 'org') {
const orgs = await Org.find({ orgPropagateMembersToBoards: true }).fetchAsync();
for (const org of orgs) {
const r = await propagateGroupMembersToBoards('org', org._id);
boardsUpdated += r.boardsUpdated;
membersAdded += r.membersAdded;
}
}
if (kind === null || kind === 'team') {
const teams = await Team.find({ teamPropagateMembersToBoards: true }).fetchAsync();
for (const team of teams) {
const r = await propagateGroupMembersToBoards('team', team._id);
boardsUpdated += r.boardsUpdated;
membersAdded += r.membersAdded;
}
}
return { boardsUpdated, membersAdded };
}
Meteor.methods({
async propagateOrgTeamMembersToBoards() {
// Allow server-to-server calls (connection === null, used by the LDAP sync)
// and admins; reject any other client-originated call. Mirrors the guard in
// server/ldapGroupSync.js setUserOrgsTeamsFromLdap.
if (this.connection !== null) {
const caller = this.userId ? await ReactiveCache.getUser(this.userId) : null;
if (!caller || caller.isAdmin !== true) {
throw new Meteor.Error('not-authorized', 'Not authorized');
}
}
return await propagateAllFlaggedGroupsToBoards();
},
});