Files
wekan/server/customBoardRoles.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

130 lines
5.6 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import Boards from '/models/boards';
import CustomBoardRoles, { CUSTOM_ROLE_NAME_MAX } from '/models/customBoardRoles';
import { allowIsBoardAdminOrSiteAdmin } from '/server/lib/utils';
// Custom board roles (#3022), the server half: the copy every permission check
// reads, the publication, and the only ways to change them.
//
// customBoardRoles.save site admin: create or change a role
// customBoardRoles.remove site admin: delete one
// boards.setMemberCustomRole board admin: give a member a custom role
//
// A member's custom role is mirrored into the old per-role flags as the
// strongest built-in role that grants nothing more (fallbackRoleFor). Saving a
// role rewrites that mirror on every board where it is used, so the two never
// disagree; deleting one leaves members on the mirror, which is a subset.
const {
setCustomRoleLookup,
normalizeCustomCapabilities,
fallbackRoleFor,
flagsForRole,
} = require('/models/lib/boardRoleCapabilities');
// The copy permission checks read. They are synchronous (allow rules), so the
// roles are kept in memory and current from the collection.
const roles = new Map();
setCustomRoleLookup(id => roles.get(id) || null);
Meteor.startup(async () => {
await CustomBoardRoles.find({}, { fields: { name: 1, capabilities: 1 } }).observeChangesAsync({
added: (id, fields) => roles.set(id, { _id: id, ...fields }),
changed: (id, fields) => roles.set(id, { ...(roles.get(id) || { _id: id }), ...fields }),
removed: id => roles.delete(id),
});
});
Meteor.publish('customBoardRoles', function () {
if (!this.userId) return this.ready();
return CustomBoardRoles.find({}, { fields: { name: 1, capabilities: 1, sort: 1 } });
});
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');
}
// The flags and customRoleId of one member, for a custom role (or null to
// return to a built-in role, which setMemberPermission writes).
function memberFields(index, roleId, capabilities) {
const fields = {};
const flags = flagsForRole(fallbackRoleFor(capabilities));
for (const [flag, value] of Object.entries(flags)) fields[`members.${index}.${flag}`] = value;
fields[`members.${index}.customRoleId`] = roleId;
return fields;
}
// Rewrite the mirrored flags of every member carrying this role.
async function refreshMembersOf(roleId, capabilities) {
const boards = await Boards.find({ 'members.customRoleId': roleId }, { fields: { members: 1 } }).fetchAsync();
for (const board of boards) {
const $set = {};
board.members.forEach((member, index) => {
if (member && member.customRoleId === roleId) Object.assign($set, memberFields(index, roleId, capabilities));
});
if (Object.keys($set).length) await Boards.direct.updateAsync(board._id, { $set });
}
}
Meteor.methods({
async 'customBoardRoles.save'(roleId, input) {
check(roleId, Match.OneOf(String, null));
check(input, { name: String, capabilities: Object });
await requireSiteAdmin(this.userId);
const name = input.name.trim();
if (!name || name.length > CUSTOM_ROLE_NAME_MAX) throw new Meteor.Error('invalid-name');
const capabilities = normalizeCustomCapabilities(input.capabilities);
delete capabilities.manageBoard;
if (roleId) {
const existing = await CustomBoardRoles.findOneAsync(roleId);
if (!existing) throw new Meteor.Error('not-found');
await CustomBoardRoles.updateAsync(roleId, { $set: { name, capabilities } });
await refreshMembersOf(roleId, capabilities);
return roleId;
}
const sort = await CustomBoardRoles.find().countAsync();
return CustomBoardRoles.insertAsync({ name, capabilities, sort });
},
async 'customBoardRoles.remove'(roleId) {
check(roleId, String);
await requireSiteAdmin(this.userId);
// Members keep the mirrored built-in role; memberRoleOf ignores an id
// that no longer names a role, so nothing else has to change.
await CustomBoardRoles.removeAsync(roleId);
return true;
},
async 'boards.setMemberCustomRole'(boardId, memberId, roleId) {
check(boardId, String);
check(memberId, String);
check(roleId, String);
if (!this.userId) throw new Meteor.Error('not-authorized');
const board = await Boards.findOneAsync(boardId);
// The same who as every other board change: its admins, and a site admin
// (server/permissions/boards.js).
if (!board || !(await allowIsBoardAdminOrSiteAdmin(this.userId, board))) throw new Meteor.Error('not-authorized');
const role = await CustomBoardRoles.findOneAsync(roleId);
if (!role) throw new Meteor.Error('not-found');
const index = board.members.findIndex(m => m && m.userId === memberId);
if (index < 0) throw new Meteor.Error('not-a-member');
const member = board.members[index];
// A custom role is never a board admin. A board admin cannot take that
// from themselves here (as with setMemberPermission), and the last
// active admin cannot lose it, or nobody could manage the board.
if (member.isAdmin) {
if (memberId === this.userId) throw new Meteor.Error('cannot-demote-self');
const admins = board.members.filter(m => m && m.isActive && m.isAdmin);
if (admins.length <= 1) throw new Meteor.Error('last-board-admin');
}
await Boards.direct.updateAsync(boardId, {
$set: memberFields(index, roleId, normalizeCustomCapabilities(role.capabilities)),
});
return true;
},
});
export { refreshMembersOf, memberFields };