mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
fix: preserve account access compatibility
This commit is contained in:
1 parent
e4e773d543
commit
4923db6464
23 files changed
+288
-14
No files matched your search
@@ -32,6 +32,7 @@ const OWNER = {
|
||||
access: "write" as const,
|
||||
level: "write" as const,
|
||||
sections: null,
|
||||
recordKind: "shared" as const,
|
||||
canWrite: true,
|
||||
};
|
||||
|
||||
|
||||
@@ -220,15 +220,20 @@ const accountAccessEntry = z
|
||||
username: z.string(),
|
||||
displayName: z.string().nullable(),
|
||||
access: z
|
||||
.enum(["read", "write", "manage"])
|
||||
.enum(["read", "write"])
|
||||
.describe(
|
||||
"The grant's resolved level, under the name v1.36.0 published. Identical to `level` — both come from one server-side expression — and kept because shipped clients read it. New code reads `level`.",
|
||||
"The legacy grant level that shipped clients decode. MANAGE serializes as `write` here; new code reads canonical `level`.",
|
||||
),
|
||||
level: z
|
||||
.enum(["read", "write", "manage"])
|
||||
.describe(
|
||||
"The grant's resolved level. `manage` additionally admits changing and removing entries on the delegable surfaces; it never reaches the identity surfaces (settings, connections, tokens, consent, grant management), which stay refused on an invited record at every level. Render it; never derive it from `canWrite`.",
|
||||
),
|
||||
recordKind: z
|
||||
.enum(["self", "shared", "managed"])
|
||||
.describe(
|
||||
"Server-resolved presentation metadata for the record. It is never accepted as an authorization input.",
|
||||
),
|
||||
sections: z
|
||||
.array(shareSection)
|
||||
.nullable()
|
||||
@@ -257,6 +262,11 @@ export const accountAccessBlock = z
|
||||
.describe(
|
||||
"The record this session is inside right now, resolved to a full entry rather than an id to look up — so a banner can name the person without joining two fields. Null when the caller is in their own record, and ALWAYS null on the Bearer transport: a token carries its selector per request and this endpoint refuses one.",
|
||||
),
|
||||
recordKind: z
|
||||
.enum(["self", "shared", "managed"])
|
||||
.describe(
|
||||
"The server-resolved kind of the record currently in view. `self` applies when `active` is null.",
|
||||
),
|
||||
canSwitch: z
|
||||
.boolean()
|
||||
.describe("Whether there is anywhere to switch to. Bind it directly."),
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
accountGrant: {
|
||||
findMany: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import { prisma } from "@/lib/db";
|
||||
import { resolveAccountAccess } from "../account-access";
|
||||
import { decodeLegacyWholeRecordAccess } from "../account-access-view";
|
||||
import { LEGACY_ACCOUNT_PAYLOADS } from "../../../../tests/fixtures/v137/legacy-account-payloads";
|
||||
|
||||
function activeGrant({
|
||||
access,
|
||||
managedProfileAt = null,
|
||||
}: {
|
||||
access: "READ" | "WRITE" | "MANAGE";
|
||||
managedProfileAt?: Date | null;
|
||||
}) {
|
||||
return {
|
||||
id: `grant-${access.toLowerCase()}`,
|
||||
access,
|
||||
scopeJson: null,
|
||||
acceptedAt: new Date("2026-08-01T00:00:00.000Z"),
|
||||
revokedAt: null,
|
||||
expiresAt: null,
|
||||
grantor: {
|
||||
id: "record-owner",
|
||||
username: "record-owner",
|
||||
displayName: "Record owner",
|
||||
managedProfileAt,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("resolveAccountAccess", () => {
|
||||
it("keeps MANAGE compatible with legacy access while publishing its canonical level", async () => {
|
||||
vi.mocked(prisma.accountGrant.findMany).mockResolvedValue([
|
||||
activeGrant({
|
||||
access: "MANAGE",
|
||||
managedProfileAt: new Date("2026-08-01T00:00:00.000Z"),
|
||||
}),
|
||||
] as never);
|
||||
|
||||
const access = await resolveAccountAccess({
|
||||
user: { id: "delegate" },
|
||||
session: { actingAsUserId: "record-owner" },
|
||||
});
|
||||
|
||||
expect(access.accounts).toEqual([
|
||||
expect.objectContaining({
|
||||
access: "write",
|
||||
level: "manage",
|
||||
sections: null,
|
||||
canWrite: true,
|
||||
recordKind: "managed",
|
||||
}),
|
||||
]);
|
||||
expect(access.active).toEqual(
|
||||
expect.objectContaining({
|
||||
level: "manage",
|
||||
sections: null,
|
||||
recordKind: "managed",
|
||||
}),
|
||||
);
|
||||
expect(access.recordKind).toBe("managed");
|
||||
});
|
||||
|
||||
it("resolves an ordinary shared record on the server", async () => {
|
||||
vi.mocked(prisma.accountGrant.findMany).mockResolvedValue([
|
||||
activeGrant({ access: "READ" }),
|
||||
] as never);
|
||||
|
||||
const access = await resolveAccountAccess({
|
||||
user: { id: "delegate" },
|
||||
session: {},
|
||||
});
|
||||
|
||||
expect(access.accounts[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
access: "read",
|
||||
level: "read",
|
||||
recordKind: "shared",
|
||||
}),
|
||||
);
|
||||
expect(access.recordKind).toBe("self");
|
||||
});
|
||||
|
||||
it("keeps whole-record legacy payloads decodable and scoped payloads closed", () => {
|
||||
for (const fixture of LEGACY_ACCOUNT_PAYLOADS) {
|
||||
const decoded = decodeLegacyWholeRecordAccess(fixture);
|
||||
expect(decoded === null ? "deny" : "allow", fixture.name).toBe(
|
||||
fixture.legacyDecoder,
|
||||
);
|
||||
if (fixture.legacyDecoder === "allow") {
|
||||
expect(decoded).toBe(fixture.access);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -23,6 +23,26 @@ import type { ShareDomain } from "@/lib/sharing/scope";
|
||||
/** What a grant lets its holder do, resolved. */
|
||||
export type AccountAccessLevel = "read" | "write" | "manage";
|
||||
|
||||
/** The two values published by the original account-access contract. */
|
||||
export type LegacyAccountAccessLevel = "read" | "write";
|
||||
|
||||
/** The server-resolved kind of record a presentation is naming. */
|
||||
export type AccountRecordKind = "self" | "shared" | "managed";
|
||||
|
||||
/**
|
||||
* Read the portion of an account-access entry that an older whole-record
|
||||
* client understands. A scoped entry is refused rather than guessed at.
|
||||
*/
|
||||
export function decodeLegacyWholeRecordAccess(
|
||||
value: unknown,
|
||||
): LegacyAccountAccessLevel | null {
|
||||
if (value === null || typeof value !== "object") return null;
|
||||
const entry = value as { access?: unknown; sections?: unknown };
|
||||
if (entry.access !== "read" && entry.access !== "write") return null;
|
||||
if (entry.sections !== undefined && entry.sections !== null) return null;
|
||||
return entry.access;
|
||||
}
|
||||
|
||||
/** One account this caller may act on. */
|
||||
export interface AccountAccessEntry {
|
||||
/** The account whose record it is — the value `POST /api/account/switch` takes. */
|
||||
@@ -37,13 +57,18 @@ export interface AccountAccessEntry {
|
||||
* assigned from one expression in `account-access.ts`, so they cannot come
|
||||
* apart; `level` is the name to write new code against.
|
||||
*/
|
||||
access: AccountAccessLevel;
|
||||
access: LegacyAccountAccessLevel;
|
||||
/**
|
||||
* v1.37.0 — the same resolved level, under the name the three-level
|
||||
* contract speaks. Read it; never derive it from `canWrite`, and never
|
||||
* derive `canWrite` from it.
|
||||
*/
|
||||
level: AccountAccessLevel;
|
||||
/**
|
||||
* What kind of record this is. This is presentation metadata only; it is
|
||||
* resolved by the server and never accepted as an authorization input.
|
||||
*/
|
||||
recordKind: AccountRecordKind;
|
||||
/**
|
||||
* v1.37.0 — which sections of the record this grant opens, or `null` for
|
||||
* the entire record.
|
||||
@@ -81,6 +106,8 @@ export interface AccountAccess {
|
||||
* and always null on the Bearer transport.
|
||||
*/
|
||||
active: AccountAccessEntry | null;
|
||||
/** The server-resolved kind of the record currently in view. */
|
||||
recordKind?: AccountRecordKind;
|
||||
/** Is there anywhere to switch to. */
|
||||
canSwitch: boolean;
|
||||
}
|
||||
|
||||
@@ -104,24 +104,32 @@ export async function resolveAccountAccess(auth: {
|
||||
where: { granteeId: actorId, revokedAt: null },
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: {
|
||||
grantor: { select: { id: true, username: true, displayName: true } },
|
||||
grantor: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
displayName: true,
|
||||
managedProfileAt: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const accounts: AccountAccessEntry[] = rows
|
||||
.filter((grant) => isGrantActive(grant, now))
|
||||
.map((grant) => {
|
||||
// One expression, two names. `access` is what v1.36.0 published and
|
||||
// what shipped clients read; `level` is what the three-level contract
|
||||
// calls it. Deriving both from one lookup is what stops them saying
|
||||
// different things about the same row.
|
||||
// `access` is the shipped two-level field, so MANAGE remains write for
|
||||
// clients that have not learned the canonical level. `level` names the
|
||||
// complete three-level contract.
|
||||
const level = LEVEL[grant.access];
|
||||
return {
|
||||
accountId: grant.grantor.id,
|
||||
username: grant.grantor.username,
|
||||
displayName: grant.grantor.displayName,
|
||||
access: level,
|
||||
access: level === "read" ? "read" : "write",
|
||||
level,
|
||||
recordKind:
|
||||
grant.grantor.managedProfileAt === null ? "shared" : "managed",
|
||||
sections: resolveGrantSections(grant.scopeJson),
|
||||
canWrite: grantAllows(grant, "write", now),
|
||||
};
|
||||
@@ -132,5 +140,10 @@ export async function resolveAccountAccess(auth: {
|
||||
? null
|
||||
: (accounts.find((a) => a.accountId === stamped) ?? null);
|
||||
|
||||
return { accounts, active, canSwitch: accounts.length > 0 };
|
||||
return {
|
||||
accounts,
|
||||
active,
|
||||
recordKind: active?.recordKind ?? "self",
|
||||
canSwitch: accounts.length > 0,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user