@i4e/invest4edu-access-core 0.43.0 → 0.43.1

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.43.0",
3
+ "version": "0.43.1",
4
4
  "description": "Shared access-control primitives for NeoFindesk: tenant keystone, role capabilities, reportee tree, entity/role vocabulary, mobile app identity, feature flags, and the unified access engine (registry schema, snapshot resolver, visibleWhen).",
5
5
  "type": "module",
6
6
  "exports": {
@@ -37,7 +37,7 @@ const UCC_RULES_TTL_MS = 60_000;
37
37
  * distributors); without it an employee is never treated as already seeing a client.
38
38
  * `toId` converts ids to the stored type (ObjectId); identity by default.
39
39
  */
40
- export function createClientProfileService({ getDb, getSettings, getSideContext = () => ({}), getVisibilitySets = null, getFieldPolicyIndex = () => new Map(), toId = (v) => v, onAudit = () => {}, emit = null, now = () => new Date() }) {
40
+ export function createClientProfileService({ getDb, getSettings, getSideContext = () => ({}), getVisibilitySets = null, getFieldPolicyIndex = () => new Map(), toId = (v) => v, onAudit = () => {}, emit = null, now = () => new Date(), getSessionContext = () => null }) {
41
41
  const db = () => {
42
42
  const handle = getDb?.();
43
43
  if (!handle) throw new Error("client-profile service: no database handle");
@@ -144,7 +144,15 @@ export function createClientProfileService({ getDb, getSettings, getSideContext
144
144
  if (!id) return null;
145
145
  const user = await db().collection("users").findOne({ _id: id }, { session, projection: { distributor_id: 1, client_id: 1, employee_id: 1, full_record_access: 1 } });
146
146
  if (!user) return null;
147
- const roles = await roleNamesOf(id, session);
147
+ // The SESSION decides, not the stored roles: a person who is both a partner and a client has one
148
+ // user record, and their app login is pinned to the client role in its token while their
149
+ // portal login carries the partner role. The backend injects `getSessionContext` (its request's
150
+ // tenant context: the verified token's userId + roles — getTenantStore from
151
+ // @i4e/invest4edu-access-core/tenant-context, kept out of this module so browser code that
152
+ // imports client-profile never pulls in async_hooks); used when it is the user being resolved.
153
+ const ctx = getSessionContext?.() ?? null;
154
+ const sessionRoles = ctx && ctx.userId != null && String(ctx.userId) === String(userId) && Array.isArray(ctx.roles) && ctx.roles.length ? ctx.roles : null;
155
+ const roles = sessionRoles ?? await roleNamesOf(id, session);
148
156
  const capabilityMap = buildCapabilityMap(await db().collection("rolecapabilities").find({}, { session }).toArray());
149
157
  const isClientApp = Boolean(user.client_id) && roles.length > 0 && roles.every((r) => CLIENT_APP_ROLE_NAMES.includes(r));
150
158
  return {