diff --git a/.changeset/36448-view-management-metadata-gate.md b/.changeset/36448-view-management-metadata-gate.md new file mode 100644 index 00000000000..f17140eec77 --- /dev/null +++ b/.changeset/36448-view-management-metadata-gate.md @@ -0,0 +1,10 @@ +--- +'@object-ui/app-shell': patch +--- + +fix(console): offer view management only to an administrator who may write views + +Clause-②: no +Metadata-protocol: none + +Manage views, the view settings panel and Create View on a list page, the plugin view's own Create View and view actions, the data page's Save as view, and a saved shared view's rename and save in place were offered to every workspace administrator. Each of them writes `view` metadata, which the server refuses without the `manage_metadata` capability, so an organization administrator who lacks it was offered them and met a refusal on the first save. They now read the answer the Shared choice of a new view already reads (`mayManageViews`): an administrator whose session holds `manage_metadata`. A metadata administrator sees them as before; a reader's own views are unchanged. diff --git a/ui/packages/app-shell/src/views/ObjectDataPage.saveAsViewRefusal-11578.test.tsx b/ui/packages/app-shell/src/views/ObjectDataPage.saveAsViewRefusal-11578.test.tsx index c38b187ac57..d47d0872ddd 100644 --- a/ui/packages/app-shell/src/views/ObjectDataPage.saveAsViewRefusal-11578.test.tsx +++ b/ui/packages/app-shell/src/views/ObjectDataPage.saveAsViewRefusal-11578.test.tsx @@ -93,6 +93,8 @@ function wire() { }); } let client: MetadataClient; +/** objectstack#36448 — the principal's `manage_metadata` answer, set per case. */ +const viewer = vi.hoisted(() => ({ canAuthorMetadata: true })); vi.mock('./metadata-admin/useMetadata', async (importOriginal) => ({ ...(await importOriginal>()), @@ -110,10 +112,14 @@ vi.mock('@object-ui/permissions', async (importOriginal) => { useFieldPermissions: () => ({ canRead: () => true, canWrite: () => true, permissions: [] }), }; }); +vi.mock('../hooks/useCanAuthorMetadata', async (importOriginal) => ({ + ...(await importOriginal>()), + useCanAuthorMetadata: () => viewer.canAuthorMetadata, +})); vi.mock('@object-ui/auth', async (importOriginal) => ({ ...(await importOriginal>()), useAuth: () => ({ user: { id: 'u1', name: 'Ada' }, activeOrganization: null }), - // "Save as view" and its dialog are admin-only. + // "Save as view" and its dialog are for an admin holding `manage_metadata`. useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), createAuthenticatedFetch: () => vi.fn(), })); @@ -177,6 +183,7 @@ async function saveAsView() { beforeEach(() => { cleanup(); puts.length = 0; + viewer.canAuthorMetadata = true; client = new MetadataClient({ baseUrl: 'http://localhost', fetch: wire() as unknown as typeof fetch }); vi.spyOn(console, 'error').mockImplementation(() => {}); // objectstack#35624 — the create asks the package list where the app saves @@ -221,3 +228,31 @@ describe('"Save as view" says a refused save, and never reports it as saved (obj expect(screen.queryByTestId('create-view-dialog')).toBeNull(); }); }); + +describe('objectstack#36448 — "Save as view" is offered to an administrator holding manage_metadata', () => { + const mount = () => render( + + + + ({ data: [] })) }} objects={[DEAL]} />} + /> + + + , + ); + + it('an organization administrator without it is not offered the save the server refuses', () => { + viewer.canAuthorMetadata = false; + mount(); + expect(screen.queryByTestId('object-data-save-as-view')).toBeNull(); + expect(screen.queryByTestId('object-data-save-as-view-menu-item')).toBeNull(); + expect(screen.queryByTestId('create-view-dialog')).toBeNull(); + }); + + it('with it, the entry is offered (control)', () => { + mount(); + expect(screen.getByTestId('object-data-save-as-view')).toBeTruthy(); + }); +}); diff --git a/ui/packages/app-shell/src/views/ObjectDataPage.tsx b/ui/packages/app-shell/src/views/ObjectDataPage.tsx index 31d1f7d5808..25162943c71 100644 --- a/ui/packages/app-shell/src/views/ObjectDataPage.tsx +++ b/ui/packages/app-shell/src/views/ObjectDataPage.tsx @@ -99,6 +99,8 @@ import { } from '../preview/PreviewModeContext.js'; import { usePreviewListDataSource } from '../preview/unpublishedObjectReads.js'; import { useTenancyPosture } from '../hooks/useTenancyPosture.js'; +import { useCanAuthorMetadata } from '../hooks/useCanAuthorMetadata.js'; +import { mayManageViews } from './objectViewViewSharing.js'; import { servedObjectLabel, servedObjectPluralLabel } from '../hooks/servedObjectLabel.js'; import type { RowCrudPredicates } from '../utils/crudAffordances.js'; import { servesRecordRead } from '../utils/recordReadServed.js'; @@ -332,6 +334,11 @@ export function ObjectDataPage({ dataSource, objects }: any) { const { canRead } = useFieldPermissions(objectName ?? ''); const { user, activeOrganization } = useAuth(); const { isAdmin } = useWorkspaceAdminStatus(); + // objectstack#36448 — "Save as view" writes `view` metadata, which the + // server refuses without `manage_metadata`: offered to an administrator + // holding it (`mayManageViews`), never to one the save would answer 403. + const canAuthorMetadata = useCanAuthorMetadata(); + const canSaveAsView = mayManageViews(isAdmin, canAuthorMetadata); const metadataClient = useMetadataClient(); // ADR-0105: group posture appends a trailing organization_id attribution // column to the auto-derived columns (reads span all the user's orgs). @@ -908,7 +915,7 @@ export function ObjectDataPage({ dataSource, objects }: any) { {t('console.objectView.new')} ) : null; - const saveAsViewButton = isAdmin ? ( + const saveAsViewButton = canSaveAsView ? (