@i4e/invest4edu-access-core 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@i4e/invest4edu-access-core",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Shared access-control primitives (Track 3: tenant keystone, role capabilities, reportee tree, feature flags) for NeoFindesk backends.",
5
5
  "type": "module",
6
6
  "exports": {
package/src/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * Tenant isolation (D1 keystone) + role capabilities (G7).
4
4
  */
5
5
  export { default as als, runWithTenant, runAsSystem, getTenantStore } from "./tenant-context.js";
6
- export { default as tenantPlugin } from "./tenant-plugin.js";
6
+ export { default as tenantPlugin, setTenantMode, getTenantMode } from "./tenant-plugin.js";
7
7
  export {
8
8
  CAPABILITY_FLAGS,
9
9
  DEFAULT_ROLE_CAPABILITIES,
@@ -20,7 +20,27 @@
20
20
  import als from "./tenant-context.js";
21
21
 
22
22
  const READ_OPS = ["find", "findOne", "countDocuments"];
23
- const mode = () => (process.env.TENANT_ENFORCEMENT || "off").toLowerCase();
23
+ const VALID = new Set(["off", "warn", "enforce"]);
24
+
25
+ // Enforcement mode is resolvable from TWO places, so it can be flipped from the admin UI
26
+ // (DB) WITHOUT a restart, while the env var stays the infra-level emergency override.
27
+ // Precedence: env TENANT_ENFORCEMENT (if valid) > DB value (setTenantMode) > "off"
28
+ let dbMode = null;
29
+
30
+ /** Called by the backend from the `accessconfig` DB doc so a UI toggle takes effect (~refresh). */
31
+ export function setTenantMode(m) {
32
+ const v = m ? String(m).toLowerCase() : "";
33
+ dbMode = VALID.has(v) ? v : null;
34
+ }
35
+
36
+ /** Effective mode. */
37
+ export function getTenantMode() {
38
+ const env = (process.env.TENANT_ENFORCEMENT || "").toLowerCase();
39
+ if (VALID.has(env)) return env; // explicit env wins (emergency / infra)
40
+ return dbMode || "off";
41
+ }
42
+
43
+ const mode = getTenantMode;
24
44
 
25
45
  function tenantHook() {
26
46
  if (mode() === "off") return; // literal no-op — identical to pre-Track-3