Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 10e6321

Browse files
fix(site): include owner context in Agents org picker permission check (#28076) (#28132)
Backport of #28076 Original PR: #28076 — fix(site): include owner context in Agents org picker permission check Merge commit: d509e1e Requested by: @ibetitsmike Co-authored-by: Michael Suchacz <[email protected]>
1 parent 83f6423 commit 10e6321

3 files changed

Lines changed: 59 additions & 8 deletions

File tree

site/src/api/queries/organizations.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,20 @@ export const provisionerJobs = (
294294
};
295295
};
296296

297+
export const permittedOrganizationsKey = (check: AuthorizationCheck) => [
298+
"organizations",
299+
"permitted",
300+
check,
301+
];
302+
297303
/**
298304
* Fetch organizations the current user is permitted to use for a given
299305
* action. Fetches all organizations, runs a per-org authorization
300306
* check, and returns only those that pass.
301307
*/
302308
export const permittedOrganizations = (check: AuthorizationCheck) => {
303309
return {
304-
queryKey: ["organizations", "permitted", check],
310+
queryKey: permittedOrganizationsKey(check),
305311
queryFn: async (): Promise<Organization[]> => {
306312
const orgs = await API.getOrganizations();
307313
const checks = Object.fromEntries(

site/src/pages/AgentsPage/components/AgentCreateForm.stories.tsx

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
within,
1010
} from "storybook/test";
1111
import { API } from "#/api/api";
12+
import { permittedOrganizationsKey } from "#/api/queries/organizations";
1213
import type * as TypesGen from "#/api/typesGenerated";
1314
import { ConfirmDialog } from "#/components/Dialogs/ConfirmDialog/ConfirmDialog";
1415
import { MockChatModelConfig } from "#/testHelpers/chatModels";
@@ -24,12 +25,10 @@ import {
2425
} from "../utils/reasoningEffort";
2526
import { AgentCreateForm } from "./AgentCreateForm";
2627

27-
// Query key used by permittedOrganizations() in the form.
28-
const permittedOrgsKey = [
29-
"organizations",
30-
"permitted",
31-
{ object: { resource_type: "chat" }, action: "create" },
32-
];
28+
const permittedOrgsKey = permittedOrganizationsKey({
29+
object: { resource_type: "chat", owner_id: "me" },
30+
action: "create",
31+
});
3332

3433
const modelConfigID = "model-config-1";
3534
const claudeModelConfigID = "model-config-claude";
@@ -1061,3 +1060,49 @@ export const PermittedOrgsResolvesToSubset: Story = {
10611060
expect(options.organizationId).toBe(MockOrganization2.id);
10621061
},
10631062
};
1063+
1064+
/**
1065+
* Member-scoped roles like agents-access grant chat:create only on
1066+
* chats the user owns, so the per-org check must carry owner context
1067+
* for the picker to render.
1068+
*/
1069+
export const MemberScopedPermissionsShowOrgPicker: Story = {
1070+
parameters: {
1071+
showOrganizations: true,
1072+
organizations: [MockDefaultOrganization, MockOrganization2],
1073+
},
1074+
beforeEach: () => {
1075+
spyOn(API, "getOrganizations").mockResolvedValue([
1076+
MockDefaultOrganization,
1077+
MockOrganization2,
1078+
]);
1079+
spyOn(API, "checkAuthorization").mockImplementation(async ({ checks }) =>
1080+
Object.fromEntries(
1081+
Object.entries(checks).map(([id, check]) => [
1082+
id,
1083+
check.object.owner_id === "me",
1084+
]),
1085+
),
1086+
);
1087+
},
1088+
play: async ({ canvasElement }) => {
1089+
const canvas = within(canvasElement);
1090+
const picker = await canvas.findByRole(
1091+
"button",
1092+
{ name: /^Organization:/ },
1093+
{ timeout: 3000 },
1094+
);
1095+
await userEvent.click(picker);
1096+
await screen.findByRole("option", {
1097+
name: MockDefaultOrganization.display_name,
1098+
});
1099+
await userEvent.click(
1100+
screen.getByRole("option", { name: MockOrganization2.display_name }),
1101+
);
1102+
expect(
1103+
canvas.getByRole("button", {
1104+
name: `Organization: ${MockOrganization2.display_name}`,
1105+
}),
1106+
).toBeInTheDocument();
1107+
},
1108+
};

site/src/pages/AgentsPage/components/AgentCreateForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ export const AgentCreateForm: FC<AgentCreateFormProps> = ({
461461

462462
const permittedOrgsQuery = useQuery({
463463
...permittedOrganizations({
464-
object: { resource_type: "chat" },
464+
object: { resource_type: "chat", owner_id: "me" },
465465
action: "create",
466466
}),
467467
enabled: showOrganizations,

0 commit comments

Comments
 (0)