Files
wekan/models/lib/boardVisibilitySelectors.js
T
Lauri Ojansivu bcf2305448 Show private starred boards in the header Favorites popup.
The starredPublicBoards publication only sent public and instance boards,
so a user whose favorites are all private boards saw an empty popup. It now
sends every starred board the user may see - public/instance, or an active
membership, organization, team or domain share - through the shared
visibility builder, so a revoked share or stale star still never brings a
private board back.

Thanks to Raymond-Voogt and xet7 !

Fixes #6764.
2026-10-12 22:21:30 +03:00

123 lines
4.8 KiB
JavaScript

const { withoutMembershipSelectors } = require('./boardPermission');
// Pure, dependency-free construction of the Mongo `$or` that answers "which
// boards may this user see". No Meteor imports, so it is unit tested directly
// with plain Node (tests/boardVisibilitySelectors.test.cjs).
//
// GHSA-gwc4-fw7p-gw58: this was written out TWICE, and the two copies disagreed.
// Boards.userBoards() - the All Boards list - matched org/team/domain shares
// with `$elemMatch: { ..., isActive: true }`, but the `board` publication (the
// one that sends the board and all its cards) matched them with a dotted
// `'orgs.orgId': { $in: [...] }`, which ignores isActive entirely. isActive is
// the REVOKE switch: setting a share to `isActive: false` made the board vanish
// from All Boards, as it should, while a user who still knew the boardId could
// subscribe to it and receive the whole private board. A revoke that the primary
// data publication does not honour is not a revoke.
//
// So there is one builder now, and both callers use it. A share entry counts
// only while it is active, everywhere.
/**
* @param {object} args
* @param {string} [args.userId] the subscriber; omitted/falsy means anonymous
* @param {string[]} [args.orgIds] organisations the user belongs to
* @param {string[]} [args.teamIds] teams the user belongs to
* @param {string[]} [args.emailDomains] the user's verified email domains (#5850)
* @param {boolean} [args.includePublic=true] include `{ permission: 'public' }`,
* which is not a relationship to the user at all but "anybody may open
* this" - wanted for direct/public discovery, not in a relationship-only
* list or a search over all boards.
* @param {boolean} [args.membersOnly=false] require active direct membership;
* excludes public, organization, team and domain access clauses
* @return {object[]} the `$or` clauses, in a stable order
*/
function boardVisibilitySelectors({
userId,
orgIds = [],
teamIds = [],
emailDomains = [],
includePublic = true,
membersOnly = false,
} = {}) {
const selectors = [];
// #3249: 'instance' boards count as readable-without-membership for a
// signed-in subscriber only (models/lib/boardPermission.js).
if (includePublic && !membersOnly) {
selectors.push(...withoutMembershipSelectors(!!userId));
}
if (!userId) {
return selectors;
}
const clean = (values) =>
Array.isArray(values) ? values.filter((v) => typeof v === 'string' && v) : [];
selectors.push({ members: { $elemMatch: { userId, isActive: true } } });
if (membersOnly) return selectors;
const cleanOrgIds = clean(orgIds);
if (cleanOrgIds.length) selectors.push({
orgs: { $elemMatch: { orgId: { $in: cleanOrgIds }, isActive: true } },
});
const cleanTeamIds = clean(teamIds);
if (cleanTeamIds.length) selectors.push({
teams: { $elemMatch: { teamId: { $in: cleanTeamIds }, isActive: true } },
});
// #5850: domain-based board sharing — board shared with the user's email domain.
const cleanDomains = clean(emailDomains);
if (cleanDomains.length) selectors.push({
domains: { $elemMatch: { domain: { $in: cleanDomains }, isActive: true } },
});
return selectors;
}
// A favorite is a user relationship only while the board remains readable
// without membership. Requiring permission here prevents a stale star from
// restoring access to a private board after its membership or share is
// revoked. Stars belong to signed-in users, so an 'instance' board (#3249)
// counts too.
function starredPublicBoardSelector(boardIds) {
const ids = Array.isArray(boardIds)
? boardIds.filter(id => typeof id === 'string' && id)
: [];
return ids.length ? { _id: { $in: ids }, permission: { $in: ['public', 'instance'] } } : null;
}
// #6764: the header's Favorites popup lists `profile.starredBoards` through
// Boards.userBoards on the client, so every starred board it may show has to be
// in minimongo on EVERY page. The `starredPublicBoards` publication only sent
// public/instance ones, which left a user whose favorites are all private
// boards with an empty popup. This selects the starred ids the user may
// actually see - public/instance, or an ACTIVE membership / org / team / domain
// share, the same builder as everywhere else - so a revoked share or a stale
// star still never brings a private board back.
function starredVisibleBoardSelector({
boardIds,
userId,
orgIds = [],
teamIds = [],
emailDomains = [],
} = {}) {
const starredPublic = starredPublicBoardSelector(boardIds);
if (!starredPublic) return null;
const { _id, permission } = starredPublic;
return {
_id,
$or: [
{ permission },
...boardVisibilitySelectors({
userId, orgIds, teamIds, emailDomains, includePublic: false,
}),
],
};
}
module.exports = {
boardVisibilitySelectors,
starredPublicBoardSelector,
starredVisibleBoardSelector,
};