Files
wekan/server/lib/orgTeamRestriction.js
T
Lauri Ojansivu f0ea1601b3 Admin Panel: the board-member restriction moves to Organizations and Teams, as two.
"Add board members only from the same Organization or Team" was one checkbox in the
Login pane - which is where neither of the two things it restricts lives. It is two
checkboxes now, each in the pane it is about:

  Admin Panel / People / Organizations   [_] Add board members only from the same
                                             Organization
  Admin Panel / People / Teams           [_] Add board members only from the same Team

A candidate may be added to a board when they share at least one ENABLED kind with
whoever adds them: an Organization when the first is on, a Team when the second is.
With both on that is "an Organization OR a Team" - exactly the rule the single
setting had - so an install that has the old field and neither new one is migrated
to both on first start, and nobody's restriction changes by upgrading. Ticking only
one is the new, narrower choice that was not expressible before.

  models/settings.js         boardMembersFromSameOrgOnly, boardMembersFromSameTeamOnly
  server/models/settings.js  the startup migration from the old single field
  server/lib/orgTeamRestriction.js
                             boardMemberRestriction(setting) reads the pair - and
                             still reads the old field, so an install behaves the
                             same before the migration runs; canAddUserToBoard()
                             takes a flag per kind (the old restrictEnabled still
                             means both)
  models/users.js            sharesOrgWith / sharesTeamWith, and
                             sharesRestrictedOrgOrTeamWith(user, restriction)

Enforced in the same three places as before - inviteUserToBoard, the user-search
typeahead that must not offer what the server would reject, and the imported-user
reconciliation - so the two settings cannot drift apart from what the UI says.

Thanks to xet7 !
2026-07-26 03:18:31 +03:00

77 lines
3.1 KiB
JavaScript

// Pure, dependency-free helpers for issue #6116:
// "Only add to board people from same Organization or Team".
//
// There are two admin settings now, one per kind, each shown beside the thing it is
// about - `boardMembersFromSameOrgOnly` in Admin Panel / People / Organizations and
// `boardMembersFromSameTeamOnly` in / Teams. A user may only be added to a board if
// they share at least one of the ENABLED kinds with the user performing the add.
// This module holds the pure decision logic so it can be unit-tested without any
// Meteor/Mongo runtime.
/**
* Return true iff the two arrays share at least one element (string ids).
* Null/undefined are treated as empty arrays.
*/
function hasIntersection(a, b) {
if (!Array.isArray(a) || !Array.isArray(b) || a.length === 0 || b.length === 0) {
return false;
}
const setB = new Set(b);
return a.some(item => setB.has(item));
}
/**
* The two restrictions, read off the admin Settings document. They live in Admin
* Panel / People / Organizations and / Teams - one checkbox each, beside the thing
* it is about - and replace the single "same Organization OR Team" setting, which
* is still read here so an install that has not been migrated yet behaves the same.
*/
export function boardMemberRestriction(setting) {
const legacy = !!(setting && setting.boardMembersFromSameOrgOrTeamOnly);
return {
org: !!(setting && setting.boardMembersFromSameOrgOnly) || legacy,
team: !!(setting && setting.boardMembersFromSameTeamOnly) || legacy,
};
}
/**
* Decide whether a candidate user may be added to a board.
*
* @param {Object} opts
* @param {boolean} opts.restrictOrgEnabled "Only from the same Organization".
* @param {boolean} opts.restrictTeamEnabled "Only from the same Team".
* @param {boolean} opts.restrictEnabled The setting these two replace: the
* same as enabling both.
* @param {string[]} opts.adderOrgs Org ids of the user performing the add
* (or the union of board members' orgs).
* @param {string[]} opts.adderTeams Team ids of the adder / board members.
* @param {string[]} opts.candidateOrgs Org ids of the user being added.
* @param {string[]} opts.candidateTeams Team ids of the user being added.
* @return {boolean} true when the add is allowed.
*/
export function canAddUserToBoard({
restrictOrgEnabled,
restrictTeamEnabled,
restrictEnabled,
adderOrgs,
adderTeams,
candidateOrgs,
candidateTeams,
} = {}) {
const org = !!restrictOrgEnabled || !!restrictEnabled;
const team = !!restrictTeamEnabled || !!restrictEnabled;
// Neither restriction => preserve the unrestricted behaviour.
if (!org && !team) {
return true;
}
// Allow when the candidate shares at least one of the ENABLED kinds. With both
// enabled that is "a shared Org OR a shared Team" - the rule the single setting
// had - and with one enabled it is only that one.
return (
(org && hasIntersection(adderOrgs, candidateOrgs)) ||
(team && hasIntersection(adderTeams, candidateTeams))
);
}
export default canAddUserToBoard;