Skip to content

Commit 227a2fb

Browse files
committed
wip(console): only administrators share list views (#36371)
Claude-Session: https://claude.ai/code/session_01QsR2jTPMb2q2wRssyREqhQ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3faabd2 commit 227a2fb

18 files changed

Lines changed: 641 additions & 9 deletions

‎packages/rest/src/meta-write-door-capability-enumeration.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import '@objectstack/objectql';
6868
import { describe, it, expect, vi, beforeEach } from 'vitest';
6969
import { ANONYMOUS_DENY_CODE, ANONYMOUS_DENY_STATUS } from '@objectstack/core';
7070
import { RestServer } from './rest-server.js';
71+
import { securityDefaultPermissionSets } from '@objectstack/plugin-security';
7172

7273
/**
7374
* The unscoped mount base, and the `/meta` prefix on it. The `DOORS` table
@@ -703,3 +704,66 @@ describe('#30675 — the data doors refuse on their own capability, not `manage_
703704
},
704705
);
705706
});
707+
708+
/**
709+
* [objectstack#36371] Only an administrator makes a list view everyone sees.
710+
* A shared view is `view` metadata, so the door that shares one is this
711+
* surface's save (the Console saves `?mode=draft` and publishes at once), the
712+
* door that unshares one is the delete, and the publish promotes it. Driven
713+
* with the systemPermissions the SHIPPED sets grant, not hand-written ones: a
714+
* member (`member_default`), an organization administrator (that baseline
715+
* plus `organization_admin`, which withholds `manage_metadata`) and the
716+
* platform administrator (`admin_full_access`, the control). The Console asks
717+
* the same capability before it offers Shared (`useCanAuthorMetadata`), so the
718+
* organization administrator's refusal here is why that control is hidden.
719+
*/
720+
describe('#36371 — sharing a list view is an administrator\'s act', () => {
721+
const grants = (...names: string[]): string[] => {
722+
const held = new Set<string>();
723+
for (const name of names) {
724+
const set = securityDefaultPermissionSets.find((ps) => ps.name === name);
725+
if (!set) throw new Error(`no shipped permission set named ${name}`);
726+
for (const capability of set.systemPermissions ?? []) held.add(capability);
727+
}
728+
return [...held];
729+
};
730+
const VIEW = { type: 'view', name: 'account.team_pipeline' };
731+
const viewDoor = (protocolMethod: string): Door => {
732+
const door = DOORS.find((d) => d.protocolMethod === protocolMethod)!;
733+
return {
734+
...door,
735+
params: { ...VIEW },
736+
...(protocolMethod === 'saveMetaItem'
737+
? { body: { name: VIEW.name, object: 'account', viewKind: 'list', label: 'Team pipeline', config: { type: 'grid', data: { provider: 'object', object: 'account' } } } }
738+
: {}),
739+
};
740+
};
741+
const SHARE_DOORS = ['saveMetaItem', 'publishMetaItem', 'deleteMetaItem'].map(viewDoor);
742+
743+
it.each([
744+
['a member', ['member_default']],
745+
['an organization administrator', ['member_default', 'organization_admin']],
746+
] as const)('%s is refused each share door 403 FORBIDDEN, and nothing is written', async (_who, sets) => {
747+
const systemPermissions = grants(...sets);
748+
expect(systemPermissions).not.toContain('manage_metadata');
749+
for (const door of SHARE_DOORS) {
750+
const stack = boot({ userId: 'u_member', systemPermissions });
751+
const out = await stack.knock(door);
752+
expect(out.status, door.label).toBe(403);
753+
expect(out.body, door.label).toMatchObject({ error: { code: 'FORBIDDEN' } });
754+
expect(stack.calls[door.protocolMethod], door.label).toBe(0);
755+
}
756+
});
757+
758+
it('the control: the platform administrator reaches each share door', async () => {
759+
const systemPermissions = grants('admin_full_access');
760+
expect(systemPermissions).toContain('manage_metadata');
761+
for (const door of SHARE_DOORS) {
762+
const stack = boot({ userId: 'u_admin', systemPermissions });
763+
const out = await stack.knock(door);
764+
expect(out.status, door.label).not.toBe(403);
765+
expect(out.status, door.label).not.toBe(401);
766+
expect(stack.calls[door.protocolMethod], door.label).toBe(1);
767+
}
768+
});
769+
});

‎ui/packages/app-shell/src/hooks/useMetaItemRefusal.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
* only, so it never answers for the next URL.
3030
* - `refusal` is set for `denied` alone. Every other verdict leaves the page's
3131
* missing-item face: the server said absence, or said nothing.
32+
* - `absent` (objectstack#36371) is set for `not_found` alone: the server
33+
* says the item is not there for anyone, so a page may say it is gone (a
34+
* view unshared or deleted while a reader had it open) rather than fall
35+
* back in silence. Unreachable and unknown answers say nothing.
3236
*
3337
* Nothing is cached across mounts: a verdict about the CALLER goes stale the
3438
* moment they are granted the capability.
@@ -50,10 +54,13 @@ export interface MetaItemRefusalReading {
5054
readonly checking: boolean;
5155
/** The server refused this caller the item; `null` for every other answer. */
5256
readonly refusal: MetaItemRefusal | null;
57+
/** The server answered that no such item exists. */
58+
readonly absent: boolean;
5359
}
5460

55-
const NOT_REFUSED: MetaItemRefusalReading = Object.freeze({ checking: false, refusal: null });
56-
const CHECKING: MetaItemRefusalReading = Object.freeze({ checking: true, refusal: null });
61+
const NOT_REFUSED: MetaItemRefusalReading = Object.freeze({ checking: false, refusal: null, absent: false });
62+
const CHECKING: MetaItemRefusalReading = Object.freeze({ checking: true, refusal: null, absent: false });
63+
const ABSENT: MetaItemRefusalReading = Object.freeze({ checking: false, refusal: null, absent: true });
5764

5865
export function useMetaItemRefusal(type: 'dashboard' | 'view', name: string | null): MetaItemRefusalReading {
5966
const adapter = useAdapter();
@@ -79,6 +86,7 @@ export function useMetaItemRefusal(type: 'dashboard' | 'view', name: string | nu
7986

8087
if (!asked) return NOT_REFUSED;
8188
if (!answer || answer.type !== type || answer.name !== asked) return CHECKING;
89+
if (answer.access.verdict === 'not_found') return ABSENT;
8290
if (answer.access.verdict !== 'denied') return NOT_REFUSED;
83-
return { checking: false, refusal: { message: answer.access.message } };
91+
return { checking: false, refusal: { message: answer.access.message }, absent: false };
8492
}

‎ui/packages/app-shell/src/views/ObjectView.tsx‎

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ import { useObjectViewListRenderer } from './objectViewListRenderer.js';
100100
import { ListDraftActions } from './ListDraftActions.js';
101101
import { viewSaveRefusalText } from './viewSaveRefusal.js';
102102
import { listDraftPreference, listDraftScope, useListDraft } from './listDraftStore.js';
103-
import { useObjectViewViewCrud } from './objectViewViewCrud.js';
103+
import { useObjectViewViewCrud, useRuntimeCreateApp } from './objectViewViewCrud.js';
104+
import { mayShareViews, useViewSharing } from './objectViewViewSharing.js';
105+
import { useCanAuthorMetadata } from '../hooks/useCanAuthorMetadata.js';
104106
import {
105107
PERSONAL_VIEW_OBJECT,
106108
buildInPlaceViewConfig,
@@ -698,6 +700,17 @@ function ObjectViewInner({ dataSource, objects, onEdit, externalRefreshKey }: Co
698700
? (requestedViewId.includes('.') ? requestedViewId : `${objectDef.name}.${requestedViewId}`)
699701
: null;
700702
const viewAccess = useMetaItemRefusal('view', missedViewName);
703+
// objectstack#36371 — a view the address names that the server says is
704+
// gone (unshared or deleted while the reader had it open): the list opens
705+
// its default view, and says why once per name.
706+
const goneViewNoticed = useRef<string | null>(null);
707+
useEffect(() => {
708+
if (!viewAccess.absent || !missedViewName || goneViewNoticed.current === missedViewName) return;
709+
goneViewNoticed.current = missedViewName;
710+
toast.info(t('view.switcher.viewGone', {
711+
defaultValue: 'The view you opened is no longer available, so the list shows its default view.',
712+
}));
713+
}, [viewAccess.absent, missedViewName, t]);
701714
const activeViewId = resolvedViewId || pinnedViewId || defaultViewId || views[0]?.id;
702715
const baseView = views.find((v: any) => v.id === activeViewId) || views[0];
703716
const activeView = viewDraft && viewDraft.id === baseView?.id
@@ -1124,6 +1137,33 @@ function ObjectViewInner({ dataSource, objects, onEdit, externalRefreshKey }: Co
11241137
listSeedRef,
11251138
});
11261139

1140+
// objectstack#36371 — only an administrator holding the shared view's write
1141+
// capability shares views: creates one shared, makes one of their own
1142+
// shared, or unshares one (`./objectViewViewSharing.ts`).
1143+
const canAuthorMetadata = useCanAuthorMetadata();
1144+
const canShareViews = mayShareViews(isAdmin, canAuthorMetadata);
1145+
const sharingApp = useRuntimeCreateApp(t);
1146+
const viewTabItemsRef = useRef<ReadonlyArray<{ id: string; label?: unknown }>>([]);
1147+
const viewSharing = useViewSharing({
1148+
objectName: objectDef.name,
1149+
objectDef,
1150+
metadataClient,
1151+
dataSource,
1152+
app: sharingApp,
1153+
personalViews,
1154+
savedViews,
1155+
viewIds: views.map((v: any) => String(v.id)),
1156+
labelOf: (id) => String(viewTabItemsRef.current.find((item) => item.id === id)?.label ?? id),
1157+
activeViewId,
1158+
routeViewId: viewId,
1159+
navigate,
1160+
pinnedViewId,
1161+
setPinnedViewId: setPinnedViewChoice,
1162+
refresh: () => setRefreshKey((k) => k + 1),
1163+
confirmHandler,
1164+
t,
1165+
});
1166+
11271167
// objectstack#27594: the list title is the view switcher, which replaced the
11281168
// view tab strip; the view items `ManageViewsDialog` lists come with it.
11291169
const { viewTabItems, titleSwitcher, refusingPackages } = useObjectViewTitleSwitcher({
@@ -1156,17 +1196,28 @@ function ObjectViewInner({ dataSource, objects, onEdit, externalRefreshKey }: Co
11561196
}),
11571197
savePersonal: personalViews.save,
11581198
openPersonal: openPersonalView,
1159-
createShared: handleViewCreate,
1199+
createShared: viewSharing.createShared,
11601200
clearSourceListState,
11611201
canCreatePersonal: canCreatePersonalView,
1162-
canCreateShared: isAdmin,
1202+
canCreateShared: canShareViews,
11631203
hasUnsavedConditions: listDraftParts.some((part) => part === 'filters' || part === 'sort'),
11641204
},
11651205
onRenamePersonalView: personalViewsReady && personalGrant('update')
11661206
? (id: string) => setPersonalViewDialog({ mode: 'rename', viewId: id })
11671207
: undefined,
11681208
onDeletePersonalView: personalViewsReady ? (id: string) => { void handleDeletePersonalView(id); } : undefined,
1209+
onMakeSharedView: canShareViews && personalViewsReady
1210+
? (id: string) => { void viewSharing.makeShared(id); }
1211+
: undefined,
1212+
// Unsharing makes the view one of the reader's own, so it needs their create.
1213+
onUnshareView: canShareViews && personalViewsReady && personalGrant('create')
1214+
? (id: string) => { void viewSharing.unshare(id); }
1215+
: undefined,
11691216
});
1217+
// Read when an unshare asks for the view's name, after this render.
1218+
useEffect(() => {
1219+
viewTabItemsRef.current = viewTabItems;
1220+
}, [viewTabItems]);
11701221

11711222
// objectstack#35974 — the view panel is read-only on a view a read-only
11721223
// package ships, judged from the SAME answer that drew its Manage views

‎ui/packages/app-shell/src/views/objectViewSwitcherMenu.tsx‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
*
1313
* Each row offers on hover the reader's ☆ (sidebar Favorites) and 📌 (the
1414
* view this object's list opens by default), and on the reader's own views
15-
* Rename and Delete. The trigger (type icon, name, count, caret) and the
15+
* Rename and Delete. For a reader who may share views (objectstack#36371),
16+
* the reader's own views add 「Make shared」 and a shared view saved here adds
17+
* 「Unshare」 (`./objectViewViewSharing.ts`). The trigger (type icon, name, count, caret) and the
1618
* popover around this body are the switcher's (`./objectViewTitleSwitcher.tsx`).
1719
*/
1820

1921
import { lazy, Suspense, useState, type ComponentType, type ReactNode } from 'react';
2022
import { cn, Input } from '@object-ui/components';
2123
import type { useObjectTranslation } from '@object-ui/i18n';
22-
import { Check, ListOrdered, Pencil, Pin, Plus, Star, Trash2 } from 'lucide-react';
24+
import { Check, ListOrdered, Lock, Pencil, Pin, Plus, Share2, Star, Trash2 } from 'lucide-react';
2325
import type { ListViewFavorites } from '../layout/listViewFavorites.js';
2426
import { VIEW_TYPE_ICONS } from './objectViewHelpers.js';
2527
import type { CreateViewCardProps } from './CreateViewDialog.js';
@@ -58,6 +60,10 @@ export interface ObjectViewSwitcherMenuProps {
5860
onManageViews?: () => void;
5961
onRenamePersonalView?: (viewId: string) => void;
6062
onDeletePersonalView?: (viewId: string) => void;
63+
/** objectstack#36371 — 「Make shared」 on the reader's own views. Absent ⇒ not offered. */
64+
onMakeSharedView?: (viewId: string) => void;
65+
/** objectstack#36371 — 「Unshare」 on the shared views marked `unsharable`. Absent ⇒ not offered. */
66+
onUnshareView?: (viewId: string) => void;
6167
/** 「Add view」 and its card. Absent ⇒ `onAddView`, if any. */
6268
create?: SwitcherCreateOptions;
6369
/** 「Add view」 for a host with no card to offer: the dropdown closes and this runs (the Create View dialog). */
@@ -106,6 +112,8 @@ export function ObjectViewSwitcherMenu({
106112
onManageViews,
107113
onRenamePersonalView,
108114
onDeletePersonalView,
115+
onMakeSharedView,
116+
onUnshareView,
109117
create,
110118
onAddView,
111119
phone = false,
@@ -162,6 +170,8 @@ export function ObjectViewSwitcherMenu({
162170
: t('view.switcher.pin', { defaultValue: 'Pin: open this view by default' });
163171
const renameLabel = t('view.rename', { defaultValue: 'Rename' });
164172
const deleteLabel = t('view.deleteView', { defaultValue: 'Delete View' });
173+
const makeSharedLabel = t('view.switcher.makeShared', { defaultValue: 'Make shared' });
174+
const unshareLabel = t('view.switcher.unshare', { defaultValue: 'Unshare' });
165175
const iconButton = 'flex h-7 w-7 shrink-0 items-center justify-center rounded-sm text-muted-foreground hover:text-foreground max-sm:h-11 max-sm:w-11 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
166176
// Shown on hover or focus, and always while set; a touch screen has no
167177
// hover, so there they always show.
@@ -235,6 +245,30 @@ export function ObjectViewSwitcherMenu({
235245
<Pencil className="h-3.5 w-3.5" />
236246
</button>
237247
)}
248+
{view.personal && onMakeSharedView && (
249+
<button
250+
type="button"
251+
data-testid="view-switcher-make-shared"
252+
aria-label={makeSharedLabel}
253+
title={makeSharedLabel}
254+
onClick={() => { onClose(); onMakeSharedView(view.id); }}
255+
className={cn(iconButton, reveal)}
256+
>
257+
<Share2 className="h-3.5 w-3.5" />
258+
</button>
259+
)}
260+
{!view.personal && view.unsharable && onUnshareView && (
261+
<button
262+
type="button"
263+
data-testid="view-switcher-unshare"
264+
aria-label={unshareLabel}
265+
title={unshareLabel}
266+
onClick={() => { onClose(); onUnshareView(view.id); }}
267+
className={cn(iconButton, reveal)}
268+
>
269+
<Lock className="h-3.5 w-3.5" />
270+
</button>
271+
)}
238272
{view.personal && onDeletePersonalView && (
239273
<button
240274
type="button"

‎ui/packages/app-shell/src/views/objectViewTitleSwitcher.tsx‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import type { buildViewTabs } from './objectViewTabs.js';
5656
import type { ConsoleObjectViewProps } from './ObjectView.js';
5757
import { FOOTER_ITEM, ObjectViewSwitcherMenu, type SwitcherCreateOptions } from './objectViewSwitcherMenu.js';
5858
import { useCreateViewInPlace } from './objectViewViewCrud.js';
59+
import { isUnsharableView } from './objectViewViewSharing.js';
5960

6061
type TFn = ReturnType<typeof useObjectTranslation>['t'];
6162

@@ -82,6 +83,8 @@ export interface TitleSwitcherView {
8283
authored: boolean;
8384
/** One of the reader's own views (My views, a `sys_personal_view` row). */
8485
personal?: boolean;
86+
/** objectstack#36371 — a shared view saved here, which may be unshared (`isUnsharableView`). */
87+
unsharable?: boolean;
8588
}
8689

8790
export interface ObjectViewTitleSwitcherProps {
@@ -115,6 +118,10 @@ export interface ObjectViewTitleSwitcherProps {
115118
onRenamePersonalView?: (viewId: string) => void;
116119
/** Delete one of the reader's own views. Absent ⇒ not offered. */
117120
onDeletePersonalView?: (viewId: string) => void;
121+
/** objectstack#36371 — 「Make shared」 on one of the reader's own views. Absent ⇒ not offered. */
122+
onMakeSharedView?: (viewId: string) => void;
123+
/** objectstack#36371 — 「Unshare」 on a view marked `unsharable`. Absent ⇒ not offered. */
124+
onUnshareView?: (viewId: string) => void;
118125
/**
119126
* 「Add view」 as the in-place create card (objectstack#35553); Save as my
120127
* view folds into it. Absent ⇒ `onAddView`, if any.
@@ -138,6 +145,8 @@ export function ObjectViewTitleSwitcher({
138145
onManageViews,
139146
onRenamePersonalView,
140147
onDeletePersonalView,
148+
onMakeSharedView,
149+
onUnshareView,
141150
create,
142151
t,
143152
}: ObjectViewTitleSwitcherProps) {
@@ -169,6 +178,8 @@ export function ObjectViewTitleSwitcher({
169178
onManageViews={onManageViews}
170179
onRenamePersonalView={onRenamePersonalView}
171180
onDeletePersonalView={onDeletePersonalView}
181+
onMakeSharedView={onMakeSharedView}
182+
onUnshareView={onUnshareView}
172183
create={create}
173184
onAddView={onAddView}
174185
phone={phone}
@@ -319,6 +330,8 @@ export function useObjectViewTitleSwitcher({
319330
create,
320331
onRenamePersonalView,
321332
onDeletePersonalView,
333+
onMakeSharedView,
334+
onUnshareView,
322335
}: {
323336
views: ReturnType<typeof buildViewTabs>;
324337
savedViews: Parameters<typeof buildViewTabs>[0]['savedViews'];
@@ -354,6 +367,13 @@ export function useObjectViewTitleSwitcher({
354367
};
355368
onRenamePersonalView?: (viewId: string) => void;
356369
onDeletePersonalView?: (viewId: string) => void;
370+
/**
371+
* objectstack#36371 — for a reader who may share views: 「Make shared」 on
372+
* their own views, and 「Unshare」 on each shared view `isUnsharableView`
373+
* admits, judged from the same item Manage views lists.
374+
*/
375+
onMakeSharedView?: (viewId: string) => void;
376+
onUnshareView?: (viewId: string) => void;
357377
}) {
358378
const createInPlace = useCreateViewInPlace({
359379
objectName: objectDef.name,
@@ -441,14 +461,19 @@ export function useObjectViewTitleSwitcher({
441461
const customized = saved?.overrides?.package === packageId;
442462
return { ...item, definitionReadonly: true, ...(customized ? { customized: true } : {}) };
443463
});
464+
const manageItemOf = new Map(viewTabItems.map((item) => [item.id, item]));
444465
const switcherViews: TitleSwitcherView[] = viewItems.map((item) => {
445466
const personal = personalViewIds.has(item.id);
467+
const manageItem = personal ? undefined : manageItemOf.get(item.id);
468+
const unsharable = !!onUnshareView && !!manageItem
469+
&& isUnsharableView(manageItem, savedViews.find((sv: any) => viewRowId(sv) === item.id));
446470
return {
447471
id: item.id,
448472
label: item.label,
449473
type: item.type,
450474
authored: !personal && !!item.readonly,
451475
...(personal ? { personal: true } : {}),
476+
...(unsharable ? { unsharable: true } : {}),
452477
};
453478
});
454479
const viewIds = switcherViews.map((v) => v.id);
@@ -490,6 +515,8 @@ export function useObjectViewTitleSwitcher({
490515
create={createOptions}
491516
onRenamePersonalView={onRenamePersonalView}
492517
onDeletePersonalView={onDeletePersonalView}
518+
onMakeSharedView={onMakeSharedView}
519+
onUnshareView={onUnshareView}
493520
t={t}
494521
/>
495522
) : null;

0 commit comments

Comments
 (0)