Skip to content

Commit 5197c40

Browse files
committed
feat(console): the sharing-rule object picker lists only shareable objects, business objects first
A new field widget, shareable-object-ref, reads GET /api/v1/sharing/shareable-objects through the host's authenticated fetch and offers the door's list in its order. When the door does not answer the list (403, 501, no door, no host fetch) the field is the object-ref picker. sys_sharing_rule.object_name now declares the new widget. Claude-Session: https://claude.ai/code/session_01FEkQ5ntiCZev2cDL6r5VLw Co-authored-by: Claude <noreply@anthropic.com>
1 parent 733efbd commit 5197c40

10 files changed

Lines changed: 338 additions & 9 deletions

File tree

‎packages/plugins/plugin-sharing/src/objects/sys-sharing-rule.by-object.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import * as SharingObjects from './index.js';
1717
* `sys_record_share.by_object`, where the field is a pointer pair's type half.
1818
*
1919
* A field stores an object's API name when it declares the `object-ref`
20-
* widget (the object picker) or when it is the `referenceVia` sibling of a
20+
* widget (the object picker) or `shareable-object-ref` (the sharing-rule object
21+
* picker, the same stored name) or when it is the `referenceVia` sibling of a
2122
* pointer pair. Enumerated over every object this package ships
2223
* (`objects/index.ts`), so a view added later is judged without anyone
2324
* listing it here.
@@ -40,7 +41,7 @@ describe('sharing list views — no grouping on a field that stores an object AP
4041
function apiNameFields(object: ObjectDef): string[] {
4142
const names = new Set<string>();
4243
for (const [name, def] of Object.entries(object.fields)) {
43-
if (def?.widget === 'object-ref') names.add(name);
44+
if (def?.widget === 'object-ref' || def?.widget === 'shareable-object-ref') names.add(name);
4445
if (typeof def?.referenceVia === 'string') names.add(def.referenceVia);
4546
}
4647
return [...names].sort();
@@ -77,6 +78,6 @@ describe('sharing list views — no grouping on a field that stores an object AP
7778
expect(view?.sort?.[0]).toEqual({ field: 'object_name', order: 'asc' });
7879
expect(view?.columns?.[0]).toBe('object_name');
7980
const fields = SysSharingRule.fields as Record<string, { widget?: unknown }>;
80-
expect(fields.object_name.widget).toBe('object-ref');
81+
expect(fields.object_name.widget).toBe('shareable-object-ref');
8182
});
8283
});

