Files
wekan/models/lib/boardRoleCapabilities.js
T
Lauri Ojansivu efa534fddf Board roles: one capability table, three roles fixed, and a Roles Status pane that shows it.
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 !
2026-07-31 04:09:41 +03:00

117 lines
5.1 KiB
JavaScript

'use strict';
// What each board role may do — the ONE table, in code.
//
// It used to be spelled out three times: once in the server allow helpers
// (server/lib/utils.js), once in the client's canModify* helpers
// (client/lib/utils.js), and once in prose in the docs. The three had drifted, and
// each place they disagreed was a role that did not do what its name says. So the
// matrix lives here, everything else reads it, and there is nowhere for a fourth
// opinion to appear.
//
// Read by: server/lib/utils.js (the authority), client/lib/utils.js (what the UI
// offers), Admin Panel / People / Roles (the Roles Status table), and
// docs/Features/Members/Roles.md through tests/boardRoles.test.cjs.
//
// Pure: no Meteor, no DOM, no database.
// The roles, in the order they are shown to a person: most power first.
const BOARD_ROLES = [
'board-admin',
'normal',
'normal-assigned-only',
'no-comments',
'comment-only',
'comment-assigned-only',
'worker',
'read-only',
'read-assigned-only',
];
// The member flag that names each role. 'normal' is the absence of all of them.
const ROLE_FLAGS = {
'board-admin': 'isAdmin',
'normal-assigned-only': 'isNormalAssignedOnly',
'no-comments': 'isNoComments',
'comment-only': 'isCommentOnly',
'comment-assigned-only': 'isCommentAssignedOnly',
'worker': 'isWorker',
'read-only': 'isReadOnly',
'read-assigned-only': 'isReadAssignedOnly',
};
// The capabilities, one column each.
//
// seesAllCards false = only the cards the member is assigned to. Enforced by
// the card publications, not by an allow rule.
// comment add a card comment or a comment reaction.
// write create and edit cards, lists, swimlanes, checklists — and MOVE
// a card, because a move is a card update and goes through the
// same rule.
// manageBoard the board's settings, its members and their roles.
const CAPABILITIES = ['seesAllCards', 'comment', 'write', 'manageBoard'];
const ROLE_CAPABILITIES = {
'board-admin': { seesAllCards: true, comment: true, write: true, manageBoard: true },
'normal': { seesAllCards: true, comment: true, write: true, manageBoard: false },
'normal-assigned-only': { seesAllCards: false, comment: true, write: true, manageBoard: false },
// "No comments" is the one that blocks COMMENTING. It used to block writing as
// well, which made it a second read-only role under a name that says otherwise.
'no-comments': { seesAllCards: true, comment: false, write: true, manageBoard: false },
'comment-only': { seesAllCards: true, comment: true, write: false, manageBoard: false },
// ...and this is "comment only" plus the assigned-cards restriction. It used to
// have full write access, because nothing outside the card publications read
// its flag — so it was really "normal, assigned only" under another name.
'comment-assigned-only': { seesAllCards: false, comment: true, write: false, manageBoard: false },
// A Worker is meant to "move card, assign himself to card and comment" (the
// board schema's own words). A move IS a card update, so `write: false` blocks
// it — the role currently cannot do the one thing it is for. Left as it is
// rather than quietly widened: letting a role write SOME fields of a card is a
// field-level policy and a security decision, not a table edit. Recorded under
// "Known gaps" in docs/Features/Members/Roles.md.
'worker': { seesAllCards: true, comment: true, write: false, manageBoard: false },
'read-only': { seesAllCards: true, comment: false, write: false, manageBoard: false },
'read-assigned-only': { seesAllCards: false, comment: false, write: false, manageBoard: false },
};
// The role of ONE member document. The order matters and matches
// Board.memberRole(): a member carries a single flag in practice
// (setMemberPermission writes all of them at once), but the REST API can set
// several, and then the strongest wins — `isAdmin` above all, which is why every
// other flag on an admin is ignored here just as it is in Board.hasCommentOnly()
// and the rest of the has*() helpers.
function memberRoleOf(member) {
if (!member || member.isActive !== true) return null;
if (member.isAdmin) return 'board-admin';
for (const role of BOARD_ROLES) {
const flag = ROLE_FLAGS[role];
if (flag && flag !== 'isAdmin' && member[flag]) return role;
}
return 'normal';
}
// May a member with this role do this? An unknown role or capability is `false`:
// a permission question with no answer is a no.
function roleCan(role, capability) {
const caps = ROLE_CAPABILITIES[role];
if (!caps) return false;
return caps[capability] === true;
}
// May THIS member of THIS board do it? `members` is board.members.
function memberCan(members, userId, capability) {
if (!userId || !Array.isArray(members)) return false;
const member = members.find(m => m && m.userId === userId && m.isActive === true);
return roleCan(memberRoleOf(member), capability);
}
module.exports = {
BOARD_ROLES,
ROLE_FLAGS,
CAPABILITIES,
ROLE_CAPABILITIES,
memberRoleOf,
roleCan,
memberCan,
};