mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
What each role may do was spelled out THREE times - in the server allow helpers,
in the client's canModify* helpers, and in prose in the docs - and the three had
drifted apart. Every place they disagreed was a role that did not do what its name
says. models/lib/boardRoleCapabilities.js is that table now, in code, and
everything reads it: the allow rules, the UI helpers, the new Admin Panel pane and
the documentation. There is nowhere left for a fourth opinion to appear.
THE THREE GAPS, fixed:
* "Comment only, assigned" (isCommentAssignedOnly) had FULL WRITE ACCESS.
Nothing outside the card publications read that flag and it was not in the
write rule, so the role could create and edit cards, lists and checklists - it
was "Normal, assigned only" under another name. It is comment-only now, like
the role it is named after; the two differ only in what they see.
* "No comments" (isNoComments) could not write ANYTHING. The write rule excluded
it, so the role blocked editing as well as commenting - a second read-only
role under a name that says otherwise, and one the UI still offered the edit
buttons for. It blocks commenting only now, which is what the name and the
board schema both say it is for.
* The write rule did not exempt board admins. Every has*() helper on the board
ignores a flag on an admin; that one read the raw flags, so an admin who also
carried isNoComments silently lost write access. Not reachable from the Web
UI, which writes all eight flags at once, but reachable over the REST API,
which takes them individually. memberRoleOf() resolves isAdmin first.
The UI helpers were part of the same drift - canModifyCard() did not exclude
isNoComments while the server did, and canModifyBoard() excluded neither
isNoComments nor isWorker - so each disagreement was a button offered to somebody
whose write the server then refused. All three ask the same table now.
A FOURTH gap found while building the table, NOT fixed: a Worker cannot move a
card, which is the one thing the role is for. Moving a card is a card UPDATE, so
it goes through the write rule, which excludes Worker. The fix is not a table
edit - it means letting a role write SOME fields of a card and not others, and
"assign himself" means validating that a member change only ever adds the caller.
That is a field-level policy on the path every card update takes and wants
deciding on purpose. It is recorded under "Known gaps" with what it would take.
ROLES STATUS, at Admin Panel / People / Roles, below the Save button: a READ-ONLY
table of what each role may do - which cards it sees, and whether it may comment,
create and edit, or change the board's settings. It is the shared table page
(docs/Design/Page/Table.md), so it looks and behaves like every other Admin Panel
table, with no markup of its own; it has no rowTemplate, no actions and no
editable cell, because a role's capabilities are a property of the code and not a
setting. Every string is a translation key, the Yes/No of each cell included - the
values are booleans, and a hard-coded "Yes" would be English in every language.
The "Invite to board" column reads the pane's WORKING copy, so the table follows
the checkboxes above the Save button as they are ticked, before saving, which is
the point of putting it there.
docs/Design/Page/Table.md listed Roles among the panes that do NOT use the shared
design, "so the gap is visible rather than forgotten" - it has a table now, so it
moved to the list of pages that use it, and the exclusion covers the two panes
that are still nothing but a form.
Two guards were checking the shape of the old code rather than what it guaranteed,
and are updated with the reason in the test: listDragPermission read the flag
exclusions out of canModifyBoard's own body (it has none now, so it checks the
BEHAVIOUR - anonymous is falsy, and each excluded role still cannot drag a list),
and tablePage counted the pager handlers file-wide, which also forbade a handler
on a different template - a separate event map is not a duplicate key.
tests/boardRoles.test.cjs is the suite: the module itself including each fixed
gap, that the server rules and the UI helpers ARE the table and name no flag
themselves, that every cell of the documentation matches it, that the
assigned-only column matches the publication scope, that the remaining gap is
still real and the three fixed ones are recorded as fixed, and that the Roles
Status pane renders from the table, is read-only, is the shared template, follows
the working copy, and translates every string it shows.
Thanks to xet7 !
256 lines
12 KiB
JavaScript
256 lines
12 KiB
JavaScript
import Boards from '/models/boards';
|
|
import Cards from '/models/cards';
|
|
import Checklists from '/models/checklists';
|
|
const { memberCan } = require('/models/lib/boardRoleCapabilities');
|
|
|
|
export function allowIsBoardAdmin(userId, board) {
|
|
return board && board.hasAdmin(userId);
|
|
}
|
|
|
|
export function allowIsBoardMember(userId, board) {
|
|
return board && board.hasMember(userId);
|
|
}
|
|
|
|
export function allowIsAnyBoardMember(userId, boards) {
|
|
return boards.some(board => {
|
|
return board && board.hasMember(userId);
|
|
});
|
|
}
|
|
|
|
// May the user COMMENT on this board (a card comment or a comment reaction)?
|
|
//
|
|
// The name is historical - it reads as "comment-only members are allowed here",
|
|
// which is what it means: everyone except the roles whose capability table says
|
|
// they may not comment.
|
|
export function allowIsBoardMemberCommentOnly(userId, board) {
|
|
return !!(board && memberCan(board.members, userId, 'comment'));
|
|
}
|
|
|
|
export function allowIsBoardMemberNoComments(userId, board) {
|
|
return board && board.hasMember(userId) && !board.hasNoComments(userId);
|
|
}
|
|
|
|
// Check if user has write access to board (can create/edit cards and lists)
|
|
//
|
|
// Both of these used to spell the rule out as a list of flags, and that list had
|
|
// drifted from the roles' own names in three ways (see
|
|
// docs/Features/Members/Roles.md):
|
|
//
|
|
// * it excluded `isNoComments`, so the role that is meant to stop COMMENTING
|
|
// stopped writing as well - a second read-only role under a name that says
|
|
// otherwise, and one the UI still offered the edit buttons for;
|
|
// * it did NOT exclude `isCommentAssignedOnly`, so the "comment only, on
|
|
// assigned cards" role could create and edit cards, lists and checklists -
|
|
// it was "normal, assigned only" under another name;
|
|
// * it read the RAW flags with no `isAdmin` exemption, unlike every has*()
|
|
// helper on the board - so a board admin who also carried one of those flags
|
|
// (which the Web UI cannot produce, but the REST API can) silently lost write
|
|
// access.
|
|
//
|
|
// The rule now comes from models/lib/boardRoleCapabilities.js, which is the same
|
|
// table the client's canModify* helpers, the Admin Panel's Roles Status pane and
|
|
// the documentation are built from - so there is nowhere for a fourth opinion to
|
|
// appear.
|
|
export function allowIsBoardMemberWithWriteAccess(userId, board) {
|
|
return !!(board && memberCan(board.members, userId, 'write'));
|
|
}
|
|
|
|
// Write-access variant of allowIsAnyBoardMember: true if the user has write
|
|
// access on at least one of the boards. Used where an object (e.g. a Custom
|
|
// Field) spans several boards but read-only/comment-only/worker members must
|
|
// still be blocked from mutating it.
|
|
export function allowIsAnyBoardMemberWithWriteAccess(userId, boards) {
|
|
return boards.some(board => allowIsBoardMemberWithWriteAccess(userId, board));
|
|
}
|
|
|
|
// Security (GHSA-gm7v-pc38-53jr): the Cards/Lists/Swimlanes allow rules only
|
|
// authorize an update against the document's CURRENT (source) boardId. A
|
|
// malicious DDP client can therefore relocate a document it owns into a private
|
|
// board it is not a member of by setting a new boardId in the update modifier:
|
|
// the allow rule still sees the attacker's own source board and approves it.
|
|
// This deny rule closes that hole by rejecting any update that moves a document
|
|
// to a destination boardId on which the caller does not have write access.
|
|
// Returns true to DENY. Used by the Cards/Lists/Swimlanes deny() rules.
|
|
export async function denyCrossBoardMove(userId, modifier) {
|
|
const set = modifier && modifier.$set;
|
|
if (!set) return false;
|
|
const newBoardId = set.boardId;
|
|
// Nothing being moved across boards.
|
|
if (typeof newBoardId !== 'string' || !newBoardId) return false;
|
|
// Caller must have write access to the destination board.
|
|
return !allowIsBoardMemberWithWriteAccess(userId, await Boards.findOneAsync(newBoardId));
|
|
}
|
|
|
|
// Shared destination gate for the checklist deny rules below: returns true to
|
|
// DENY when boardId names a board the caller cannot write to. An unknown/missing
|
|
// board id also denies, matching denyCrossBoardMove's fail-closed behavior.
|
|
async function denyMoveToUnwritableBoard(userId, boardId) {
|
|
if (typeof boardId !== 'string' || !boardId) return false;
|
|
return !allowIsBoardMemberWithWriteAccess(userId, await Boards.findOneAsync(boardId));
|
|
}
|
|
|
|
// Security (GHSA-gv8h-5p3p-6hx7): Checklists and ChecklistItems are attached to a
|
|
// card and carry a denormalized boardId. They are MOVED between cards by setting a
|
|
// new cardId (and, for items, a new checklistId) in a direct collection update; a
|
|
// before.update hook then re-derives boardId from the destination card. The allow
|
|
// rules only authorize against the document's CURRENT (source) cardId, so a DDP
|
|
// client with write access to its own board can relocate a checklist/item it owns
|
|
// onto a card in a private board it is not a member of. This is the same class as
|
|
// denyCrossBoardMove / BoardBleed, but for the card-attached checklist documents
|
|
// that the boardId-only deny rule does not cover.
|
|
//
|
|
// Returns true to DENY any update whose destination board — resolved from a new
|
|
// boardId or a new cardId in the modifier — the caller cannot write to.
|
|
// Legitimate moves (destination writable) and same-card edits are unaffected, and
|
|
// server-side Meteor methods bypass allow/deny entirely, so the trusted
|
|
// moveChecklist method still works.
|
|
export async function denyCrossBoardMoveByCard(userId, modifier) {
|
|
const set = modifier && modifier.$set;
|
|
if (!set) return false;
|
|
// Direct boardId change (defense in depth, same as denyCrossBoardMove).
|
|
if (await denyMoveToUnwritableBoard(userId, set.boardId)) return true;
|
|
// cardId change: resolve the destination card's board.
|
|
if (typeof set.cardId === 'string' && set.cardId) {
|
|
const card = await Cards.findOneAsync(set.cardId);
|
|
if (!card) return true;
|
|
if (await denyMoveToUnwritableBoard(userId, card.boardId)) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// As denyCrossBoardMoveByCard, plus the ChecklistItem-only checklistId move: an
|
|
// item can be re-parented to another checklist, which (via that checklist's card)
|
|
// may live on a board the caller cannot write to. Returns true to DENY.
|
|
export async function denyCrossBoardMoveByChecklistItem(userId, modifier) {
|
|
if (await denyCrossBoardMoveByCard(userId, modifier)) return true;
|
|
const set = modifier && modifier.$set;
|
|
if (set && typeof set.checklistId === 'string' && set.checklistId) {
|
|
const checklist = await Checklists.findOneAsync(set.checklistId);
|
|
if (!checklist) return true;
|
|
const card = await Cards.findOneAsync(checklist.cardId);
|
|
if (!card) return true;
|
|
if (await denyMoveToUnwritableBoard(userId, card.boardId)) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Check if user has write access via a card's board
|
|
export async function allowIsBoardMemberWithWriteAccessByCard(userId, card) {
|
|
const board = card && await Boards.findOneAsync(card.boardId);
|
|
return allowIsBoardMemberWithWriteAccess(userId, board);
|
|
}
|
|
|
|
export async function allowIsBoardMemberByCard(userId, card) {
|
|
const board = card && await Boards.findOneAsync(card.boardId);
|
|
return board && board.hasMember(userId);
|
|
}
|
|
|
|
// Policy: can a user update a board's 'sort' field?
|
|
// Requirements:
|
|
// - user must be authenticated
|
|
// - update must touch ONLY the 'sort' field (nothing else)
|
|
// - user must be a member of the board
|
|
//
|
|
// Security (SortBleed, GHSA-xm8x-c8wg-jhmf): this rule is OR'd with the
|
|
// admin-only `allowIsBoardAdmin` update rule (server/permissions/boards.js) so
|
|
// that any board member can drag-reorder boards. Meteor applies the WHOLE
|
|
// modifier once any allow rule approves and no deny rule rejects, and it does
|
|
// NOT scope the approving rule to the field that satisfied it. If this helper
|
|
// merely checked that 'sort' was AMONG the modified fields, a low-privilege
|
|
// (comment-only / read-only) member could smuggle arbitrary board mutations
|
|
// (members, permission, title, ...) into the same $set as sort and take over
|
|
// the board. Requiring 'sort' to be the SOLE modified field means this rule can
|
|
// never approve a modifier that also changes any other field.
|
|
export function canUpdateBoardSort(userId, board, fieldNames) {
|
|
const fields = fieldNames || [];
|
|
return !!userId && fields.length === 1 && fields[0] === 'sort' && allowIsBoardMember(userId, board);
|
|
}
|
|
|
|
// Issue #5998: the REST board-member endpoints historically took eight separate
|
|
// boolean permission flags. They now also accept a single named `role`, which
|
|
// this helper maps to that flag set. Returns null for an unknown role so callers
|
|
// can return a 400. All flags default to false; 'normal'/'member' means a plain
|
|
// board member (all flags false).
|
|
export const BOARD_MEMBER_ROLE_FLAGS = [
|
|
'isAdmin',
|
|
'isNoComments',
|
|
'isCommentOnly',
|
|
'isWorker',
|
|
'isNormalAssignedOnly',
|
|
'isCommentAssignedOnly',
|
|
'isReadOnly',
|
|
'isReadAssignedOnly',
|
|
];
|
|
|
|
export function boardMemberRoleToFlags(role) {
|
|
const flags = {};
|
|
BOARD_MEMBER_ROLE_FLAGS.forEach(flag => {
|
|
flags[flag] = false;
|
|
});
|
|
const normalized = String(role || '').trim().toLowerCase();
|
|
const roleMap = {
|
|
admin: 'isAdmin',
|
|
nocomments: 'isNoComments',
|
|
comment: 'isCommentOnly',
|
|
commentonly: 'isCommentOnly',
|
|
worker: 'isWorker',
|
|
normalassignedonly: 'isNormalAssignedOnly',
|
|
commentassignedonly: 'isCommentAssignedOnly',
|
|
readonly: 'isReadOnly',
|
|
readassignedonly: 'isReadAssignedOnly',
|
|
};
|
|
if (normalized === 'normal' || normalized === 'member') {
|
|
return flags;
|
|
}
|
|
if (Object.prototype.hasOwnProperty.call(roleMap, normalized)) {
|
|
flags[roleMap[normalized]] = true;
|
|
return flags;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Copy/Move endpoints accept a 0-based target position counted from the top
|
|
// (top-left), e.g. "place after N items". This converts that index into a
|
|
// numeric `sort` value placed between the existing siblings, so callers don't
|
|
// have to know WeKan's fractional sort scheme. `siblings` must be the
|
|
// destination items (EXCLUDING the item being moved) sorted ascending by sort.
|
|
export function computeSortForIndex(siblings, position) {
|
|
const list = Array.isArray(siblings) ? siblings : [];
|
|
if (list.length === 0) {
|
|
return 0;
|
|
}
|
|
const sortValue = item =>
|
|
typeof item.sort === 'number' && !Number.isNaN(item.sort) ? item.sort : 0;
|
|
const index = Number.isFinite(position) ? Math.max(0, Math.floor(position)) : list.length;
|
|
if (index <= 0) {
|
|
return sortValue(list[0]) - 1;
|
|
}
|
|
if (index >= list.length) {
|
|
return sortValue(list[list.length - 1]) + 1;
|
|
}
|
|
return (sortValue(list[index - 1]) + sortValue(list[index])) / 2;
|
|
}
|
|
|
|
// Issue #5819: merge label ids on a card — keep existing labels, drop the ones
|
|
// in removeLabelIds, add the ones in addLabelIds, de-duplicated and order-stable.
|
|
// Pure function so the bulk-labels behavior is unit-testable.
|
|
export function mergeLabelIds(currentLabelIds, addLabelIds = [], removeLabelIds = []) {
|
|
const current = Array.isArray(currentLabelIds) ? currentLabelIds : [];
|
|
const add = Array.isArray(addLabelIds) ? addLabelIds : [];
|
|
const removeSet = new Set(Array.isArray(removeLabelIds) ? removeLabelIds : []);
|
|
return Array.from(new Set([...current.filter(id => !removeSet.has(id)), ...add]));
|
|
}
|
|
|
|
// Issue #5998: a board member may only be assigned to a card (as member or
|
|
// assignee) when they are an active member of that card's board.
|
|
export function canAssignCardMember(board, userId) {
|
|
return !!board && !!userId && board.hasMember(userId);
|
|
}
|
|
|
|
// Issue #5846: a card date (received/start/due/end) is CLEARED when the request
|
|
// supplies an empty string, null, or the literal "null"; any other value SETS
|
|
// it. Pure so the add/remove-date behavior is unit-testable.
|
|
export function isCardDateClear(value) {
|
|
return value === '' || value === null || value === 'null';
|
|
}
|