Files
wekan/models/lib/groupBoardMembership.js
T
Lauri Ojansivu 50bb989eb8 Custom board roles, and team memberships that leave with the team.
#3022 (decided 2026-10-14): a site admin builds named roles in Admin
Panel / People / Roles from the capability matrix - sees all cards,
comments, edits, moves cards, invites - and a board admin gives one to a
member in the same popup as the built-in roles. Board admin stays
built-in. Every permission check asks boardRoleCapabilities, which
answers a custom role from the role; the member's built-in flags carry
the strongest built-in role that grants nothing more (fallbackRoleFor,
checked for every combination), so code reading a flag never grants
more, and a deleted role leaves exactly that. Where a role is asked by
name (hasWorker, isCommentOnly, ...) a custom role reads as the built-in
role that does what it does; commenting is asked as a capability
everywhere. Invites, REST whole-board reads, imports (a foreign custom
role is dropped) and member mapping (all eight flags now, not four)
follow it. The six new strings are translated in all 234 locales.

#4178 (decided 2026-10-14): a team or organization that gives board
memberships takes them away again when the user leaves it - only those:
each records where it came from (grantedBy), a direct membership is
never touched, a board admin is kept, and a board admin's own add or
removal is final. Teams through user edits, LDAP, OIDC and the board
sidebar; organizations where they propagate members.

Also: the Roles Status table gets a Move cards column; the comment form,
reactions and chat ask the comment capability (a No comments member was
offered reactions the server refused).

Tests: customBoardRoles and groupBoardMembership node suites, and the
runtime suites (9 passing) against a real database.

Thanks to xet7 and mfnalex !

Fixes #3022,
Fixes #4178.
2026-10-14 08:20:49 +03:00

119 lines
4.7 KiB
JavaScript

'use strict';
// Where a board membership came from, so that leaving a team or an
// organization removes what it gave and nothing else (#4178, maintainer
// decision 2026-10-14). Pure: tested in tests/groupBoardMembership.test.cjs.
//
// A member entry that a team or an organization created carries
// `grantedBy: ['team:<id>', 'org:<id>', ...]`. An entry WITHOUT grantedBy is a
// direct membership - an invitation, a board admin's choice, or any entry from
// before this existed - and a group never touches it.
//
// gaining a group not a member yet -> new entry, grantedBy [group]
// member through groups -> group added (and active)
// direct member -> untouched
// losing a group member through it -> group removed; with no
// group left, deactivated
// ...but a board admin -> kept, now direct: a board
// must not lose its admin
// to a team change
// direct member -> untouched
//
// A board admin's own decisions make an entry direct: adding a member who is
// there through a team, or removing one (models/boards.js addMember and
// removeMember drop grantedBy), so a later team change does not undo them.
const ROLE_FLAGS_OFF = {
isAdmin: false,
isNoComments: false,
isCommentOnly: false,
isWorker: false,
isNormalAssignedOnly: false,
isCommentAssignedOnly: false,
isReadOnly: false,
isReadAssignedOnly: false,
};
function grantOf(kind, id) {
return `${kind}:${id}`;
}
function isGroupDerived(member) {
return !!member && Array.isArray(member.grantedBy);
}
/**
* What gaining `grant` does to this user's entry in `members`.
* @return {{action: 'push', entry: object} | {action: 'addGrant', index: number}
* | {action: 'none'}}
*/
function planGain(members, userId, grant) {
const list = Array.isArray(members) ? members : [];
const index = list.findIndex(m => m && m.userId === userId);
if (index < 0) {
return { action: 'push', entry: { userId, isActive: true, ...ROLE_FLAGS_OFF, grantedBy: [grant] } };
}
const member = list[index];
if (!isGroupDerived(member)) return { action: 'none' };
if (member.isActive === true && member.grantedBy.includes(grant)) return { action: 'none' };
return { action: 'addGrant', index };
}
/**
* What losing `grant` does to this user's entry in `members`.
* @return {{action: 'pullGrant', index: number} | {action: 'deactivate', index: number}
* | {action: 'makeDirect', index: number} | {action: 'none'}}
*/
function planLoss(members, userId, grant) {
const list = Array.isArray(members) ? members : [];
const index = list.findIndex(m => m && m.userId === userId);
if (index < 0) return { action: 'none' };
const member = list[index];
if (!isGroupDerived(member) || !member.grantedBy.includes(grant)) return { action: 'none' };
const remaining = member.grantedBy.filter(g => g !== grant);
if (remaining.length) return { action: 'pullGrant', index };
if (member.isAdmin === true) return { action: 'makeDirect', index };
return { action: 'deactivate', index };
}
/** The Mongo update that carries out a plan on one board. */
function updateFor(plan, grant) {
switch (plan.action) {
case 'push':
return { $push: { members: plan.entry } };
case 'addGrant':
return {
$set: { [`members.${plan.index}.isActive`]: true },
$addToSet: { [`members.${plan.index}.grantedBy`]: grant },
};
case 'pullGrant':
return { $pull: { [`members.${plan.index}.grantedBy`]: grant } };
case 'deactivate':
// Kept, with an empty list: still group-derived, so gaining a group
// again brings the member back, while a board admin's removal (which
// drops grantedBy) is final.
return { $set: { [`members.${plan.index}.isActive`]: false, [`members.${plan.index}.grantedBy`]: [] } };
case 'makeDirect':
return { $unset: { [`members.${plan.index}.grantedBy`]: '' } };
default:
return null;
}
}
function groupIdsOf(list, idField) {
if (!Array.isArray(list)) return [];
return [...new Set(list.map(g => g && g[idField]).filter(id => typeof id === 'string' && id))];
}
/** The group ids gained and lost between two user.teams / user.orgs lists. */
function groupChanges(oldList, newList, idField) {
const before = new Set(groupIdsOf(oldList, idField));
const after = new Set(groupIdsOf(newList, idField));
return {
gained: [...after].filter(id => !before.has(id)),
lost: [...before].filter(id => !after.has(id)),
};
}
module.exports = { grantOf, isGroupDerived, planGain, planLoss, updateFor, groupChanges, ROLE_FLAGS_OFF };