Files
wekan/models/lib/tenantAdmin.js
T
Lauri Ojansivu f15d0c0cba Multitenancy: the three tenant modules must be ES modules.
Every app file is an ES module in Meteor, so `module.exports = {…}` in one of
them throws the moment the CLIENT bundle loads it:

  Uncaught Error: ES Modules may not assign module.exports or exports.*,
  Use ESM export syntax, instead: ./models/lib/tenantAdmin.js

and because it throws, every module after it in the client graph never runs -
models/users.js imports tenantAdmin, so the whole client went down with it.

models/lib/tenants.js, tenantAdmin.js and tenantBackup.js now export with ESM
syntax, and every importer uses import instead of require(). The .cjs unit tests
load them with a dynamic import(), the way tests/cardUrl.test.cjs already loads
its module.

tests/tenantWiring.test.cjs guards both halves, so the same crash cannot come
back quietly.

Thanks to xet7 !
2026-07-26 07:59:30 +03:00

250 lines
10 KiB
JavaScript

// Per-tenant Global Admins — the admin half of multitenancy option D
// (docs/Design/Multitenancy/Multitenancy.md).
//
// WeKan has ONE Global Admin flag today (`user.isAdmin`), and it can do everything
// to everything. With Organizations as tenants there has to be a second, smaller
// kind: someone who administers ONE Organization — its people and its backups —
// and nothing else. That is a per-org flag on the membership the user already has:
//
// user.orgs = [ { orgId, orgDisplayName, isAdmin: true } ]
//
// so no new collection, no new membership list, and every place that already reads
// `user.orgs` keeps working.
//
// The rules are pure and dependency-free here so they can be unit-tested without a
// Meteor/Mongo runtime (tests/tenantAdmin.test.cjs) and so the SAME decision runs
// on the client (which menu entries to draw) and on the server (which is the one
// that counts). A client-side check is a convenience; every method and publication
// asks these functions again.
//
// The deliberate limits, so nothing here reads as more isolation than it is:
// * a per-tenant admin can NEVER grant `isAdmin` (the site-wide flag), and can
// never manage a user who has it;
// * a per-tenant admin only sees the panes that are about their own tenant;
// * everything instance-wide (Settings, Problems, Login, E-mail, Domains, Roles,
// Shared templates, Locked users, the storages) stays site-admin only.
// ── who is what ──────────────────────────────────────────────────────────────
// The site-wide Global Admin — `user.isAdmin`, unchanged.
function isSiteAdmin(user) {
return !!(user && user.isAdmin === true);
}
// Ids of the orgs this user is a per-tenant Global Admin of.
function adminOrgIds(user) {
if (!user || !Array.isArray(user.orgs)) return [];
const out = [];
user.orgs.forEach(org => {
if (org && org.isAdmin === true && org.orgId && !out.includes(org.orgId)) {
out.push(org.orgId);
}
});
return out;
}
// Ids of every org this user belongs to, admin or not.
function memberOrgIds(user) {
if (!user || !Array.isArray(user.orgs)) return [];
const out = [];
user.orgs.forEach(org => {
if (org && org.orgId && !out.includes(org.orgId)) out.push(org.orgId);
});
return out;
}
function isOrgAdmin(user, orgId) {
return !!orgId && adminOrgIds(user).includes(orgId);
}
// A per-tenant Global Admin of at least one org (and not the site admin, who is
// handled by every rule below on their own terms).
function isTenantAdmin(user) {
return adminOrgIds(user).length > 0;
}
// The Admin Panel opens for the site admin and for a per-tenant admin. Everyone
// else still gets "error-notAuthorized", exactly as before.
function canOpenAdminPanel(user) {
return isSiteAdmin(user) || isTenantAdmin(user);
}
// The orgs whose settings, people and backups this user may touch.
// `null` means EVERY org (the site admin) — a value the callers below must not
// confuse with the empty array, which means none.
function manageableOrgIds(user) {
if (isSiteAdmin(user)) return null;
return adminOrgIds(user);
}
function canManageOrg(user, orgId) {
if (isSiteAdmin(user)) return true;
return isOrgAdmin(user, orgId);
}
// May `actor` administer `target` (edit, lock, delete, appoint)?
// * the site admin may administer anyone;
// * a per-tenant admin may administer a member of an org they administer, but
// NEVER a site admin — otherwise a tenant admin could lock out or take over the
// instance owner, which is a privilege escalation, not tenancy;
// * nobody else may administer anyone.
function canManageUser(actor, target) {
if (!target) return false;
if (isSiteAdmin(actor)) return true;
if (isSiteAdmin(target)) return false;
const mine = adminOrgIds(actor);
if (mine.length === 0) return false;
return memberOrgIds(target).some(orgId => mine.includes(orgId));
}
// Appointing (or dismissing) a per-tenant Global Admin of `orgId`. The site admin
// may do it anywhere; a per-tenant admin may do it inside their own org, which is
// what makes a tenant self-administering.
function canSetOrgAdmin(actor, orgId) {
return canManageOrg(actor, orgId);
}
// ── scoping the queries ──────────────────────────────────────────────────────
// A selector that never matches anything. Used for a caller with no rights, so a
// publication returns an empty set rather than accidentally returning everything.
const MATCH_NOTHING = { _id: '-no-such-document' };
function isPlainObject(value) {
return !!value && typeof value === 'object' && !Array.isArray(value);
}
// Merge a caller-supplied query with a mandatory restriction, so the restriction
// cannot be argued away by a crafted query: both must hold ($and), and the
// restriction is added even when the query already mentions the same field.
function andQuery(query, restriction) {
const q = isPlainObject(query) ? query : {};
if (Object.keys(q).length === 0) return { ...restriction };
return { $and: [q, restriction] };
}
// Admin Panel / People / People, scoped: the site admin sees every user; a
// per-tenant admin sees the members of the orgs they administer; anyone else sees
// nobody.
function peopleScopeSelector(user, query) {
if (isSiteAdmin(user)) return isPlainObject(query) ? { ...query } : {};
const ids = adminOrgIds(user);
if (ids.length === 0) return { ...MATCH_NOTHING };
return andQuery(query, { 'orgs.orgId': { $in: ids } });
}
// Admin Panel / People / Organizations, scoped the same way.
function orgScopeSelector(user, query) {
if (isSiteAdmin(user)) return isPlainObject(query) ? { ...query } : {};
const ids = adminOrgIds(user);
if (ids.length === 0) return { ...MATCH_NOTHING };
return andQuery(query, { _id: { $in: ids } });
}
// ── which menu entries ───────────────────────────────────────────────────────
// The Admin Panel panes a per-tenant admin may open. Everything else is about the
// instance rather than about one tenant, so it stays site-admin only. These are the
// EXISTING menu ids — a tenant admin gets the same menus, shorter.
const TENANT_ADMIN_PEOPLE_PANES = ['people-setting', 'org-setting'];
const TENANT_ADMIN_ATTACHMENT_PANES = ['backup'];
// Settings holds one pane a tenant may have: Visibility, and inside it only the
// "Change color" section - the site theme, which is a per-Organization override on
// a tenant host (docs/Theme/Theme.md). Everything else in Settings, and every other
// Settings pane, is instance-wide and stays site-admin only.
const TENANT_ADMIN_SETTINGS_PANES = ['tableVisibilityMode-setting'];
// The tabs of the Admin Panel top bar a tenant admin sees. Problems is
// instance-wide; Settings is offered only for the pane above.
const TENANT_ADMIN_TABS = ['people', 'attachments', 'settings'];
// Keep only the entries a tenant admin may open. The site admin keeps the whole
// menu; a user who is neither gets nothing (their page shows not-authorized
// anyway). Separators and headings that would be left dangling are dropped, so a
// shortened menu does not render a heading with nothing under it.
function filterMenuItems(items, user, allowedIds) {
const list = Array.isArray(items) ? items : [];
if (isSiteAdmin(user)) return list.slice();
if (!isTenantAdmin(user)) return [];
const allowed = new Set(Array.isArray(allowedIds) ? allowedIds : []);
const kept = list.filter(item => item && item.id && allowed.has(item.id));
return kept;
}
function tenantAdminPeopleMenu(items, user) {
return filterMenuItems(items, user, TENANT_ADMIN_PEOPLE_PANES);
}
function tenantAdminAttachmentsMenu(items, user) {
return filterMenuItems(items, user, TENANT_ADMIN_ATTACHMENT_PANES);
}
function tenantAdminSettingsMenu(items, user) {
return filterMenuItems(items, user, TENANT_ADMIN_SETTINGS_PANES);
}
// Where does "Change color" in Admin Panel / Settings / Visibility write?
// * the site admin sets the site theme -> the instance settings document;
// * a per-tenant Global Admin sets their own Organization's -> that org.
// `tenantOrgId` is the Organization of the host the admin is looking at, when there
// is one: an admin of several Organizations editing one of their tenant hosts means
// THAT Organization, and otherwise the first one they administer. Returns
// { scope: 'instance' } | { scope: 'org', orgId } | null (may not set a theme)
function themeTarget(user, tenantOrgId) {
if (isSiteAdmin(user)) return { scope: 'instance' };
const mine = adminOrgIds(user);
if (!mine.length) return null;
if (tenantOrgId && mine.includes(tenantOrgId)) return { scope: 'org', orgId: tenantOrgId };
return { scope: 'org', orgId: mine[0] };
}
function canOpenTab(user, tab) {
if (isSiteAdmin(user)) return true;
if (!isTenantAdmin(user)) return false;
return TENANT_ADMIN_TABS.includes(tab);
}
// ── writes a tenant admin may not make ───────────────────────────────────────
// Strip from a user update everything a per-tenant admin must not be able to set.
// `isAdmin` is the site-wide flag: letting a tenant admin write it would let a
// tenant appoint an instance owner. The site admin's updates pass through
// unchanged.
function sanitizeUserFields(actor, fields) {
const input = isPlainObject(fields) ? fields : {};
if (isSiteAdmin(actor)) return { ...input };
const out = { ...input };
delete out.isAdmin;
return out;
}
// ESM exports: every app file is an ES module in Meteor, so a `module.exports`
// assignment throws the moment the CLIENT bundle loads it. The .cjs unit tests read
// this file with a dynamic `import()`, the way the other pure modules are tested.
export {
MATCH_NOTHING,
TENANT_ADMIN_ATTACHMENT_PANES,
TENANT_ADMIN_PEOPLE_PANES,
TENANT_ADMIN_SETTINGS_PANES,
TENANT_ADMIN_TABS,
adminOrgIds,
andQuery,
canManageOrg,
canManageUser,
canOpenAdminPanel,
canOpenTab,
canSetOrgAdmin,
filterMenuItems,
isOrgAdmin,
isSiteAdmin,
isTenantAdmin,
manageableOrgIds,
memberOrgIds,
orgScopeSelector,
peopleScopeSelector,
sanitizeUserFields,
tenantAdminAttachmentsMenu,
tenantAdminPeopleMenu,
tenantAdminSettingsMenu,
themeTarget,
};