‎packages/plugins/plugin-sharing/src/objects/sys-sharing-rule.object.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const SysSharingRule = ObjectSchema.create({
7171
columns: ['object_name', 'label', 'recipient_type', 'access_level', 'active'],
7272
// [#24701] Ordered by the target object, not grouped on it: a group header
7373
// draws `object_name` as its stored API name, while the Object cell draws
74-
// the object's icon and label (its `object-ref` widget). No column stores
74+
// the object's icon and label (its object-picker widget). No column stores
7575
// the label, so no grouping key could carry it. The sort keeps one
7676
// object's rules together (`sys-sharing-rule.by-object.test.ts`).
7777
sort: [{ field: 'object_name', order: 'asc' }, { field: 'label', order: 'asc' }],
@@ -132,10 +132,12 @@ export const SysSharingRule = ObjectSchema.create({
132132
label: 'Object',
133133
required: true,
134134
maxLength: 100,
135-
// Rendered as an object picker (choose a registered object by name)
136-
// instead of a free-text machine-name input. Falls back to a text input
137-
// when the `field:object-ref` widget is unavailable.
138-
widget: 'object-ref',
135+
// Rendered as the sharing-rule object picker: only the objects a rule
136+
// can take effect on (`GET /api/v1/sharing/shareable-objects`, the save
137+
// doors' own verdict), business objects first. It is the `object-ref`
138+
// picker when that door does not answer the list, and a text input when
139+
// the `field:shareable-object-ref` widget is unavailable.
140+
widget: 'shareable-object-ref',
139141
description: 'The object whose records this rule shares.',
140142
group: 'Target',
141143
}),

‎ui/packages/app-shell/src/utils/paramValueShape.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export const PARAM_VALUE_SHAPES: Readonly<Record<string, ParamValueShapeSpec>> =
175175
// Widget-hint-only pickers — reached via a field `widget:` override, never a
176176
// bare param `type`. Present for total FORM_FIELD_TYPES coverage only.
177177
'object-ref': { base: 'string', cardinality: 'scalar', widgetHintOnly: true, note: 'Object name string. Widget-hint only — not a declarable param type.' },
178+
'shareable-object-ref': { base: 'string', cardinality: 'scalar', widgetHintOnly: true, note: 'Object name string (an object a sharing rule can take effect on). Widget-hint only — not a declarable param type.' },
178179
'filter-condition': { base: 'object', cardinality: 'scalar', widgetHintOnly: true, note: 'Filter/criteria object. Widget-hint only — not a declarable param type.' },
179180
'recipient-picker': { base: 'string', cardinality: 'scalar', widgetHintOnly: true, note: 'Recipient id string. Widget-hint only — not a declarable param type.' },
180181
'position-ref': { base: 'string', cardinality: 'scalar', widgetHintOnly: true, note: 'Position name string. Widget-hint only — not a declarable param type.' },

‎ui/packages/app-shell/src/views/widget-ref-cells/widget-ref-cell-renderers.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ export function RecipientCell(props: CellRendererProps) {
4747
}
4848

4949
registerFieldRenderer('object-ref', ObjectRefCell);
50+
// The sharing-rule object picker stores an object name too: the same face.
51+
registerFieldRenderer('shareable-object-ref', ObjectRefCell);
5052
registerFieldRenderer('recipient-picker', RecipientCell);

‎ui/packages/cli/src/utils/known-schema-types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export const KNOWN_SCHEMA_TYPES: readonly string[] = [
163163
'field:recipient-picker',
164164
'field:richtext',
165165
'field:select',
166+
'field:shareable-object-ref',
166167
'field:signature',
167168
'field:slider',
168169
'field:summary',

‎ui/packages/components/src/renderers/form/form.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,9 @@ function reportUnrecognizedRulesOnce(fieldName: string, unrecognized: string[]):
740740
*/
741741
const DATA_SOURCE_ONLY_WIDGET_TYPES = new Set([
742742
'object-ref', 'filter-condition', 'recipient-picker',
743+
// The sharing-rule object picker is `object-ref` when its door does not
744+
// answer, and that list is read through `dataSource.getObjects`.
745+
'shareable-object-ref',
743746
// objectui#7611: the registry-name pickers read their catalog through
744747
// `dataSource.listMetadataItems`.
745748
'position-ref', 'permission-ref',

‎ui/packages/fields/src/FieldEditWidget.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export const INLINE_EXCLUDED_FIELD_TYPES = new Set<string>([
188188
'object', 'grid', 'vector',
189189
// Widget-hint-only pickers — authored in the record form (they depend on
190190
// sibling fields / a loaded object catalog), not inline in a grid cell.
191-
'object-ref', 'filter-condition', 'recipient-picker',
191+
'object-ref', 'shareable-object-ref', 'filter-condition', 'recipient-picker',
192192
'position-ref', 'permission-ref',
193193
'notification-topic', 'notification-channel',
194194
]);

‎ui/packages/fields/src/index.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3697,6 +3697,7 @@ const TEXTUAL_BASE_TYPES = new Set(['text', 'textarea', 'string', 'longtext', ''
36973697
*/
36983698
const WIDGET_CELL_RENDERER_TYPES: ReadonlySet<string> = new Set([
36993699
'object-ref',
3700+
'shareable-object-ref',
37003701
'recipient-picker',
37013702
'filter-condition',
37023703
'position-ref',
@@ -4439,6 +4440,9 @@ registerFieldRenderer(RECORD_POINTER_CARD_TYPE, RecordPointerTextCellRenderer);
44394440
// in `scripts/check-eager-closure-budget.mjs` has no room for a label reader;
44404441
// the host loads its faces lazily, off the eager closure.
44414442
registerFieldRenderer('object-ref', TextCellRenderer);
4443+
// The sharing-rule object picker stores the same object name `object-ref` does,
4444+
// so its read face is the same one.
4445+
registerFieldRenderer('shareable-object-ref', TextCellRenderer);
44424446
registerFieldRenderer('recipient-picker', TextCellRenderer);
44434447
// The registry-name keys draw their label in this package: the reader is the
44444448
// edit widget's own `listMetadataItems` catalog, one shared read per data
@@ -4877,9 +4881,13 @@ const fieldWidgetMap = {
48774881
// bare field `type`). They render a *picker* over machine data an admin would
48784882
// otherwise have to type — used by sys_sharing_rule (ADR-0056 P2 pattern):
48794883
// object-ref → choose a registered object by name
4884+
// shareable-object-ref → choose an object a sharing rule can take effect
4885+
// on, from `GET /api/v1/sharing/shareable-objects`
4886+
// (`object-ref` when that door does not answer the list)
48804887
// filter-condition → visual criteria builder scoped to the chosen object
48814888
// recipient-picker → record picker whose target follows a sibling type
48824889
'object-ref': () => import('./widgets/ObjectRefField.js').then(m => ({ default: m.ObjectRefField })),
4890+
'shareable-object-ref': () => import('./widgets/ShareableObjectRefField.js').then(m => ({ default: m.ShareableObjectRefField })),
48834891
'filter-condition': () => import('./widgets/FilterConditionField.js').then(m => ({ default: m.FilterConditionField })),
48844892
'recipient-picker': () => import('./widgets/RecipientPickerField.js').then(m => ({ default: m.RecipientPickerField })),
48854893
// Registry-name pickers (objectui#7611, ruling B2 on objectstack#22865): a
@@ -5076,6 +5084,7 @@ const FIELD_TYPES_SKIP_FALLBACK = new Set([
50765084
// Widget-hint-only pickers — resolved solely via `field:<widget>`, so the
50775085
// bare-key fallback is never wanted.
50785086
'object-ref',
5087+
'shareable-object-ref',
50795088
'filter-condition',
50805089
'recipient-picker',
50815090
'position-ref',
@@ -5226,6 +5235,7 @@ export const FIELD_WIDGET_LABELLING: Record<
52265235
signature: 'group',
52275236
qrcode: 'control',
52285237
'object-ref': 'control',
5238+
'shareable-object-ref': 'control',
52295239
'filter-condition': 'control',
52305240
'recipient-picker': 'control',
52315241
'position-ref': 'control',
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
import React from 'react';
10+
import { Combobox, cn } from '@object-ui/components';
11+
import { SchemaRendererContext, type ApiFetch } from '@object-ui/react';
12+
import type { FieldWidgetComponentProps } from './types.js';
13+
import { toDomProps } from './toDomProps.js';
14+
import { useFieldTranslation } from './useFieldTranslation.js';
15+
import { objectRefOptionLabel } from './objectRefLabel.js';
16+
import { ObjectRefField } from './ObjectRefField.js';
17+
18+
/**
19+
* ShareableObjectRefField — the `shareable-object-ref` widget: the object
20+
* picker of a sharing rule, offering only the objects a rule can take effect on
21+
* (`sys_sharing_rule.object_name`).
22+
*
23+
* The list is the server's, read from the sharing plugin's shareable-objects
24+
* door through the host's authenticated fetch (`SchemaRendererContext.apiFetch`).
25+
* The door lists exactly the objects a rule save admits (it asks the save
26+
* doors' own `SHARING_NOT_ENABLED` verdict), business objects first, then
27+
* platform objects, each by label. The picker shows that order as given and
28+
* never re-sorts it.
29+
*
30+
* When the door does not answer the list, the field is the `object-ref`
31+
* picker, unchanged: every registered object, as before this widget existed.
32+
* That covers a caller the door refuses (`403`: the list is the sharing-rule
33+
* administrators'), a deployment where sharing rules do not run (`501`), a
34+
* server without the door, an answer that is not the door's shape, and a host
35+
* with no authenticated fetch. The save doors stay the authority on which
36+
* object a rule may name, so the wider list costs a refusal at save, never a
37+
* wrong rule; an empty picker would leave the field unfillable.
38+
*
39+
* The stored value is always the object's machine name. A stored name the list
40+
* does not hold (a rule on an object that no longer takes one) stays on screen
41+
* as its own option, so editing the rule never drops it. The read-only face is
42+
* `object-ref`'s, which labels the stored name without asking the door.
43+
*/
44+
45+
/** The door the list is read from (plugin-sharing `shareable-objects-routes.ts`). */
46+
export const SHAREABLE_OBJECTS_URL = '/api/v1/sharing/shareable-objects';
47+
48+
/** One listed object, as the door answers it. */
49+
interface ShareableObjectHeader {
50+
name: string;
51+
label: string;
52+
}
53+
54+
type ShareableCatalog =
55+
| { status: 'loading' }
56+
| { status: 'listed'; objects: ShareableObjectHeader[] }
57+
| { status: 'fallback' };
58+
59+
const LOADING: ShareableCatalog = { status: 'loading' };
60+
const FALLBACK: ShareableCatalog = { status: 'fallback' };
61+
62+
/** The door's list, or `null` when the answer is not the door's success shape. */
63+
function listOf(body: unknown): ShareableObjectHeader[] | null {
64+
const raw = (body as { data?: { objects?: unknown } } | null)?.data?.objects;
65+
if (!Array.isArray(raw)) return null;
66+
const objects: ShareableObjectHeader[] = [];
67+
for (const entry of raw) {
68+
const name = (entry as { name?: unknown } | null)?.name;
69+
if (typeof name !== 'string' || name === '') continue;
70+
const label = (entry as { label?: unknown }).label;
71+
objects.push({ name, label: typeof label === 'string' ? label : '' });
72+
}
73+
return objects;
74+
}
75+
76+
async function readShareableObjects(doFetch: ApiFetch): Promise<ShareableObjectHeader[] | null> {
77+
const res = await doFetch(SHAREABLE_OBJECTS_URL, {
78+
headers: { accept: 'application/json' },
79+
credentials: 'include',
80+
});
81+
if (!res.ok) return null;
82+
return listOf(await res.json());
83+
}
84+
85+
function useShareableObjects(doFetch: ApiFetch | undefined, enabled: boolean): ShareableCatalog {
86+
const [catalog, setCatalog] = React.useState<ShareableCatalog>(doFetch && enabled ? LOADING : FALLBACK);
87+
React.useEffect(() => {
88+
if (!doFetch || !enabled) {
89+
setCatalog(FALLBACK);
90+
return;
91+
}
92+
let cancelled = false;
93+
setCatalog(LOADING);
94+
readShareableObjects(doFetch).then(
95+
(objects) => {
96+
if (!cancelled) setCatalog(objects ? { status: 'listed', objects } : FALLBACK);
97+
},
98+
() => {
99+
if (!cancelled) setCatalog(FALLBACK);
100+
},
101+
);
102+
return () => {
103+
cancelled = true;
104+
};
105+
}, [doFetch, enabled]);
106+
return catalog;
107+
}
108+
109+
export function ShareableObjectRefField(props: FieldWidgetComponentProps<string>) {
110+
const { value, onChange, readonly, className, error, ...rest } = props;
111+
const ctx = React.useContext(SchemaRendererContext);
112+
const { t } = useFieldTranslation();
113+
const catalog = useShareableObjects(ctx?.apiFetch, !readonly);
114+
115+
const options = React.useMemo(() => {
116+
if (catalog.status !== 'listed') return [];
117+
const opts = catalog.objects.map((o) => ({ value: o.name, label: objectRefOptionLabel(o.name, o.label) }));
118+
if (value && !opts.some((o) => o.value === value)) opts.unshift({ value, label: value });
119+
return opts;
120+
}, [catalog, value]);
121+
122+
if (readonly || catalog.status === 'fallback') return <ObjectRefField {...props} />;
123+
124+
// DOM pass-through onto the combobox trigger, as `object-ref` does: `name`
125+
// is withheld because the trigger is a button, not a submission control.
126+
const { name: _domName, ...triggerDomProps } = toDomProps(rest);
127+
const loading = catalog.status === 'loading';
128+
129+
return (
130+
<Combobox
131+
{...triggerDomProps}
132+
options={options}
133+
value={value ?? ''}
134+
onValueChange={(v) => onChange(v)}
135+
placeholder={loading ? t('fields.objectRef.loading') : t('fields.objectRef.placeholder')}
136+
searchPlaceholder={t('fields.objectRef.search')}
137+
emptyText={loading ? t('common.loading') : t('fields.objectRef.empty')}
138+
disabled={rest.disabled}
139+
className={cn('w-full', className)}
140+
aria-invalid={!!error}
141+
/>
142+
);
143+
}
144+
145+
export default ShareableObjectRefField;

0 commit comments

Comments
 (0)