mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
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 !
250 lines
10 KiB
JavaScript
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: ' |