@app-vitals/agent-admin 1.101.3 → 1.103.0
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 +2 -2
- package/src/accounts.ts +11 -0
- package/src/admin-extension.ts +2 -0
- package/src/admin-ui-account-pages.ts +1 -0
- package/src/admin-ui-account.smoke.test.ts +27 -11
- package/src/admin-ui-account.ts +2 -0
- package/src/admin-ui-settings-layout.ts +56 -6
- package/src/admin-ui-settings-layout.unit.test.ts +100 -5
- package/src/admin-ui-settings.smoke.test.ts +55 -5
- package/src/admin-ui-slack-config-token-pages.ts +1 -0
- package/src/admin-ui-templates-pages.ts +1 -0
- package/src/admin-ui-templates.smoke.test.ts +43 -6
- package/src/admin-ui-templates.ts +10 -3
- package/src/admin-ui.ts +36 -7
- package/src/caller-scope.integration.test.ts +94 -0
- package/src/caller-scope.ts +51 -4
- package/src/caller-scope.unit.test.ts +113 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-admin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.103.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/app-vitals/agent.git",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@anthropic-ai/sdk": "0.137.0",
|
|
47
|
-
"@app-vitals/agent-lib": "1.
|
|
47
|
+
"@app-vitals/agent-lib": "1.103.0",
|
|
48
48
|
"@hono/zod-openapi": "^1.0.0",
|
|
49
49
|
"@prisma/adapter-pg": "^7.10.0",
|
|
50
50
|
"@prisma/client": "^7.10.0",
|
package/src/accounts.ts
CHANGED
|
@@ -71,6 +71,17 @@ export class AccountService {
|
|
|
71
71
|
return member?.account ?? null;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
/** Account id + role of the member with this email, or null. */
|
|
75
|
+
async getMembershipByEmail(
|
|
76
|
+
email: string,
|
|
77
|
+
): Promise<{ accountId: string; role: string } | null> {
|
|
78
|
+
const member = await this.prisma.accountMember.findUnique({
|
|
79
|
+
where: { email: normalizeEmail(email) },
|
|
80
|
+
select: { accountId: true, role: true },
|
|
81
|
+
});
|
|
82
|
+
return member ?? null;
|
|
83
|
+
}
|
|
84
|
+
|
|
74
85
|
async list(): Promise<Account[]> {
|
|
75
86
|
return this.prisma.account.findMany({ orderBy: { createdAt: "asc" } });
|
|
76
87
|
}
|
package/src/admin-extension.ts
CHANGED
|
@@ -105,6 +105,7 @@ export function renderAccountPage(opts: AccountPageOpts): string {
|
|
|
105
105
|
active: ACCOUNT_PATH,
|
|
106
106
|
toolbarPath: "/admin/settings",
|
|
107
107
|
isAdmin: opts.isAdmin ?? false,
|
|
108
|
+
isOwner: true,
|
|
108
109
|
body: `${errorHtml}
|
|
109
110
|
<div class="card">
|
|
110
111
|
<div class="card-title">Account</div>
|
|
@@ -4,17 +4,17 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { describe, expect, it } from "bun:test";
|
|
7
|
-
import { Hono } from "hono";
|
|
8
7
|
import type { MiddlewareHandler } from "hono";
|
|
9
|
-
import {
|
|
10
|
-
type AccountRouteDeps,
|
|
11
|
-
registerAccountRoutes,
|
|
12
|
-
} from "./admin-ui-account.ts";
|
|
13
|
-
import type { AdminUIEnv } from "./admin-ui.ts";
|
|
8
|
+
import { Hono } from "hono";
|
|
14
9
|
import {
|
|
15
10
|
AccountMemberNotFoundError,
|
|
16
11
|
LastOwnerError,
|
|
17
12
|
} from "./account-members.ts";
|
|
13
|
+
import type { AdminUIEnv } from "./admin-ui.ts";
|
|
14
|
+
import {
|
|
15
|
+
type AccountRouteDeps,
|
|
16
|
+
registerAccountRoutes,
|
|
17
|
+
} from "./admin-ui-account.ts";
|
|
18
18
|
|
|
19
19
|
const d = new Date("2026-01-01T00:00:00Z");
|
|
20
20
|
|
|
@@ -58,7 +58,7 @@ function setup(
|
|
|
58
58
|
enabled: opts.enabled ?? true,
|
|
59
59
|
requireAuth: (async (c, next) => {
|
|
60
60
|
c.set("userEmail", c.req.header("x-test-user-email") ?? "owner@x.com");
|
|
61
|
-
c.set("isAdmin",
|
|
61
|
+
c.set("isAdmin", c.req.header("x-test-admin") === "1");
|
|
62
62
|
await next();
|
|
63
63
|
}) as MiddlewareHandler<AdminUIEnv>,
|
|
64
64
|
html: (content, o) =>
|
|
@@ -169,10 +169,10 @@ describe("/admin/account", () => {
|
|
|
169
169
|
expect(res.status).toBe(404);
|
|
170
170
|
});
|
|
171
171
|
|
|
172
|
-
it("GET renders the account for
|
|
172
|
+
it("GET renders the account for an owner", async () => {
|
|
173
173
|
const { app } = setup();
|
|
174
174
|
const res = await app.request("/admin/account", {
|
|
175
|
-
headers: { "x-test-user-email": "
|
|
175
|
+
headers: { "x-test-user-email": "owner@x.com" },
|
|
176
176
|
});
|
|
177
177
|
expect(res.status).toBe(200);
|
|
178
178
|
const body = await res.text();
|
|
@@ -184,6 +184,22 @@ describe("/admin/account", () => {
|
|
|
184
184
|
expect(body).not.toContain('class="vos-nav-link" href="/admin/account"');
|
|
185
185
|
});
|
|
186
186
|
|
|
187
|
+
it("GET renders the account for an admin who belongs to it", async () => {
|
|
188
|
+
const { app } = setup();
|
|
189
|
+
const res = await app.request("/admin/account", {
|
|
190
|
+
headers: { "x-test-user-email": "member@x.com", "x-test-admin": "1" },
|
|
191
|
+
});
|
|
192
|
+
expect(res.status).toBe(200);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("GET 404s for a plain member (ACM-7.2)", async () => {
|
|
196
|
+
const { app } = setup();
|
|
197
|
+
const res = await app.request("/admin/account", {
|
|
198
|
+
headers: { "x-test-user-email": "member@x.com" },
|
|
199
|
+
});
|
|
200
|
+
expect(res.status).toBe(404);
|
|
201
|
+
});
|
|
202
|
+
|
|
187
203
|
it("owner can invite, see it pending, and revoke it", async () => {
|
|
188
204
|
const { app } = setup();
|
|
189
205
|
const res = await post(app, "/admin/account/invite", {
|
|
@@ -212,7 +228,7 @@ describe("/admin/account", () => {
|
|
|
212
228
|
).toBe(400);
|
|
213
229
|
});
|
|
214
230
|
|
|
215
|
-
it("member gets
|
|
231
|
+
it("member gets 404 on every mutation (ACM-7.2)", async () => {
|
|
216
232
|
const { app } = setup();
|
|
217
233
|
for (const [path, fields] of [
|
|
218
234
|
["/admin/account/invite", { email: "a@b.co" }],
|
|
@@ -223,7 +239,7 @@ describe("/admin/account", () => {
|
|
|
223
239
|
["/admin/account/rename", { name: "X" }],
|
|
224
240
|
] as const) {
|
|
225
241
|
const res = await post(app, path, { ...fields }, "member@x.com");
|
|
226
|
-
expect(res.status).toBe(
|
|
242
|
+
expect(res.status).toBe(404);
|
|
227
243
|
}
|
|
228
244
|
});
|
|
229
245
|
|
package/src/admin-ui-account.ts
CHANGED
|
@@ -88,6 +88,8 @@ export function registerAccountRoutes(
|
|
|
88
88
|
const account = await deps.accounts.getByMemberEmail(c.var.userEmail);
|
|
89
89
|
if (!account) return notFound();
|
|
90
90
|
const me = await deps.members.getByEmail(c.var.userEmail);
|
|
91
|
+
// Plain members never see the Account page; admins with an account may.
|
|
92
|
+
if (me?.role !== "owner" && c.var.isAdmin !== true) return notFound();
|
|
91
93
|
return { account, isOwner: me?.role === "owner" };
|
|
92
94
|
}
|
|
93
95
|
|
|
@@ -7,11 +7,15 @@
|
|
|
7
7
|
|
|
8
8
|
import type { SettingsNavContribution } from "./admin-extension.ts";
|
|
9
9
|
import { escapeHtml, renderAdminToolbar } from "./admin-ui-styles.ts";
|
|
10
|
+
import type { ViewerRole } from "./caller-scope.ts";
|
|
10
11
|
|
|
11
12
|
export interface SettingsNavItem {
|
|
12
13
|
label: string;
|
|
13
14
|
path: string;
|
|
15
|
+
/** Visible to platform admins only. */
|
|
14
16
|
adminOnly?: boolean;
|
|
17
|
+
/** Visible to platform admins and account owners, hidden from members. */
|
|
18
|
+
ownerOnly?: boolean;
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
export interface SettingsNavSection {
|
|
@@ -36,16 +40,33 @@ export interface RenderSettingsLayoutOptions {
|
|
|
36
40
|
toolbarPath?: string;
|
|
37
41
|
/** Show adminOnly items. Default false. */
|
|
38
42
|
isAdmin?: boolean;
|
|
43
|
+
/** Show ownerOnly items (admins always see them). Default false. */
|
|
44
|
+
isOwner?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Whether an item is visible to a viewer with the given role. */
|
|
48
|
+
export function isSettingsItemVisible(
|
|
49
|
+
item: SettingsNavItem,
|
|
50
|
+
role: ViewerRole | null,
|
|
51
|
+
): boolean {
|
|
52
|
+
if (role === "admin") return true;
|
|
53
|
+
if (item.adminOnly) return false;
|
|
54
|
+
if (item.ownerOnly) return role === "owner";
|
|
55
|
+
return true;
|
|
39
56
|
}
|
|
40
57
|
|
|
41
58
|
export function renderSettingsLayout(
|
|
42
59
|
opts: RenderSettingsLayoutOptions,
|
|
43
60
|
): string {
|
|
44
|
-
const
|
|
61
|
+
const role: ViewerRole = opts.isAdmin
|
|
62
|
+
? "admin"
|
|
63
|
+
: opts.isOwner
|
|
64
|
+
? "owner"
|
|
65
|
+
: "member";
|
|
45
66
|
const sections = opts.nav.sections
|
|
46
67
|
.map((s) => ({
|
|
47
68
|
label: s.label,
|
|
48
|
-
items: s.items.filter((i) =>
|
|
69
|
+
items: s.items.filter((i) => isSettingsItemVisible(i, role)),
|
|
49
70
|
}))
|
|
50
71
|
.filter((s) => s.items.length > 0);
|
|
51
72
|
|
|
@@ -137,14 +158,19 @@ export function buildSettingsNav(
|
|
|
137
158
|
});
|
|
138
159
|
}
|
|
139
160
|
if (opts.slackConfigToken) {
|
|
140
|
-
//
|
|
161
|
+
// ownerOnly, like Templates: account owners manage their own row.
|
|
141
162
|
platform.push({
|
|
142
163
|
label: "Slack config token",
|
|
143
164
|
path: SETTINGS_SLACK_CONFIG_TOKEN_PATH,
|
|
165
|
+
ownerOnly: true,
|
|
144
166
|
});
|
|
145
167
|
}
|
|
146
168
|
if (opts.templates) {
|
|
147
|
-
platform.push({
|
|
169
|
+
platform.push({
|
|
170
|
+
label: "Templates",
|
|
171
|
+
path: SETTINGS_TEMPLATES_PATH,
|
|
172
|
+
ownerOnly: true,
|
|
173
|
+
});
|
|
148
174
|
}
|
|
149
175
|
if (opts.accounts) {
|
|
150
176
|
platform.push({
|
|
@@ -176,6 +202,30 @@ export function withoutAccountItem(nav: SettingsNav): SettingsNav {
|
|
|
176
202
|
};
|
|
177
203
|
}
|
|
178
204
|
|
|
205
|
+
/**
|
|
206
|
+
* The nav as the viewer's role sees it: items the role may not see are
|
|
207
|
+
* dropped (adminOnly / ownerOnly), the Account item is hidden from members,
|
|
208
|
+
* and sections left empty are removed. A null role (no account) sees only
|
|
209
|
+
* unflagged items.
|
|
210
|
+
*/
|
|
211
|
+
export function filterSettingsNavForRole(
|
|
212
|
+
nav: SettingsNav,
|
|
213
|
+
role: ViewerRole | null,
|
|
214
|
+
): SettingsNav {
|
|
215
|
+
return {
|
|
216
|
+
sections: nav.sections
|
|
217
|
+
.map((sec) => ({
|
|
218
|
+
...sec,
|
|
219
|
+
items: sec.items.filter(
|
|
220
|
+
(i) =>
|
|
221
|
+
isSettingsItemVisible(i, role) &&
|
|
222
|
+
!(role === "member" && i.path === SETTINGS_ACCOUNT_PATH),
|
|
223
|
+
),
|
|
224
|
+
}))
|
|
225
|
+
.filter((sec) => sec.items.length > 0),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
179
229
|
/** Request-context key under which the viewer-resolved nav is stored. */
|
|
180
230
|
export const SETTINGS_NAV_CONTEXT_KEY = "settingsNav";
|
|
181
231
|
|
|
@@ -195,9 +245,9 @@ export function getSettingsNav(c: {
|
|
|
195
245
|
/** First nav item visible to the viewer, or undefined. */
|
|
196
246
|
export function firstVisibleSettingsItem(
|
|
197
247
|
nav: SettingsNav,
|
|
198
|
-
|
|
248
|
+
role: ViewerRole | null,
|
|
199
249
|
): SettingsNavItem | undefined {
|
|
200
250
|
return nav.sections
|
|
201
251
|
.flatMap((s) => s.items)
|
|
202
|
-
.find((i) =>
|
|
252
|
+
.find((i) => isSettingsItemVisible(i, role));
|
|
203
253
|
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { describe, expect, test } from "bun:test";
|
|
7
7
|
import {
|
|
8
8
|
buildSettingsNav,
|
|
9
|
+
filterSettingsNavForRole,
|
|
9
10
|
firstVisibleSettingsItem,
|
|
10
11
|
renderSettingsLayout,
|
|
11
12
|
type SettingsNav,
|
|
@@ -121,7 +122,11 @@ describe("buildSettingsNav / firstVisibleSettingsItem (SGL-2.2)", () => {
|
|
|
121
122
|
path: "/admin/settings/github-enterprise",
|
|
122
123
|
adminOnly: true,
|
|
123
124
|
},
|
|
124
|
-
{
|
|
125
|
+
{
|
|
126
|
+
label: "Templates",
|
|
127
|
+
path: "/admin/settings/templates",
|
|
128
|
+
ownerOnly: true,
|
|
129
|
+
},
|
|
125
130
|
],
|
|
126
131
|
},
|
|
127
132
|
],
|
|
@@ -150,17 +155,107 @@ describe("buildSettingsNav / firstVisibleSettingsItem (SGL-2.2)", () => {
|
|
|
150
155
|
"A",
|
|
151
156
|
]);
|
|
152
157
|
});
|
|
153
|
-
test("first visible item respects
|
|
158
|
+
test("first visible item respects the viewer role", () => {
|
|
154
159
|
const nav = buildSettingsNav(both);
|
|
155
|
-
expect(firstVisibleSettingsItem(nav,
|
|
160
|
+
expect(firstVisibleSettingsItem(nav, "admin")?.label).toBe(
|
|
156
161
|
"GitHub Enterprise",
|
|
157
162
|
);
|
|
158
|
-
expect(firstVisibleSettingsItem(nav,
|
|
163
|
+
expect(firstVisibleSettingsItem(nav, "owner")?.label).toBe("Templates");
|
|
164
|
+
expect(firstVisibleSettingsItem(nav, "member")).toBeUndefined();
|
|
159
165
|
expect(
|
|
160
166
|
firstVisibleSettingsItem(
|
|
161
167
|
buildSettingsNav({ githubEnterprise: true }),
|
|
162
|
-
|
|
168
|
+
"owner",
|
|
163
169
|
),
|
|
164
170
|
).toBeUndefined();
|
|
165
171
|
});
|
|
172
|
+
test("a member lands on the unflagged Notifications item", () => {
|
|
173
|
+
const nav = buildSettingsNav({
|
|
174
|
+
...both,
|
|
175
|
+
accounts: true,
|
|
176
|
+
extraItems: [
|
|
177
|
+
{ section: "Account", label: "Notifications", path: "/n" },
|
|
178
|
+
{
|
|
179
|
+
section: "Platform",
|
|
180
|
+
label: "System crons",
|
|
181
|
+
path: "/c",
|
|
182
|
+
adminOnly: true,
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
});
|
|
186
|
+
expect(firstVisibleSettingsItem(nav, "member")).toBeDefined();
|
|
187
|
+
expect(
|
|
188
|
+
firstVisibleSettingsItem(
|
|
189
|
+
filterSettingsNavForRole(nav, "member"),
|
|
190
|
+
"member",
|
|
191
|
+
)?.label,
|
|
192
|
+
).toBe("Notifications");
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe("filterSettingsNavForRole (ACM-7.2)", () => {
|
|
197
|
+
const nav = buildSettingsNav({
|
|
198
|
+
githubEnterprise: true,
|
|
199
|
+
templates: true,
|
|
200
|
+
slackConfigToken: true,
|
|
201
|
+
accounts: true,
|
|
202
|
+
extraItems: [
|
|
203
|
+
{ section: "Account", label: "Notifications", path: "/n" },
|
|
204
|
+
{
|
|
205
|
+
section: "Platform",
|
|
206
|
+
label: "System crons",
|
|
207
|
+
path: "/c",
|
|
208
|
+
adminOnly: true,
|
|
209
|
+
},
|
|
210
|
+
],
|
|
211
|
+
});
|
|
212
|
+
const labels = (role: Parameters<typeof filterSettingsNavForRole>[1]) =>
|
|
213
|
+
filterSettingsNavForRole(nav, role).sections.flatMap((s) =>
|
|
214
|
+
s.items.map((i) => i.label),
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
test("flags Templates and Slack config token ownerOnly", () => {
|
|
218
|
+
const items = nav.sections.flatMap((s) => s.items);
|
|
219
|
+
const flag = (l: string) => items.find((i) => i.label === l);
|
|
220
|
+
expect(flag("Templates")?.ownerOnly).toBe(true);
|
|
221
|
+
expect(flag("Slack config token")?.ownerOnly).toBe(true);
|
|
222
|
+
expect(flag("GitHub Enterprise")?.adminOnly).toBe(true);
|
|
223
|
+
expect(flag("Notifications")?.ownerOnly).toBeUndefined();
|
|
224
|
+
});
|
|
225
|
+
test("admin sees every item", () => {
|
|
226
|
+
expect(labels("admin")).toEqual([
|
|
227
|
+
"Account",
|
|
228
|
+
"Notifications",
|
|
229
|
+
"GitHub Enterprise",
|
|
230
|
+
"Slack config token",
|
|
231
|
+
"Templates",
|
|
232
|
+
"Accounts",
|
|
233
|
+
"System crons",
|
|
234
|
+
]);
|
|
235
|
+
});
|
|
236
|
+
test("owner sees all non-adminOnly items", () => {
|
|
237
|
+
expect(labels("owner")).toEqual([
|
|
238
|
+
"Account",
|
|
239
|
+
"Notifications",
|
|
240
|
+
"Slack config token",
|
|
241
|
+
"Templates",
|
|
242
|
+
]);
|
|
243
|
+
});
|
|
244
|
+
test("member sees only unflagged items and not Account", () => {
|
|
245
|
+
expect(labels("member")).toEqual(["Notifications"]);
|
|
246
|
+
});
|
|
247
|
+
test("no role behaves like a member except Account is left to the caller", () => {
|
|
248
|
+
expect(labels(null)).toContain("Notifications");
|
|
249
|
+
expect(labels(null)).not.toContain("Templates");
|
|
250
|
+
});
|
|
251
|
+
test("renderSettingsLayout hides ownerOnly unless isOwner or isAdmin", () => {
|
|
252
|
+
const base = { userName: "u", title: "T", nav, active: "/n", body: "" };
|
|
253
|
+
expect(renderSettingsLayout(base)).not.toContain("Templates");
|
|
254
|
+
expect(renderSettingsLayout({ ...base, isOwner: true })).toContain(
|
|
255
|
+
"Templates",
|
|
256
|
+
);
|
|
257
|
+
expect(renderSettingsLayout({ ...base, isAdmin: true })).toContain(
|
|
258
|
+
"Templates",
|
|
259
|
+
);
|
|
260
|
+
});
|
|
166
261
|
});
|
|
@@ -30,7 +30,10 @@ const adminCookie = async () => `admin_session=${await makeSessionCookie()}`;
|
|
|
30
30
|
const userCookie = async (email = "m@acme.com") =>
|
|
31
31
|
`admin_session=${await makeSessionCookie(undefined, "u2", email, false)}`;
|
|
32
32
|
|
|
33
|
-
const memberAccounts = (
|
|
33
|
+
const memberAccounts = (
|
|
34
|
+
member: string | null,
|
|
35
|
+
role: "owner" | "member" = "owner",
|
|
36
|
+
) =>
|
|
34
37
|
({
|
|
35
38
|
accounts: {
|
|
36
39
|
getByMemberEmail: async (e: string) =>
|
|
@@ -38,7 +41,10 @@ const memberAccounts = (member: string | null) =>
|
|
|
38
41
|
listWithCounts: async () => [{ id: "acct-1", name: "Acme" }],
|
|
39
42
|
getWithCounts: async (id: string) => ({ id, name: "Acme" }),
|
|
40
43
|
},
|
|
41
|
-
members: {
|
|
44
|
+
members: {
|
|
45
|
+
getByEmail: async (e: string) =>
|
|
46
|
+
e === member ? { accountId: "acct-1", email: e, role } : null,
|
|
47
|
+
},
|
|
42
48
|
}) as never;
|
|
43
49
|
|
|
44
50
|
describe("GET /admin/settings", () => {
|
|
@@ -63,7 +69,7 @@ describe("GET /admin/settings", () => {
|
|
|
63
69
|
expect(res.headers.get("location")).toBe("/admin/settings/templates");
|
|
64
70
|
});
|
|
65
71
|
|
|
66
|
-
it("redirects
|
|
72
|
+
it("redirects an account owner past the adminOnly item", async () => {
|
|
67
73
|
const app = createAdminUIApp(
|
|
68
74
|
makeMockDeps({
|
|
69
75
|
githubEnterpriseApi: ghe,
|
|
@@ -78,6 +84,28 @@ describe("GET /admin/settings", () => {
|
|
|
78
84
|
expect(res.headers.get("location")).toBe("/admin/settings/templates");
|
|
79
85
|
});
|
|
80
86
|
|
|
87
|
+
it("lands an account member on Notifications (ACM-7.2)", async () => {
|
|
88
|
+
const app = createAdminUIApp(
|
|
89
|
+
makeMockDeps({
|
|
90
|
+
githubEnterpriseApi: ghe,
|
|
91
|
+
templateService,
|
|
92
|
+
settingsNavItems: [
|
|
93
|
+
{
|
|
94
|
+
section: "Account",
|
|
95
|
+
label: "Notifications",
|
|
96
|
+
path: "/admin/notifications",
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
accountServices: memberAccounts("m@acme.com", "member"),
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
102
|
+
const res = await app.request("/admin/settings", {
|
|
103
|
+
headers: { cookie: await userCookie() },
|
|
104
|
+
});
|
|
105
|
+
expect(res.status).toBe(302);
|
|
106
|
+
expect(res.headers.get("location")).toBe("/admin/notifications");
|
|
107
|
+
});
|
|
108
|
+
|
|
81
109
|
it("404s a non-admin when only admin-only items exist", async () => {
|
|
82
110
|
const app = createAdminUIApp(makeMockDeps({ githubEnterpriseApi: ghe }));
|
|
83
111
|
const res = await app.request("/admin/settings", {
|
|
@@ -126,7 +154,7 @@ describe("settings shell pages", () => {
|
|
|
126
154
|
);
|
|
127
155
|
});
|
|
128
156
|
|
|
129
|
-
it("renders Templates in the shell and hides the GHE item from
|
|
157
|
+
it("renders Templates in the shell and hides the GHE item from owners", async () => {
|
|
130
158
|
const app = createAdminUIApp(
|
|
131
159
|
makeMockDeps({
|
|
132
160
|
githubEnterpriseApi: ghe,
|
|
@@ -203,7 +231,7 @@ describe("GET /admin/settings with accounts (SGL-2.3)", () => {
|
|
|
203
231
|
} as never),
|
|
204
232
|
);
|
|
205
233
|
|
|
206
|
-
it("lands an account
|
|
234
|
+
it("lands an account owner on Account", async () => {
|
|
207
235
|
const res = await build("m@acme.com").request("/admin/settings", {
|
|
208
236
|
headers: { cookie: await userCookie("m@acme.com") },
|
|
209
237
|
});
|
|
@@ -211,6 +239,28 @@ describe("GET /admin/settings with accounts (SGL-2.3)", () => {
|
|
|
211
239
|
expect(res.headers.get("location")).toBe("/admin/account");
|
|
212
240
|
});
|
|
213
241
|
|
|
242
|
+
it("lands an account member on Notifications, not Account (ACM-7.2)", async () => {
|
|
243
|
+
const app = createAdminUIApp(
|
|
244
|
+
makeMockDeps({
|
|
245
|
+
templateService,
|
|
246
|
+
selfServe,
|
|
247
|
+
settingsNavItems: [
|
|
248
|
+
{
|
|
249
|
+
section: "Account",
|
|
250
|
+
label: "Notifications",
|
|
251
|
+
path: "/admin/notifications",
|
|
252
|
+
},
|
|
253
|
+
],
|
|
254
|
+
accountServices: memberAccounts("m@acme.com", "member"),
|
|
255
|
+
} as never),
|
|
256
|
+
);
|
|
257
|
+
const res = await app.request("/admin/settings", {
|
|
258
|
+
headers: { cookie: await userCookie("m@acme.com") },
|
|
259
|
+
});
|
|
260
|
+
expect(res.status).toBe(302);
|
|
261
|
+
expect(res.headers.get("location")).toBe("/admin/notifications");
|
|
262
|
+
});
|
|
263
|
+
|
|
214
264
|
it("skips Account for an admin without an account", async () => {
|
|
215
265
|
const res = await build(null).request("/admin/settings", {
|
|
216
266
|
headers: { cookie: await adminCookie() },
|
|
@@ -113,6 +113,7 @@ export function renderSlackConfigTokenPage(
|
|
|
113
113
|
buildSettingsNav({ githubEnterprise: true, slackConfigToken: true }),
|
|
114
114
|
active: BASE,
|
|
115
115
|
isAdmin: o.isAdmin ?? false,
|
|
116
|
+
isOwner: true,
|
|
116
117
|
body: `${error}${notice}${encryptionWarning}
|
|
117
118
|
<div class="card">
|
|
118
119
|
<p class="help-text">The Slack App Configuration token used to create and update agent Slack apps. A deployment-wide token is the default; an account's own token overrides it. Tokens are stored encrypted and never shown.</p>
|
|
@@ -178,6 +178,7 @@ export function renderTemplatesPage(o: TemplatesPageOpts): string {
|
|
|
178
178
|
o.nav ?? buildSettingsNav({ githubEnterprise: true, templates: true }),
|
|
179
179
|
active: BASE,
|
|
180
180
|
isAdmin: o.isAdmin ?? false,
|
|
181
|
+
isOwner: true,
|
|
181
182
|
body: `${error}${notice}
|
|
182
183
|
<div class="card">
|
|
183
184
|
<p class="help-text">Default env vars, tools, plugins, crons and plugin marketplaces for new agents in a scope. Secret values are masked after saving; leave <code>***</code> to keep the stored value.</p>
|
|
@@ -94,7 +94,12 @@ function fakeService() {
|
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
function setup(
|
|
97
|
-
opts: {
|
|
97
|
+
opts: {
|
|
98
|
+
isAdmin?: boolean;
|
|
99
|
+
scope?: CallerScope;
|
|
100
|
+
/** Role of u@x.com in acct-1; default "owner" (a non-admin is an owner). */
|
|
101
|
+
role?: "owner" | "member";
|
|
102
|
+
} = {},
|
|
98
103
|
): { app: Hono<AdminUIEnv> } & ReturnType<typeof fakeService> {
|
|
99
104
|
const svc = fakeService();
|
|
100
105
|
const app = new Hono<AdminUIEnv>();
|
|
@@ -108,6 +113,16 @@ function setup(
|
|
|
108
113
|
templateService: svc.service,
|
|
109
114
|
callerScopeResolver: async () =>
|
|
110
115
|
opts.scope ?? { kind: "scoped", accountId: null, agentIds: [] },
|
|
116
|
+
members: {
|
|
117
|
+
getByEmail: async () =>
|
|
118
|
+
({
|
|
119
|
+
id: "m1",
|
|
120
|
+
accountId: "acct-1",
|
|
121
|
+
email: "u@x.com",
|
|
122
|
+
role: opts.role ?? "owner",
|
|
123
|
+
createdAt: now,
|
|
124
|
+
}) as never,
|
|
125
|
+
},
|
|
111
126
|
accounts: {
|
|
112
127
|
listWithCounts: async () =>
|
|
113
128
|
[
|
|
@@ -136,7 +151,7 @@ const form = (fields: Record<string, string>) => ({
|
|
|
136
151
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
137
152
|
body: new URLSearchParams(fields).toString(),
|
|
138
153
|
});
|
|
139
|
-
const
|
|
154
|
+
const ownerScope: CallerScope = {
|
|
140
155
|
kind: "scoped",
|
|
141
156
|
accountId: "acct-1",
|
|
142
157
|
agentIds: [],
|
|
@@ -257,16 +272,16 @@ describe("/admin/settings/templates smoke", () => {
|
|
|
257
272
|
expect(await res.text()).toContain("unknown specialty ghost");
|
|
258
273
|
});
|
|
259
274
|
|
|
260
|
-
it("offers an account
|
|
261
|
-
const { app } = setup({ isAdmin: false, scope:
|
|
275
|
+
it("offers an account owner only their own account", async () => {
|
|
276
|
+
const { app } = setup({ isAdmin: false, scope: ownerScope });
|
|
262
277
|
const html = await (await app.request(BASE)).text();
|
|
263
278
|
expect(html).toContain("Acme");
|
|
264
279
|
expect(html).not.toContain("Globex");
|
|
265
280
|
expect(html).not.toContain("Deployment-wide");
|
|
266
281
|
});
|
|
267
282
|
|
|
268
|
-
it("pins
|
|
269
|
-
const { app, rows } = setup({ isAdmin: false, scope:
|
|
283
|
+
it("pins owner writes to their account and rejects other accounts", async () => {
|
|
284
|
+
const { app, rows } = setup({ isAdmin: false, scope: ownerScope });
|
|
270
285
|
const ok = await app.request(
|
|
271
286
|
`${BASE}/items`,
|
|
272
287
|
form({ accountId: "", specialty: "", kind: "tool", key: "Bash" }),
|
|
@@ -319,6 +334,28 @@ describe("/admin/settings/templates smoke", () => {
|
|
|
319
334
|
expect(seeded).toContain("Remove");
|
|
320
335
|
});
|
|
321
336
|
|
|
337
|
+
it("403s a plain account member on GET and every mutating route (ACM-7.2)", async () => {
|
|
338
|
+
const { app, rows } = setup({
|
|
339
|
+
isAdmin: false,
|
|
340
|
+
scope: ownerScope,
|
|
341
|
+
role: "member",
|
|
342
|
+
});
|
|
343
|
+
expect((await app.request(BASE)).status).toBe(403);
|
|
344
|
+
for (const path of ["items", "items/delete", "delete"]) {
|
|
345
|
+
const res = await app.request(
|
|
346
|
+
`${BASE}/${path}`,
|
|
347
|
+
form({ accountId: "", specialty: "", kind: "tool", key: "Bash" }),
|
|
348
|
+
);
|
|
349
|
+
expect(res.status).toBe(403);
|
|
350
|
+
}
|
|
351
|
+
expect(rows).toHaveLength(0);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("lets an account owner in, scoped to their own account (ACM-7.2)", async () => {
|
|
355
|
+
const { app } = setup({ isAdmin: false, scope: ownerScope });
|
|
356
|
+
expect((await app.request(BASE)).status).toBe(200);
|
|
357
|
+
});
|
|
358
|
+
|
|
322
359
|
it("403s a non-admin without an account", async () => {
|
|
323
360
|
const { app } = setup({ isAdmin: false });
|
|
324
361
|
expect((await app.request(BASE)).status).toBe(403);
|
|
@@ -8,11 +8,12 @@
|
|
|
8
8
|
* errors (denylisted env key, unknown specialty, bad payload) are shown inline.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import type { SettingsNav } from "./admin-ui-settings-layout.ts";
|
|
12
11
|
import type { Context, Hono, MiddlewareHandler } from "hono";
|
|
13
12
|
import { accountIdFromScope } from "./account-agent-create.ts";
|
|
13
|
+
import type { AccountMemberService } from "./account-members.ts";
|
|
14
14
|
import type { AccountService } from "./accounts.ts";
|
|
15
15
|
import type { AdminUIEnv } from "./admin-ui.ts";
|
|
16
|
+
import type { SettingsNav } from "./admin-ui-settings-layout.ts";
|
|
16
17
|
import {
|
|
17
18
|
ADMIN_TEMPLATES_PATH as BASE,
|
|
18
19
|
renderTemplatesPage,
|
|
@@ -40,6 +41,8 @@ export interface AdminTemplatesRouteDeps {
|
|
|
40
41
|
"list" | "replace" | "putItem" | "deleteItem" | "delete"
|
|
41
42
|
>;
|
|
42
43
|
callerScopeResolver: CallerScopeResolver;
|
|
44
|
+
/** Needed to tell account owners from plain members; absent = admins only. */
|
|
45
|
+
members?: Pick<AccountMemberService, "getByEmail">;
|
|
43
46
|
/** Settings sidebar nav (SGL-2.2). */
|
|
44
47
|
settingsNav?: SettingsNav;
|
|
45
48
|
/** Optional: without it admins can only pick deployment-wide. */
|
|
@@ -73,8 +76,12 @@ export function registerAdminTemplatesRoutes(
|
|
|
73
76
|
const accountId = accountIdFromScope(
|
|
74
77
|
await deps.callerScopeResolver(c.var.userEmail, false),
|
|
75
78
|
);
|
|
76
|
-
if (accountId === null) {
|
|
77
|
-
throw new ForbiddenError("Admin or account access required");
|
|
79
|
+
if (accountId === null || !deps.members) {
|
|
80
|
+
throw new ForbiddenError("Admin or account owner access required");
|
|
81
|
+
}
|
|
82
|
+
const me = await deps.members.getByEmail(c.var.userEmail);
|
|
83
|
+
if (!me || me.role !== "owner" || me.accountId !== accountId) {
|
|
84
|
+
throw new ForbiddenError("Admin or account owner access required");
|
|
78
85
|
}
|
|
79
86
|
return { kind: "account", accountId };
|
|
80
87
|
}
|
package/src/admin-ui.ts
CHANGED
|
@@ -100,6 +100,7 @@ import {
|
|
|
100
100
|
} from "./admin-ui-pages.ts";
|
|
101
101
|
import {
|
|
102
102
|
buildSettingsNav,
|
|
103
|
+
filterSettingsNavForRole,
|
|
103
104
|
firstVisibleSettingsItem,
|
|
104
105
|
SETTINGS_NAV_CONTEXT_KEY,
|
|
105
106
|
type SettingsNav,
|
|
@@ -134,6 +135,7 @@ import {
|
|
|
134
135
|
type CallerScopeResolver,
|
|
135
136
|
memberOnlyCallerScopeResolver,
|
|
136
137
|
scopeIncludesAgent,
|
|
138
|
+
type ViewerRole,
|
|
137
139
|
} from "./caller-scope.ts";
|
|
138
140
|
import { ForbiddenError, UnprocessableEntityError } from "./errors.ts";
|
|
139
141
|
import type { GithubAppsState } from "./github-apps-state.ts";
|
|
@@ -1023,16 +1025,31 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
1023
1025
|
|
|
1024
1026
|
// Expose the viewer-resolved nav to every /admin/* handler registered after
|
|
1025
1027
|
// this point, including extension adminUiRoutes (mounted after this app).
|
|
1028
|
+
// Viewer role for the nav: admin, else the AccountMember.role, else null.
|
|
1029
|
+
const resolveNavRole = async (
|
|
1030
|
+
email: string,
|
|
1031
|
+
isAdmin: boolean,
|
|
1032
|
+
): Promise<ViewerRole | null> => {
|
|
1033
|
+
if (isAdmin) return "admin";
|
|
1034
|
+
if (!accountServices) return null;
|
|
1035
|
+
const me = await accountServices.members
|
|
1036
|
+
.getByEmail(email)
|
|
1037
|
+
.catch(() => null);
|
|
1038
|
+
return me?.role === "owner" || me?.role === "member" ? me.role : null;
|
|
1039
|
+
};
|
|
1040
|
+
|
|
1026
1041
|
app.use("/admin/*", async (c, next) => {
|
|
1027
1042
|
let nav = settingsNav;
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1043
|
+
const token = getCookie(c, SESSION_COOKIE);
|
|
1044
|
+
const user = token ? await getSessionUser(token, sessionSecret) : null;
|
|
1045
|
+
if (user) {
|
|
1046
|
+
const role = await resolveNavRole(user.email, user.isAdmin === true);
|
|
1047
|
+
nav = filterSettingsNavForRole(settingsNav, role);
|
|
1048
|
+
if (accountNavEnabled && accountServices) {
|
|
1032
1049
|
const account = await accountServices.accounts
|
|
1033
1050
|
.getByMemberEmail(user.email)
|
|
1034
1051
|
.catch(() => null);
|
|
1035
|
-
if (!account) nav = withoutAccountItem(
|
|
1052
|
+
if (!account) nav = withoutAccountItem(nav);
|
|
1036
1053
|
}
|
|
1037
1054
|
}
|
|
1038
1055
|
c.set(SETTINGS_NAV_CONTEXT_KEY, nav);
|
|
@@ -4493,6 +4510,10 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
4493
4510
|
) {
|
|
4494
4511
|
app.get("/admin/settings", requireAuth, async (c) => {
|
|
4495
4512
|
// Admins without an account would 404 on the Account item; skip it.
|
|
4513
|
+
const role = await resolveNavRole(
|
|
4514
|
+
c.var.userEmail,
|
|
4515
|
+
c.var.isAdmin === true,
|
|
4516
|
+
);
|
|
4496
4517
|
let nav = settingsNav;
|
|
4497
4518
|
if (accountNavEnabled && accountServices) {
|
|
4498
4519
|
const account = await accountServices.accounts
|
|
@@ -4500,7 +4521,10 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
4500
4521
|
.catch(() => null);
|
|
4501
4522
|
if (!account) nav = withoutAccountItem(settingsNav);
|
|
4502
4523
|
}
|
|
4503
|
-
const first = firstVisibleSettingsItem(
|
|
4524
|
+
const first = firstVisibleSettingsItem(
|
|
4525
|
+
filterSettingsNavForRole(nav, role),
|
|
4526
|
+
role,
|
|
4527
|
+
);
|
|
4504
4528
|
if (!first) {
|
|
4505
4529
|
return new Response("No settings are available for your account.", {
|
|
4506
4530
|
status: 404,
|
|
@@ -4528,7 +4552,12 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
4528
4552
|
templateService,
|
|
4529
4553
|
settingsNav,
|
|
4530
4554
|
callerScopeResolver,
|
|
4531
|
-
...(accountServices
|
|
4555
|
+
...(accountServices
|
|
4556
|
+
? {
|
|
4557
|
+
accounts: accountServices.accounts,
|
|
4558
|
+
members: accountServices.members,
|
|
4559
|
+
}
|
|
4560
|
+
: {}),
|
|
4532
4561
|
specialtyRegistry,
|
|
4533
4562
|
html,
|
|
4534
4563
|
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/caller-scope.integration.test.ts
|
|
3
|
+
* Role resolver + role-aware scope against real Postgres (ACM-7.1).
|
|
4
|
+
*
|
|
5
|
+
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
9
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
10
|
+
import { AccountMemberService } from "./account-members.ts";
|
|
11
|
+
import { AccountService } from "./accounts.ts";
|
|
12
|
+
import { AgentMemberService } from "./agent-members.ts";
|
|
13
|
+
import {
|
|
14
|
+
callerScopeDepsFromServices,
|
|
15
|
+
resolveCallerScope,
|
|
16
|
+
resolveViewerRole,
|
|
17
|
+
} from "./caller-scope.ts";
|
|
18
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
19
|
+
|
|
20
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
21
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
22
|
+
|
|
23
|
+
describeOrSkip("caller scope role resolver (integration)", () => {
|
|
24
|
+
let prisma: PrismaClient;
|
|
25
|
+
let deps: ReturnType<typeof callerScopeDepsFromServices>;
|
|
26
|
+
let accountId: string;
|
|
27
|
+
let accountAgentIds: string[];
|
|
28
|
+
let sideAgentId: string;
|
|
29
|
+
|
|
30
|
+
beforeEach(async () => {
|
|
31
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
32
|
+
await prisma.agentMember.deleteMany();
|
|
33
|
+
await prisma.accountInvite.deleteMany();
|
|
34
|
+
await prisma.accountMember.deleteMany();
|
|
35
|
+
await prisma.agent.deleteMany();
|
|
36
|
+
await prisma.account.deleteMany();
|
|
37
|
+
const accountService = new AccountService(prisma);
|
|
38
|
+
const agentMemberService = new AgentMemberService(prisma);
|
|
39
|
+
deps = callerScopeDepsFromServices({ accountService, agentMemberService });
|
|
40
|
+
|
|
41
|
+
const account = await accountService.create("Acme", "Owner@Acme.com");
|
|
42
|
+
accountId = account.id;
|
|
43
|
+
await new AccountMemberService(prisma).add(
|
|
44
|
+
account.id,
|
|
45
|
+
"member@acme.com",
|
|
46
|
+
"member",
|
|
47
|
+
);
|
|
48
|
+
const a1 = await prisma.agent.create({
|
|
49
|
+
data: { name: "a1", accountId: account.id },
|
|
50
|
+
});
|
|
51
|
+
const a2 = await prisma.agent.create({
|
|
52
|
+
data: { name: "a2", accountId: account.id },
|
|
53
|
+
});
|
|
54
|
+
accountAgentIds = [a1.id, a2.id];
|
|
55
|
+
const side = await prisma.agent.create({ data: { name: "side" } });
|
|
56
|
+
sideAgentId = side.id;
|
|
57
|
+
await agentMemberService.add(side.id, "member@acme.com");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
afterEach(async () => {
|
|
61
|
+
await prisma.$disconnect();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("resolves admin / owner / member / null", async () => {
|
|
65
|
+
const role = (email: string, isAdmin = false) =>
|
|
66
|
+
resolveViewerRole({ email, isAdmin }, deps);
|
|
67
|
+
expect(await role("anyone@x.com", true)).toBe("admin");
|
|
68
|
+
expect(await role("OWNER@acme.com")).toBe("owner");
|
|
69
|
+
expect(await role("member@acme.com")).toBe("member");
|
|
70
|
+
expect(await role("stranger@x.com")).toBeNull();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("owner gets account agents; member only AgentMember agents (flag on)", async () => {
|
|
74
|
+
const scope = (email: string, flagEnabled: boolean) =>
|
|
75
|
+
resolveCallerScope({ email, isAdmin: false, flagEnabled }, deps);
|
|
76
|
+
|
|
77
|
+
const owner = await scope("owner@acme.com", true);
|
|
78
|
+
expect(owner.kind === "scoped" && owner.accountId).toBe(accountId);
|
|
79
|
+
expect(owner.kind === "scoped" && owner.agentIds.sort()).toEqual(
|
|
80
|
+
[...accountAgentIds].sort(),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
expect(await scope("member@acme.com", true)).toEqual({
|
|
84
|
+
kind: "scoped",
|
|
85
|
+
accountId,
|
|
86
|
+
agentIds: [sideAgentId],
|
|
87
|
+
});
|
|
88
|
+
expect(await scope("owner@acme.com", false)).toEqual({
|
|
89
|
+
kind: "scoped",
|
|
90
|
+
accountId: null,
|
|
91
|
+
agentIds: [],
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
package/src/caller-scope.ts
CHANGED
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
*
|
|
8
8
|
* Resolved per request from the caller's email — the account is deliberately
|
|
9
9
|
* NOT stored in the session JWT, so membership changes take effect
|
|
10
|
-
* immediately. Account
|
|
11
|
-
* flag is on;
|
|
10
|
+
* immediately. Account agents contribute to the scope only for account owners
|
|
11
|
+
* and only when the self-serve flag is on; members (and flag off) get exactly
|
|
12
|
+
* the AgentMember-only scope.
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
15
|
import type { AccountService } from "./accounts.ts";
|
|
@@ -32,6 +33,36 @@ export interface CallerScopeDeps {
|
|
|
32
33
|
listAgentIdsByAccount(accountId: string): Promise<string[]>;
|
|
33
34
|
/** Ids of agents the email holds an AgentMember row for. */
|
|
34
35
|
listMemberAgentIds(email: string): Promise<string[]>;
|
|
36
|
+
/**
|
|
37
|
+
* Account id + AccountMember.role for the email, or null. Optional so
|
|
38
|
+
* existing consumers keep compiling; when absent, the role is unknown and
|
|
39
|
+
* account agents contribute as before.
|
|
40
|
+
*/
|
|
41
|
+
getAccountMembershipByEmail?(
|
|
42
|
+
email: string,
|
|
43
|
+
): Promise<{ accountId: string; role: string } | null>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** How the caller relates to the platform/account: see resolveViewerRole. */
|
|
47
|
+
export type ViewerRole = "admin" | "owner" | "member";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The caller's role: `admin` for allowlisted callers, otherwise the
|
|
51
|
+
* AccountMember.role (`owner` | `member`), or null when the caller has no
|
|
52
|
+
* account. Independent of the self-serve flag.
|
|
53
|
+
*/
|
|
54
|
+
export async function resolveViewerRole(
|
|
55
|
+
input: { email: string; isAdmin: boolean },
|
|
56
|
+
deps: Pick<CallerScopeDeps, "getAccountMembershipByEmail">,
|
|
57
|
+
): Promise<ViewerRole | null> {
|
|
58
|
+
if (input.isAdmin) return "admin";
|
|
59
|
+
const membership = await deps.getAccountMembershipByEmail?.(
|
|
60
|
+
input.email.toLowerCase(),
|
|
61
|
+
);
|
|
62
|
+
if (membership?.role === "owner" || membership?.role === "member") {
|
|
63
|
+
return membership.role;
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
35
66
|
}
|
|
36
67
|
|
|
37
68
|
export async function resolveCallerScope(
|
|
@@ -44,8 +75,18 @@ export async function resolveCallerScope(
|
|
|
44
75
|
const accountId = input.flagEnabled
|
|
45
76
|
? await deps.getAccountIdByEmail(email)
|
|
46
77
|
: null;
|
|
78
|
+
// Only owners get account-wide agents; with no role dep the role is unknown
|
|
79
|
+
// and the legacy behavior (account agents contribute) is kept.
|
|
80
|
+
const role = accountId
|
|
81
|
+
? await resolveViewerRole({ email, isAdmin: false }, deps)
|
|
82
|
+
: null;
|
|
83
|
+
const includeAccountAgents =
|
|
84
|
+
accountId !== null &&
|
|
85
|
+
(role === "owner" || !deps.getAccountMembershipByEmail);
|
|
47
86
|
const [accountAgentIds, memberAgentIds] = await Promise.all([
|
|
48
|
-
|
|
87
|
+
includeAccountAgents && accountId
|
|
88
|
+
? deps.listAgentIdsByAccount(accountId)
|
|
89
|
+
: [],
|
|
49
90
|
deps.listMemberAgentIds(email),
|
|
50
91
|
]);
|
|
51
92
|
return {
|
|
@@ -100,14 +141,20 @@ export function createCallerScopeResolver(
|
|
|
100
141
|
|
|
101
142
|
/** Wire the resolver's I/O to the real services. */
|
|
102
143
|
export function callerScopeDepsFromServices(services: {
|
|
103
|
-
accountService: Pick<AccountService, "getByMemberEmail" | "listAgentIds"
|
|
144
|
+
accountService: Pick<AccountService, "getByMemberEmail" | "listAgentIds"> &
|
|
145
|
+
Partial<Pick<AccountService, "getMembershipByEmail">>;
|
|
104
146
|
agentMemberService: Pick<AgentMemberService, "listByEmail">;
|
|
105
147
|
}): CallerScopeDeps {
|
|
148
|
+
const getMembership = services.accountService.getMembershipByEmail?.bind(
|
|
149
|
+
services.accountService,
|
|
150
|
+
);
|
|
106
151
|
return {
|
|
107
152
|
getAccountIdByEmail: async (email) =>
|
|
108
153
|
(await services.accountService.getByMemberEmail(email))?.id ?? null,
|
|
109
154
|
listAgentIdsByAccount: (accountId) =>
|
|
110
155
|
services.accountService.listAgentIds(accountId),
|
|
156
|
+
getAccountMembershipByEmail:
|
|
157
|
+
getMembership && ((email) => getMembership(email)),
|
|
111
158
|
listMemberAgentIds: async (email) =>
|
|
112
159
|
(await services.agentMemberService.listByEmail(email)).map(
|
|
113
160
|
(m) => m.agentId,
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
type CallerScope,
|
|
4
4
|
type CallerScopeDeps,
|
|
5
5
|
resolveCallerScope,
|
|
6
|
+
resolveViewerRole,
|
|
6
7
|
scopeIncludesAgent,
|
|
7
8
|
taskStoreViewScope,
|
|
8
9
|
} from "./caller-scope.ts";
|
|
@@ -11,18 +12,39 @@ function makeDeps(opts: {
|
|
|
11
12
|
account?: Record<string, string>;
|
|
12
13
|
accountAgents?: Record<string, string[]>;
|
|
13
14
|
members?: Record<string, string[]>;
|
|
15
|
+
roles?: Record<string, string>;
|
|
14
16
|
}): CallerScopeDeps {
|
|
15
17
|
return {
|
|
16
18
|
getAccountIdByEmail: async (e) => opts.account?.[e] ?? null,
|
|
17
19
|
listAgentIdsByAccount: async (id) => opts.accountAgents?.[id] ?? [],
|
|
18
20
|
listMemberAgentIds: async (e) => opts.members?.[e] ?? [],
|
|
21
|
+
getAccountMembershipByEmail: async (e) => {
|
|
22
|
+
const accountId = opts.account?.[e];
|
|
23
|
+
const role = opts.roles?.[e];
|
|
24
|
+
return accountId && role ? { accountId, role } : null;
|
|
25
|
+
},
|
|
19
26
|
};
|
|
20
27
|
}
|
|
21
28
|
|
|
22
29
|
const deps = makeDeps({
|
|
23
|
-
account: {
|
|
30
|
+
account: {
|
|
31
|
+
"a@x.com": "acct-a",
|
|
32
|
+
"both@x.com": "acct-a",
|
|
33
|
+
"mem@x.com": "acct-a",
|
|
34
|
+
"memboth@x.com": "acct-a",
|
|
35
|
+
},
|
|
36
|
+
roles: {
|
|
37
|
+
"a@x.com": "owner",
|
|
38
|
+
"both@x.com": "owner",
|
|
39
|
+
"mem@x.com": "member",
|
|
40
|
+
"memboth@x.com": "member",
|
|
41
|
+
},
|
|
24
42
|
accountAgents: { "acct-a": ["ag-1", "ag-2"] },
|
|
25
|
-
members: {
|
|
43
|
+
members: {
|
|
44
|
+
"m@x.com": ["ag-9"],
|
|
45
|
+
"both@x.com": ["ag-2", "ag-9"],
|
|
46
|
+
"memboth@x.com": ["ag-9"],
|
|
47
|
+
},
|
|
26
48
|
});
|
|
27
49
|
|
|
28
50
|
const run = (email: string, isAdmin = false, flagEnabled = true) =>
|
|
@@ -33,7 +55,7 @@ describe("resolveCallerScope", () => {
|
|
|
33
55
|
expect(await run("a@x.com", true)).toEqual({ kind: "all" });
|
|
34
56
|
});
|
|
35
57
|
|
|
36
|
-
it("account
|
|
58
|
+
it("account owner gets every account agent", async () => {
|
|
37
59
|
expect(await run("a@x.com")).toEqual({
|
|
38
60
|
kind: "scoped",
|
|
39
61
|
accountId: "acct-a",
|
|
@@ -41,6 +63,33 @@ describe("resolveCallerScope", () => {
|
|
|
41
63
|
});
|
|
42
64
|
});
|
|
43
65
|
|
|
66
|
+
it("account member (flag on) gets only AgentMember agents, keeps accountId", async () => {
|
|
67
|
+
expect(await run("mem@x.com")).toEqual({
|
|
68
|
+
kind: "scoped",
|
|
69
|
+
accountId: "acct-a",
|
|
70
|
+
agentIds: [],
|
|
71
|
+
});
|
|
72
|
+
expect(await run("memboth@x.com")).toEqual({
|
|
73
|
+
kind: "scoped",
|
|
74
|
+
accountId: "acct-a",
|
|
75
|
+
agentIds: ["ag-9"],
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("deps without the role lookup keep the legacy account-agent behavior", async () => {
|
|
80
|
+
const { getAccountMembershipByEmail: _omit, ...legacy } = deps;
|
|
81
|
+
expect(
|
|
82
|
+
await resolveCallerScope(
|
|
83
|
+
{ email: "mem@x.com", isAdmin: false, flagEnabled: true },
|
|
84
|
+
legacy,
|
|
85
|
+
),
|
|
86
|
+
).toEqual({
|
|
87
|
+
kind: "scoped",
|
|
88
|
+
accountId: "acct-a",
|
|
89
|
+
agentIds: ["ag-1", "ag-2"],
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
44
93
|
it("explicit member gets only AgentMember agents", async () => {
|
|
45
94
|
expect(await run("m@x.com")).toEqual({
|
|
46
95
|
kind: "scoped",
|
|
@@ -114,3 +163,64 @@ describe("taskStoreViewScope (SSP-6.8)", () => {
|
|
|
114
163
|
).toEqual({ kind: "none" });
|
|
115
164
|
});
|
|
116
165
|
});
|
|
166
|
+
|
|
167
|
+
describe("resolveViewerRole", () => {
|
|
168
|
+
const role = (email: string, isAdmin = false) =>
|
|
169
|
+
resolveViewerRole({ email, isAdmin }, deps);
|
|
170
|
+
|
|
171
|
+
it("admin for allowlisted callers, even with a member row", async () => {
|
|
172
|
+
expect(await role("mem@x.com", true)).toBe("admin");
|
|
173
|
+
expect(await role("nobody@x.com", true)).toBe("admin");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("owner or member from the account role, case-insensitive", async () => {
|
|
177
|
+
expect(await role("A@x.com")).toBe("owner");
|
|
178
|
+
expect(await role("mem@x.com")).toBe("member");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("null for a caller with no account", async () => {
|
|
182
|
+
expect(await role("none@x.com")).toBeNull();
|
|
183
|
+
expect(await role("m@x.com")).toBeNull();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("null for an unknown role value or deps without the lookup", async () => {
|
|
187
|
+
expect(
|
|
188
|
+
await resolveViewerRole(
|
|
189
|
+
{ email: "x@x.com", isAdmin: false },
|
|
190
|
+
{
|
|
191
|
+
getAccountMembershipByEmail: async () => ({
|
|
192
|
+
accountId: "a",
|
|
193
|
+
role: "weird",
|
|
194
|
+
}),
|
|
195
|
+
},
|
|
196
|
+
),
|
|
197
|
+
).toBeNull();
|
|
198
|
+
expect(
|
|
199
|
+
await resolveViewerRole({ email: "x@x.com", isAdmin: false }, {}),
|
|
200
|
+
).toBeNull();
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
describe("resolveCallerScope matrix: role x flag", () => {
|
|
205
|
+
const cases: Array<[string, boolean, string[], string | null]> = [
|
|
206
|
+
["a@x.com", true, ["ag-1", "ag-2"], "acct-a"], // owner, flag on
|
|
207
|
+
["a@x.com", false, [], null], // owner, flag off
|
|
208
|
+
["memboth@x.com", true, ["ag-9"], "acct-a"], // member, flag on
|
|
209
|
+
["memboth@x.com", false, ["ag-9"], null], // member, flag off
|
|
210
|
+
["none@x.com", true, [], null], // no account
|
|
211
|
+
["none@x.com", false, [], null],
|
|
212
|
+
];
|
|
213
|
+
for (const [email, flag, agentIds, accountId] of cases) {
|
|
214
|
+
it(`${email} flag=${flag}`, async () => {
|
|
215
|
+
expect(await run(email, false, flag)).toEqual({
|
|
216
|
+
kind: "scoped",
|
|
217
|
+
accountId,
|
|
218
|
+
agentIds,
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
it("admin is all regardless of flag", async () => {
|
|
223
|
+
expect(await run("a@x.com", true, false)).toEqual({ kind: "all" });
|
|
224
|
+
expect(await run("a@x.com", true, true)).toEqual({ kind: "all" });
|
|
225
|
+
});
|
|
226
|
+
});
|