Files
wekan/server/sharedLabels.js
T
Lauri Ojansivu 7595727917 Boards every signed-in user may edit, per-list permissions, board keys and shared labels.
A board can let every signed-in user edit it, when Admin Panel / Settings /
Visibility allows it: a non-member then comments, edits and moves cards like
a Normal member, and their first edit makes them one. Board-wide settings
stay members-only.

Lists can allow or refuse editing and commenting per role (list menu, board
admins): built-in roles, custom roles and, on an open board, everyone signed
in. The server applies it to card, checklist, attachment and comment writes
and to moves into the list; the REST API honours a list's refusal on every
per-list card route. A direct client write of the setting is refused and
shown in Admin Panel / Problems.

Every board has a key, unique in the instance, suggested from its title and
set for existing boards at startup; card numbers show as KEY-7 and searching
for KEY-7 finds the card.

Shared labels in Admin Panel / Labels, for every board or an organization's
or team's boards; a board uses them beside its own, and a rename or colour
change reaches every board that uses it.

New strings translated in all languages. Low confidence: chr, tlh, wal, ay,
ace, nah, gn, iu, zgh, rup, wo, se, ff, tig, vo, bm, kl, bua, nap, mai, to,
ks, qu, bo, fj, haw, om, ss, ts, ve-PP, lld, scn.

Thanks to Meeques, justinr1234, tiFabGrinch, ejlkemper and xet7 !

Fixes #4575,
Fixes #4527,
Fixes #2449,
Fixes #2450,
Fixes #2761,
Fixes #4698.
2026-10-14 09:28:45 +03:00

105 lines
4.5 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import { Random } from 'meteor/random';
import Boards from '/models/boards';
import SharedLabels from '/models/sharedLabels';
import { LABEL_COLORS } from '/models/metadata/colors';
import { allowIsBoardMemberWithWriteAccess } from '/server/lib/utils';
// Shared labels (#2761, #4698), the server half: who sees which catalog, the
// site admin's edits (which reach every board copy), and using one on a board.
const { availableOn, boardCopyOf, cleanSharedLabel } = require('/models/lib/sharedLabels');
const isHexColor = v => /^#[0-9a-fA-F]{6}$/.test(v);
const isAllowedColor = v => LABEL_COLORS.includes(v) || isHexColor(v);
// A user sees the instance catalog and those of their own organizations and
// teams - never another organization's label names (tenant isolation).
Meteor.publish('sharedLabels', async function () {
if (!this.userId) return this.ready();
const user = await Meteor.users.findOneAsync(this.userId, { fields: { isAdmin: 1, orgs: 1, teams: 1 } });
if (!user) return this.ready();
if (user.isAdmin) return SharedLabels.find();
const orgIds = (user.orgs || []).map(o => o && o.orgId).filter(Boolean);
const teamIds = (user.teams || []).map(t => t && t.teamId).filter(Boolean);
return SharedLabels.find({ $or: [
{ scope: 'instance' },
{ scope: 'org', scopeId: { $in: orgIds } },
{ scope: 'team', scopeId: { $in: teamIds } },
] });
});
async function requireSiteAdmin(userId) {
const user = userId && (await Meteor.users.findOneAsync(userId, { fields: { isAdmin: 1 } }));
if (!user || !user.isAdmin) throw new Meteor.Error('not-authorized');
}
// Every board copy of a shared label follows its name and colour.
async function updateCopies(labelId, name, color) {
const boards = await Boards.find({ 'labels.sharedLabelId': labelId }, { fields: { labels: 1 } }).fetchAsync();
for (const board of boards) {
const $set = {};
board.labels.forEach((l, i) => {
if (l && l.sharedLabelId === labelId) {
$set[`labels.${i}.name`] = name;
$set[`labels.${i}.color`] = color;
}
});
if (Object.keys($set).length) await Boards.direct.updateAsync(board._id, { $set });
}
}
Meteor.methods({
async 'sharedLabels.save'(labelId, input) {
check(labelId, Match.OneOf(String, null));
check(input, Object);
await requireSiteAdmin(this.userId);
let clean;
try {
clean = cleanSharedLabel(input, isAllowedColor);
} catch (error) {
throw new Meteor.Error(error.message);
}
if (labelId) {
if (!(await SharedLabels.findOneAsync(labelId))) throw new Meteor.Error('not-found');
await SharedLabels.updateAsync(labelId, { $set: clean });
await updateCopies(labelId, clean.name, clean.color);
return labelId;
}
return SharedLabels.insertAsync({ ...clean, sort: await SharedLabels.find().countAsync() });
},
async 'sharedLabels.remove'(labelId) {
check(labelId, String);
await requireSiteAdmin(this.userId);
// The copies stay, as the boards' own labels.
const boards = await Boards.find({ 'labels.sharedLabelId': labelId }, { fields: { labels: 1 } }).fetchAsync();
for (const board of boards) {
const $unset = {};
board.labels.forEach((l, i) => { if (l && l.sharedLabelId === labelId) $unset[`labels.${i}.sharedLabelId`] = ''; });
if (Object.keys($unset).length) await Boards.direct.updateAsync(board._id, { $unset });
}
await SharedLabels.removeAsync(labelId);
return true;
},
// Use a shared label on a board: its copy is added once, and the copy's id
// is returned so the caller can put it on a card.
async 'boards.useSharedLabel'(boardId, labelId) {
check(boardId, String);
check(labelId, String);
const board = await Boards.findOneAsync(boardId);
if (!board || !allowIsBoardMemberWithWriteAccess(this.userId, board)) throw new Meteor.Error('not-authorized');
const label = await SharedLabels.findOneAsync(labelId);
if (!label || !availableOn(label, board)) throw new Meteor.Error('not-found');
const existing = (board.labels || []).find(l => l && l.sharedLabelId === labelId);
if (existing) return existing._id;
const copy = boardCopyOf(label, Random.id(6));
await Boards.updateAsync({ _id: boardId, 'labels.sharedLabelId': { $ne: labelId } }, { $push: { labels: copy } });
const after = await Boards.findOneAsync(boardId, { fields: { labels: 1 } });
return (after.labels || []).find(l => l && l.sharedLabelId === labelId)._id;
},
});
export { updateCopies };