@proveanything/smartlinks 2.1.18 → 2.1.20
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/dist/adminViews.d.ts +19 -0
- package/dist/adminViews.js +16 -0
- package/dist/api/ai.d.ts +15 -5
- package/dist/api/ai.js +15 -9
- package/dist/api/aiGuide.d.ts +110 -0
- package/dist/api/aiGuide.js +162 -0
- package/dist/api/appObjects.d.ts +21 -0
- package/dist/api/appObjects.js +33 -1
- package/dist/api/authKit.d.ts +37 -1
- package/dist/api/authKit.js +51 -0
- package/dist/docs/API_SUMMARY.md +510 -40
- package/dist/docs/admin-lists-and-pickers.md +140 -0
- package/dist/docs/admin-views.md +22 -0
- package/dist/docs/app-data-storage.md +277 -276
- package/dist/docs/auth-kit.md +872 -741
- package/dist/docs/comms.md +686 -644
- package/dist/docs/customer-guide.md +70 -0
- package/dist/docs/headless-providers.md +112 -112
- package/dist/docs/server-functions.md +69 -1
- package/dist/docs/ui-utils.md +123 -121
- package/dist/docs/user-groups.md +175 -168
- package/dist/headless.js +38 -2
- package/dist/http.js +4 -0
- package/dist/index.d.ts +2 -2
- package/dist/live.d.ts +88 -0
- package/dist/live.js +357 -0
- package/dist/openapi.yaml +888 -34
- package/dist/testing/index.d.ts +22 -0
- package/dist/testing/index.js +31 -0
- package/dist/types/appManifest.d.ts +214 -1
- package/dist/types/appOptions.d.ts +32 -0
- package/dist/types/appOptions.js +3 -0
- package/dist/types/authKit.d.ts +193 -4
- package/dist/types/comms.d.ts +41 -2
- package/dist/types/headless.d.ts +49 -1
- package/dist/types/iframeResponder.d.ts +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/docs/API_SUMMARY.md +510 -40
- package/docs/admin-lists-and-pickers.md +140 -0
- package/docs/admin-views.md +22 -0
- package/docs/app-data-storage.md +277 -276
- package/docs/auth-kit.md +872 -741
- package/docs/comms.md +686 -644
- package/docs/customer-guide.md +70 -0
- package/docs/headless-providers.md +112 -112
- package/docs/server-functions.md +69 -1
- package/docs/ui-utils.md +123 -121
- package/docs/user-groups.md +175 -168
- package/openapi.yaml +888 -34
- package/package.json +1 -1
- package/scripts/doctor.mjs +454 -428
- package/scripts/lib/keyed-app-data.mjs +21 -0
package/docs/ui-utils.md
CHANGED
|
@@ -1,121 +1,123 @@
|
|
|
1
|
-
# UI Utils — `@proveanything/smartlinks-utils-ui`
|
|
2
|
-
|
|
3
|
-
The standardised, **admin-only** React toolkit for SmartLinks microapps. This page
|
|
4
|
-
is self-contained: the table and examples below are enough to build with. The
|
|
5
|
-
exhaustive per-component reference (props, slots, hooks, design tokens) ships
|
|
6
|
-
**inside the package** — read it from `node_modules/@proveanything/smartlinks-utils-ui/docs/`
|
|
7
|
-
after install. Nothing here depends on an external site.
|
|
8
|
-
|
|
9
|
-
- **NPM:** [`@proveanything/smartlinks-utils-ui`](https://www.npmjs.com/package/@proveanything/smartlinks-utils-ui)
|
|
10
|
-
- **Per-component reference:** `node_modules/@proveanything/smartlinks-utils-ui/docs/*.md`
|
|
11
|
-
(`records-admin-shell.md`, `admin-page-header.md`, `asset-picker.md`, `conditions-editor.md`,
|
|
12
|
-
`facet-rule-editor.md`, `link-picker.md`, `records-admin-hooks.md`, `public-consumption.md`, …)
|
|
13
|
-
- **Tracks:** `@proveanything/smartlinks ≥ 2.0` (peer dep)
|
|
14
|
-
|
|
15
|
-
## Admin is standardised; the consumer surface is bespoke
|
|
16
|
-
|
|
17
|
-
Two different design mandates, and the split is deliberate:
|
|
18
|
-
|
|
19
|
-
- **Admin surfaces** (the desktop `admin.html` a brand operator uses) are a
|
|
20
|
-
**product family** — like Salesforce: consistent, predictable chrome across every
|
|
21
|
-
app an operator opens. Do **not** hand-roll admin tables, editors or page headers.
|
|
22
|
-
Mount `RecordsAdminShell` for anything record-shaped; otherwise wrap the page in
|
|
23
|
-
`AdminPageHeader` and use the pickers. That consistency *is* the product.
|
|
24
|
-
- **Consumer / public surfaces** (the container, widgets, public pages the
|
|
25
|
-
end-customer sees) are **bespoke and brand-led** — entirely your design. utils-ui
|
|
26
|
-
belongs here only as read-side hooks (`useResolvedRecord`, `useResolveAllRecords`),
|
|
27
|
-
never as UI.
|
|
28
|
-
|
|
29
|
-
## Install
|
|
30
|
-
|
|
31
|
-
```bash
|
|
32
|
-
npm install @proveanything/smartlinks-utils-ui
|
|
33
|
-
# Peer deps you probably already have:
|
|
34
|
-
npm install react react-dom @proveanything/smartlinks @tanstack/react-query
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
Import the styles **once** in your admin entry (typically `src/admin-main.tsx`):
|
|
38
|
-
|
|
39
|
-
```ts
|
|
40
|
-
import '@proveanything/smartlinks-utils-ui/styles.css';
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
Components inherit your shadcn-compatible CSS variables (`--primary`,
|
|
44
|
-
`--background`, `--border`, …) — no extra theming required.
|
|
45
|
-
|
|
46
|
-
> **Admin-only.** Every component in this package calls the SDK with
|
|
47
|
-
> `admin: true` somewhere (save, upload, etc.). Never import it from a
|
|
48
|
-
> public widget or from `MobileAdminContainer`.
|
|
49
|
-
|
|
50
|
-
## What's in the box
|
|
51
|
-
|
|
52
|
-
| Module | Subpath import | Use it for |
|
|
53
|
-
|---|---|---|
|
|
54
|
-
| **RecordsAdminShell** | `/records-admin` | Full admin UI for the `app.records` pattern. Top-level scopes: **Global** (`'collection'`), **Rule** (`'rule'`, AND-of-OR over facets), and **Product** — with automatic variant & batch drill-down where the collection enables them. Browser pane, editor pane with sticky save/discard/delete, optimistic save, deep-linking, lifecycle hooks, conflict handling. Decide **cardinality** up front: `'singleton'` (one winning record per scope — warranty, nutrition) vs `'list'` (many records per scope — auction items, FAQs, gallery). See `docs/records-admin-shell.md`. |
|
|
55
|
-
| **Records hooks** | `/records-admin` | `useResolvedRecord`, `useMergedRecord`, `useCollectedRecords`, `useResolveAllRecords`, `useRulePreview`, … for the **public widget** side. See `docs/records-admin-hooks.md`. |
|
|
56
|
-
| **AdminPageHeader** | (root) | Standardised title / subtitle / icon / help / actions header. **Required** for any admin app that doesn't mount `RecordsAdminShell` (which embeds it). No bespoke admin chrome. |
|
|
57
|
-
| **AssetPicker** | `/asset-picker` | Pick / upload / paste / URL-import / AI-generate / stock-search / crop media assets. MIME filtering, scope-aware, tag editor. |
|
|
58
|
-
| **IconPicker** | `/icon-picker` | Searchable Font Awesome 7 Pro picker (requires the FA kit script on the host page). |
|
|
59
|
-
| **FontPicker** | `/font-picker` | Google Fonts catalogue + custom uploaded font families with live previews. |
|
|
60
|
-
| **ConditionsEditor** | `/conditions-editor` | Recursive AND/OR rule builder (12 condition types, facet-aware). |
|
|
61
|
-
| **FacetRuleEditor** | `/facet-rule-editor` | Author server-side facet rules (AND-of-OR) for record targeting. |
|
|
62
|
-
| **LinkPicker** | `/link-picker` | Universal navigation picker: external URL, an installed app, or a deep link inside an app. Stores a `LinkTarget` discriminated union — never a resolved URL. Ships with `resolveLink` and `useLinkTargets`. |
|
|
63
|
-
| **
|
|
64
|
-
| **
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
1
|
+
# UI Utils — `@proveanything/smartlinks-utils-ui`
|
|
2
|
+
|
|
3
|
+
The standardised, **admin-only** React toolkit for SmartLinks microapps. This page
|
|
4
|
+
is self-contained: the table and examples below are enough to build with. The
|
|
5
|
+
exhaustive per-component reference (props, slots, hooks, design tokens) ships
|
|
6
|
+
**inside the package** — read it from `node_modules/@proveanything/smartlinks-utils-ui/docs/`
|
|
7
|
+
after install. Nothing here depends on an external site.
|
|
8
|
+
|
|
9
|
+
- **NPM:** [`@proveanything/smartlinks-utils-ui`](https://www.npmjs.com/package/@proveanything/smartlinks-utils-ui)
|
|
10
|
+
- **Per-component reference:** `node_modules/@proveanything/smartlinks-utils-ui/docs/*.md`
|
|
11
|
+
(`records-admin-shell.md`, `admin-page-header.md`, `asset-picker.md`, `conditions-editor.md`,
|
|
12
|
+
`facet-rule-editor.md`, `link-picker.md`, `records-admin-hooks.md`, `public-consumption.md`, …)
|
|
13
|
+
- **Tracks:** `@proveanything/smartlinks ≥ 2.0` (peer dep)
|
|
14
|
+
|
|
15
|
+
## Admin is standardised; the consumer surface is bespoke
|
|
16
|
+
|
|
17
|
+
Two different design mandates, and the split is deliberate:
|
|
18
|
+
|
|
19
|
+
- **Admin surfaces** (the desktop `admin.html` a brand operator uses) are a
|
|
20
|
+
**product family** — like Salesforce: consistent, predictable chrome across every
|
|
21
|
+
app an operator opens. Do **not** hand-roll admin tables, editors or page headers.
|
|
22
|
+
Mount `RecordsAdminShell` for anything record-shaped; otherwise wrap the page in
|
|
23
|
+
`AdminPageHeader` and use the pickers. That consistency *is* the product.
|
|
24
|
+
- **Consumer / public surfaces** (the container, widgets, public pages the
|
|
25
|
+
end-customer sees) are **bespoke and brand-led** — entirely your design. utils-ui
|
|
26
|
+
belongs here only as read-side hooks (`useResolvedRecord`, `useResolveAllRecords`),
|
|
27
|
+
never as UI.
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install @proveanything/smartlinks-utils-ui
|
|
33
|
+
# Peer deps you probably already have:
|
|
34
|
+
npm install react react-dom @proveanything/smartlinks @tanstack/react-query
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Import the styles **once** in your admin entry (typically `src/admin-main.tsx`):
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import '@proveanything/smartlinks-utils-ui/styles.css';
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Components inherit your shadcn-compatible CSS variables (`--primary`,
|
|
44
|
+
`--background`, `--border`, …) — no extra theming required.
|
|
45
|
+
|
|
46
|
+
> **Admin-only.** Every component in this package calls the SDK with
|
|
47
|
+
> `admin: true` somewhere (save, upload, etc.). Never import it from a
|
|
48
|
+
> public widget or from `MobileAdminContainer`.
|
|
49
|
+
|
|
50
|
+
## What's in the box
|
|
51
|
+
|
|
52
|
+
| Module | Subpath import | Use it for |
|
|
53
|
+
|---|---|---|
|
|
54
|
+
| **RecordsAdminShell** | `/records-admin` | Full admin UI for the `app.records` pattern. Top-level scopes: **Global** (`'collection'`), **Rule** (`'rule'`, AND-of-OR over facets), and **Product** — with automatic variant & batch drill-down where the collection enables them. Browser pane, editor pane with sticky save/discard/delete, optimistic save, deep-linking, lifecycle hooks, conflict handling. Decide **cardinality** up front: `'singleton'` (one winning record per scope — warranty, nutrition) vs `'list'` (many records per scope — auction items, FAQs, gallery). See `docs/records-admin-shell.md`. |
|
|
55
|
+
| **Records hooks** | `/records-admin` | `useResolvedRecord`, `useMergedRecord`, `useCollectedRecords`, `useResolveAllRecords`, `useRulePreview`, … for the **public widget** side. See `docs/records-admin-hooks.md`. |
|
|
56
|
+
| **AdminPageHeader** | (root) | Standardised title / subtitle / icon / help / actions header. **Required** for any admin app that doesn't mount `RecordsAdminShell` (which embeds it). No bespoke admin chrome. |
|
|
57
|
+
| **AssetPicker** | `/asset-picker` | Pick / upload / paste / URL-import / AI-generate / stock-search / crop media assets. MIME filtering, scope-aware, tag editor. |
|
|
58
|
+
| **IconPicker** | `/icon-picker` | Searchable Font Awesome 7 Pro picker (requires the FA kit script on the host page). |
|
|
59
|
+
| **FontPicker** | `/font-picker` | Google Fonts catalogue + custom uploaded font families with live previews. |
|
|
60
|
+
| **ConditionsEditor** | `/conditions-editor` | Recursive AND/OR rule builder (12 condition types, facet-aware). |
|
|
61
|
+
| **FacetRuleEditor** | `/facet-rule-editor` | Author server-side facet rules (AND-of-OR) for record targeting. |
|
|
62
|
+
| **LinkPicker** | `/link-picker` | Universal navigation picker: external URL, an installed app, or a deep link inside an app. Stores a `LinkTarget` discriminated union — never a resolved URL. Ships with `resolveLink` and `useLinkTargets`. |
|
|
63
|
+
| **RecordPicker / ValuePicker** | `/option-picker` | Pick one of the app's records of a declared type, or a value from a value list. Reads the cached options call; switched-off entries stay labelled. See `docs/admin-lists-and-pickers.md`. |
|
|
64
|
+
| **SetupPanel** | `/setup-editor` | Generated list + form for every declared set-up type (`setup: true`) and editable value list. Use it instead of hand-building set-up tabs. |
|
|
65
|
+
| **Liquid editors** | `/liquid-editor` | Author Liquid templates with a code or rich (TipTap) editor, variable autocomplete. |
|
|
66
|
+
| **Hints** | (root) | `useHintsPreference`, `useIntroState`, `HintsPreferenceToggle` — global "show/hide intro hints" preference shared across admin apps. |
|
|
67
|
+
|
|
68
|
+
Each module has a per-subpath export so bundlers tree-shake the rest:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { RecordsAdminShell } from '@proveanything/smartlinks-utils-ui/records-admin';
|
|
72
|
+
import { AssetPicker } from '@proveanything/smartlinks-utils-ui/asset-picker';
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Minimal examples
|
|
76
|
+
|
|
77
|
+
### Records admin (most apps need this)
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
import { RecordsAdminShell } from '@proveanything/smartlinks-utils-ui/records-admin';
|
|
81
|
+
import * as SL from '@proveanything/smartlinks';
|
|
82
|
+
|
|
83
|
+
<RecordsAdminShell
|
|
84
|
+
SL={SL}
|
|
85
|
+
collectionId={collectionId}
|
|
86
|
+
appId={appId}
|
|
87
|
+
recordType="nutrition"
|
|
88
|
+
label="Nutrition info"
|
|
89
|
+
scopes={['collection', 'rule', 'product']} // canonical top-level tabs
|
|
90
|
+
// items={{ cardinality: 'list' }} // ← opt in for multi-item apps
|
|
91
|
+
defaultData={() => ({})}
|
|
92
|
+
renderEditor={(ctx) => <NutritionForm ctx={ctx} />}
|
|
93
|
+
/>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Public widget — read the resolved value
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import { useResolvedRecord } from '@proveanything/smartlinks-utils-ui/records-admin';
|
|
100
|
+
|
|
101
|
+
const { data, source } = useResolvedRecord({
|
|
102
|
+
SL, appId, recordType: 'nutrition',
|
|
103
|
+
collectionId, productId, variantId, batchId,
|
|
104
|
+
});
|
|
105
|
+
// source: 'proof' | 'batch' | 'variant' | 'product' | 'rule' | 'collection' | null
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
For multi-item (`cardinality: 'list'`) apps, use `useResolveAllRecords` /
|
|
109
|
+
`app.records.resolveAll()` instead — it returns every applicable record, not
|
|
110
|
+
just the winner.
|
|
111
|
+
|
|
112
|
+
For everything else (props, slots, cardinality decisions, deep-link adapters,
|
|
113
|
+
lifecycle hooks, conflict handling, design tokens) → **read the matching file in
|
|
114
|
+
`node_modules/@proveanything/smartlinks-utils-ui/docs/`**.
|
|
115
|
+
|
|
116
|
+
## Prerequisites
|
|
117
|
+
|
|
118
|
+
All components assume `SL.initializeApi()` has already run.
|
|
119
|
+
|
|
120
|
+
## Where to go next
|
|
121
|
+
|
|
122
|
+
- Per-component reference (shipped with the package): `node_modules/@proveanything/smartlinks-utils-ui/docs/`
|
|
123
|
+
- Records pattern (concepts, scope inheritance, resolution order): [`app-records-pattern.md`](./app-records-pattern.md)
|
package/docs/user-groups.md
CHANGED
|
@@ -1,168 +1,175 @@
|
|
|
1
|
-
# User groups
|
|
2
|
-
|
|
3
|
-
A **user group** is a named list of people in a collection: "QC Controllers", "Warehouse", "Approved
|
|
4
|
-
partners". Use them to say who may do what, for staff who aren't collection admins and for third
|
|
5
|
-
parties alike.
|
|
6
|
-
|
|
7
|
-
> There is no separate "roles" object. A role is a user group: "Sam has the QC role" and "Sam is in
|
|
8
|
-
> the QC Controllers user group" are the same fact.
|
|
9
|
-
|
|
10
|
-
## The three things you do with them
|
|
11
|
-
|
|
12
|
-
| You want to… | Use |
|
|
13
|
-
|---|---|
|
|
14
|
-
| **Protect** an action, so only some people can do it | `userGroups` on the server function's declaration |
|
|
15
|
-
| **Decide** something finer inside a function | `ctx.caller.userGroups` |
|
|
16
|
-
| **Show or hide** a button or a screen | `userGroups.mine(collectionId)`, or a `group` condition |
|
|
17
|
-
|
|
18
|
-
Only the first two protect anything. Hiding a button doesn't stop someone calling the function behind
|
|
19
|
-
it, so limit the function as well.
|
|
20
|
-
|
|
21
|
-
## A user group
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
{
|
|
25
|
-
id: 'k3Jd…', // random, never changes
|
|
26
|
-
key: 'qc-controller', // the name code uses; set once, kept through a rename
|
|
27
|
-
name: 'QC Controllers', // what people see; free to change
|
|
28
|
-
description: 'Sign off finished batches',
|
|
29
|
-
members: ['uid_1', 'uid_2'] // user ids
|
|
30
|
-
}
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
- **Refer to user groups by `key`** in code and manifests, never by name or id.
|
|
34
|
-
- A key is lowercase letters, digits and hyphens. It's made from the name when you don't give one
|
|
35
|
-
("QC Controllers" → `qc-controllers`), and it's unique in the collection.
|
|
36
|
-
- Members are people who sign in: on the site's own login, or with a SmartLinks account.
|
|
37
|
-
- Collection admins pass every user group check. They don't need adding.
|
|
38
|
-
|
|
39
|
-
## Limit a server function
|
|
40
|
-
|
|
41
|
-
In the manifest (see [server-functions.md](server-functions.md)):
|
|
42
|
-
|
|
43
|
-
```jsonc
|
|
44
|
-
{
|
|
45
|
-
"name": "releaseBatch",
|
|
46
|
-
"trigger": { "type": "http", "methods": ["POST"] },
|
|
47
|
-
"visibility": "public",
|
|
48
|
-
"authority": "collection",
|
|
49
|
-
"elevated": true,
|
|
50
|
-
"userGroups": ["qc-controller", "manager"], // in at least ONE of these
|
|
51
|
-
"capabilities": ["sl:records:write"]
|
|
52
|
-
}
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
The platform checks this before any of your code runs:
|
|
56
|
-
|
|
57
|
-
| Caller | Result |
|
|
58
|
-
|---|---|
|
|
59
|
-
| In one of the user groups, or a collection admin | The function runs |
|
|
60
|
-
| Signed in, in none of them | `403 { error: 'NOT_IN_USER_GROUP' }` |
|
|
61
|
-
| Not signed in | `401 { error: 'SIGN_IN_REQUIRED' }` |
|
|
62
|
-
|
|
63
|
-
- `http` functions only: `event` and `cron` functions have no caller.
|
|
64
|
-
- `userGroups` doesn't replace `elevated: true` on a `public` + `collection` function. Declare both.
|
|
65
|
-
|
|
66
|
-
## Decide inside a function
|
|
67
|
-
|
|
68
|
-
Every function sees the caller's keys:
|
|
69
|
-
|
|
70
|
-
```ts
|
|
71
|
-
export async function adjustQuantity(ctx, event) {
|
|
72
|
-
const isManager = ctx.caller.userGroups.includes('manager')
|
|
73
|
-
if (event.body.delta > 100 && !isManager) {
|
|
74
|
-
return new Response(JSON.stringify({ error: 'Over 100 needs a manager' }), { status: 403 })
|
|
75
|
-
}
|
|
76
|
-
// …
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
`ctx.caller.userGroups` is `[]` when nobody is signed in, and for `event` / `cron` runs.
|
|
81
|
-
|
|
82
|
-
## Show or hide in a screen
|
|
83
|
-
|
|
84
|
-
```ts
|
|
85
|
-
import { userGroups } from '@proveanything/smartlinks'
|
|
86
|
-
|
|
87
|
-
const { userGroups: mine } = await userGroups.mine(collectionId)
|
|
88
|
-
const showReleaseButton = mine.includes('qc-controller')
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
- `mine()` returns `{ userGroups: string[], userGroupIds: string[] }`: keys for code, and ids for
|
|
92
|
-
older saved conditions. Both are empty when nobody is signed in.
|
|
93
|
-
- It is never cached, so a change an admin makes shows on the next call.
|
|
94
|
-
- A person can only ask about themselves.
|
|
95
|
-
|
|
96
|
-
With conditions (see [utils.md](utils.md)):
|
|
97
|
-
|
|
98
|
-
```ts
|
|
99
|
-
const { userGroups: keys, userGroupIds } = await userGroups.mine(collectionId)
|
|
100
|
-
|
|
101
|
-
await utils.validateCondition({
|
|
102
|
-
condition: { type: 'and', conditions: [{ type: 'user', userType: 'group', groupIds: ['qc-controller'] }] },
|
|
103
|
-
user: { valid: true, uid, groups: [...keys, ...userGroupIds] },
|
|
104
|
-
})
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
## Manage them (admin)
|
|
108
|
-
|
|
109
|
-
```ts
|
|
110
|
-
const qc = await userGroups.create(collectionId, { name: 'QC Controllers' }) // key: 'qc-controllers'
|
|
111
|
-
|
|
112
|
-
await userGroups.addMember(collectionId, qc.id, { email: 'sam@example.com', name: 'Sam' })
|
|
113
|
-
await userGroups.addMember(collectionId, qc.id, { userId: 'uid_2' })
|
|
114
|
-
await userGroups.removeMember(collectionId, qc.id, 'uid_2')
|
|
115
|
-
|
|
116
|
-
await userGroups.members(collectionId, qc.id) // [{ uid, name, email }]
|
|
117
|
-
await userGroups.ofUser(collectionId, 'uid_1') // [{ id, key, name }]
|
|
118
|
-
await userGroups.list(collectionId)
|
|
119
|
-
await userGroups.update(collectionId, qc.id, { name: 'Quality control' }) // key stays 'qc-controllers'
|
|
120
|
-
await userGroups.remove(collectionId, qc.id)
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
- **Add and remove one person at a time** with `addMember` / `removeMember`. Saving a whole `members`
|
|
124
|
-
list through `update` can overwrite a change someone else made at the same moment.
|
|
125
|
-
- Adding by email finds the person in the collection's logins, or adds them. No welcome email is sent.
|
|
126
|
-
- A change takes up to a minute to reach function checks.
|
|
127
|
-
|
|
128
|
-
Errors: `KEY_INVALID`, `KEY_TAKEN` (409), `KEY_FIXED` (409, a key can't be changed once set),
|
|
129
|
-
`MISSING_FIELDS`, `NOT_FOUND`.
|
|
130
|
-
|
|
131
|
-
## Who changed what
|
|
132
|
-
|
|
133
|
-
Every change is recorded: a user group created, renamed or deleted, and each person added or removed,
|
|
134
|
-
with the admin who did it and when.
|
|
135
|
-
|
|
136
|
-
```ts
|
|
137
|
-
await userGroups.history(collectionId, { userGroupId: qc.id }) // one user group
|
|
138
|
-
await userGroups.history(collectionId, { userId: 'uid_1' }) // everything about one person
|
|
139
|
-
// [{ at, type: 'usergroup.member-added', actorId: 'uid_admin', data: { key: 'qc-controllers', userId: 'uid_1', … } }]
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
It covers the last 30 days unless you pass `from` / `to` (up to 400 days), and takes about a second.
|
|
143
|
-
Entries hold ids only, never names or emails.
|
|
144
|
-
|
|
145
|
-
## In a site
|
|
146
|
-
|
|
147
|
-
It works the same way. A site's functions run in the site's own worker, and the site's function
|
|
148
|
-
runtime applies `userGroups` from `public/app.manifest.json` before your handler runs, with the same
|
|
149
|
-
`401` / `403` answers. `ctx.caller.userId` and `ctx.caller.userGroups` are filled in for a signed-in
|
|
150
|
-
visitor.
|
|
151
|
-
|
|
152
|
-
A site made before this existed has no `src/site/lib/fn-access.ts`. There, `userGroups` in the
|
|
153
|
-
manifest is ignored until the site is updated.
|
|
154
|
-
|
|
155
|
-
## Testing
|
|
156
|
-
|
|
157
|
-
```ts
|
|
158
|
-
import { createFunctionTestContext } from '@proveanything/smartlinks/testing'
|
|
159
|
-
|
|
160
|
-
const ctx = createFunctionTestContext({
|
|
161
|
-
def: { trigger: { type: 'http' }, capabilities: ['sl:records:write'] },
|
|
162
|
-
caller: { userId: 'sam', userGroups: ['qc-controller'] },
|
|
163
|
-
})
|
|
164
|
-
ctx.caller.userGroups // ['qc-controller']
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
The test context gives your handler the caller you describe. It doesn't apply the declaration's
|
|
168
|
-
`userGroups` limit: that check happens on the platform before your handler is called.
|
|
1
|
+
# User groups
|
|
2
|
+
|
|
3
|
+
A **user group** is a named list of people in a collection: "QC Controllers", "Warehouse", "Approved
|
|
4
|
+
partners". Use them to say who may do what, for staff who aren't collection admins and for third
|
|
5
|
+
parties alike.
|
|
6
|
+
|
|
7
|
+
> There is no separate "roles" object. A role is a user group: "Sam has the QC role" and "Sam is in
|
|
8
|
+
> the QC Controllers user group" are the same fact.
|
|
9
|
+
|
|
10
|
+
## The three things you do with them
|
|
11
|
+
|
|
12
|
+
| You want to… | Use |
|
|
13
|
+
|---|---|
|
|
14
|
+
| **Protect** an action, so only some people can do it | `userGroups` on the server function's declaration |
|
|
15
|
+
| **Decide** something finer inside a function | `ctx.caller.userGroups` |
|
|
16
|
+
| **Show or hide** a button or a screen | `userGroups.mine(collectionId)`, or a `group` condition |
|
|
17
|
+
|
|
18
|
+
Only the first two protect anything. Hiding a button doesn't stop someone calling the function behind
|
|
19
|
+
it, so limit the function as well.
|
|
20
|
+
|
|
21
|
+
## A user group
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
{
|
|
25
|
+
id: 'k3Jd…', // random, never changes
|
|
26
|
+
key: 'qc-controller', // the name code uses; set once, kept through a rename
|
|
27
|
+
name: 'QC Controllers', // what people see; free to change
|
|
28
|
+
description: 'Sign off finished batches',
|
|
29
|
+
members: ['uid_1', 'uid_2'] // user ids
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- **Refer to user groups by `key`** in code and manifests, never by name or id.
|
|
34
|
+
- A key is lowercase letters, digits and hyphens. It's made from the name when you don't give one
|
|
35
|
+
("QC Controllers" → `qc-controllers`), and it's unique in the collection.
|
|
36
|
+
- Members are people who sign in: on the site's own login, or with a SmartLinks account.
|
|
37
|
+
- Collection admins pass every user group check. They don't need adding.
|
|
38
|
+
|
|
39
|
+
## Limit a server function
|
|
40
|
+
|
|
41
|
+
In the manifest (see [server-functions.md](server-functions.md)):
|
|
42
|
+
|
|
43
|
+
```jsonc
|
|
44
|
+
{
|
|
45
|
+
"name": "releaseBatch",
|
|
46
|
+
"trigger": { "type": "http", "methods": ["POST"] },
|
|
47
|
+
"visibility": "public",
|
|
48
|
+
"authority": "collection",
|
|
49
|
+
"elevated": true,
|
|
50
|
+
"userGroups": ["qc-controller", "manager"], // in at least ONE of these
|
|
51
|
+
"capabilities": ["sl:records:write"]
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The platform checks this before any of your code runs:
|
|
56
|
+
|
|
57
|
+
| Caller | Result |
|
|
58
|
+
|---|---|
|
|
59
|
+
| In one of the user groups, or a collection admin | The function runs |
|
|
60
|
+
| Signed in, in none of them | `403 { error: 'NOT_IN_USER_GROUP' }` |
|
|
61
|
+
| Not signed in | `401 { error: 'SIGN_IN_REQUIRED' }` |
|
|
62
|
+
|
|
63
|
+
- `http` functions only: `event` and `cron` functions have no caller.
|
|
64
|
+
- `userGroups` doesn't replace `elevated: true` on a `public` + `collection` function. Declare both.
|
|
65
|
+
|
|
66
|
+
## Decide inside a function
|
|
67
|
+
|
|
68
|
+
Every function sees the caller's keys:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
export async function adjustQuantity(ctx, event) {
|
|
72
|
+
const isManager = ctx.caller.userGroups.includes('manager')
|
|
73
|
+
if (event.body.delta > 100 && !isManager) {
|
|
74
|
+
return new Response(JSON.stringify({ error: 'Over 100 needs a manager' }), { status: 403 })
|
|
75
|
+
}
|
|
76
|
+
// …
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`ctx.caller.userGroups` is `[]` when nobody is signed in, and for `event` / `cron` runs.
|
|
81
|
+
|
|
82
|
+
## Show or hide in a screen
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { userGroups } from '@proveanything/smartlinks'
|
|
86
|
+
|
|
87
|
+
const { userGroups: mine } = await userGroups.mine(collectionId)
|
|
88
|
+
const showReleaseButton = mine.includes('qc-controller')
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
- `mine()` returns `{ userGroups: string[], userGroupIds: string[] }`: keys for code, and ids for
|
|
92
|
+
older saved conditions. Both are empty when nobody is signed in.
|
|
93
|
+
- It is never cached, so a change an admin makes shows on the next call.
|
|
94
|
+
- A person can only ask about themselves.
|
|
95
|
+
|
|
96
|
+
With conditions (see [utils.md](utils.md)):
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
const { userGroups: keys, userGroupIds } = await userGroups.mine(collectionId)
|
|
100
|
+
|
|
101
|
+
await utils.validateCondition({
|
|
102
|
+
condition: { type: 'and', conditions: [{ type: 'user', userType: 'group', groupIds: ['qc-controller'] }] },
|
|
103
|
+
user: { valid: true, uid, groups: [...keys, ...userGroupIds] },
|
|
104
|
+
})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Manage them (admin)
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
const qc = await userGroups.create(collectionId, { name: 'QC Controllers' }) // key: 'qc-controllers'
|
|
111
|
+
|
|
112
|
+
await userGroups.addMember(collectionId, qc.id, { email: 'sam@example.com', name: 'Sam' })
|
|
113
|
+
await userGroups.addMember(collectionId, qc.id, { userId: 'uid_2' })
|
|
114
|
+
await userGroups.removeMember(collectionId, qc.id, 'uid_2')
|
|
115
|
+
|
|
116
|
+
await userGroups.members(collectionId, qc.id) // [{ uid, name, email }]
|
|
117
|
+
await userGroups.ofUser(collectionId, 'uid_1') // [{ id, key, name }]
|
|
118
|
+
await userGroups.list(collectionId)
|
|
119
|
+
await userGroups.update(collectionId, qc.id, { name: 'Quality control' }) // key stays 'qc-controllers'
|
|
120
|
+
await userGroups.remove(collectionId, qc.id)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
- **Add and remove one person at a time** with `addMember` / `removeMember`. Saving a whole `members`
|
|
124
|
+
list through `update` can overwrite a change someone else made at the same moment.
|
|
125
|
+
- Adding by email finds the person in the collection's logins, or adds them. No welcome email is sent.
|
|
126
|
+
- A change takes up to a minute to reach function checks.
|
|
127
|
+
|
|
128
|
+
Errors: `KEY_INVALID`, `KEY_TAKEN` (409), `KEY_FIXED` (409, a key can't be changed once set),
|
|
129
|
+
`MISSING_FIELDS`, `NOT_FOUND`.
|
|
130
|
+
|
|
131
|
+
## Who changed what
|
|
132
|
+
|
|
133
|
+
Every change is recorded: a user group created, renamed or deleted, and each person added or removed,
|
|
134
|
+
with the admin who did it and when.
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
await userGroups.history(collectionId, { userGroupId: qc.id }) // one user group
|
|
138
|
+
await userGroups.history(collectionId, { userId: 'uid_1' }) // everything about one person
|
|
139
|
+
// [{ at, type: 'usergroup.member-added', actorId: 'uid_admin', data: { key: 'qc-controllers', userId: 'uid_1', … } }]
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
It covers the last 30 days unless you pass `from` / `to` (up to 400 days), and takes about a second.
|
|
143
|
+
Entries hold ids only, never names or emails.
|
|
144
|
+
|
|
145
|
+
## In a site
|
|
146
|
+
|
|
147
|
+
It works the same way. A site's functions run in the site's own worker, and the site's function
|
|
148
|
+
runtime applies `userGroups` from `public/app.manifest.json` before your handler runs, with the same
|
|
149
|
+
`401` / `403` answers. `ctx.caller.userId` and `ctx.caller.userGroups` are filled in for a signed-in
|
|
150
|
+
visitor.
|
|
151
|
+
|
|
152
|
+
A site made before this existed has no `src/site/lib/fn-access.ts`. There, `userGroups` in the
|
|
153
|
+
manifest is ignored until the site is updated.
|
|
154
|
+
|
|
155
|
+
## Testing
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import { createFunctionTestContext } from '@proveanything/smartlinks/testing'
|
|
159
|
+
|
|
160
|
+
const ctx = createFunctionTestContext({
|
|
161
|
+
def: { trigger: { type: 'http' }, capabilities: ['sl:records:write'] },
|
|
162
|
+
caller: { userId: 'sam', userGroups: ['qc-controller'] },
|
|
163
|
+
})
|
|
164
|
+
ctx.caller.userGroups // ['qc-controller']
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
The test context gives your handler the caller you describe. It doesn't apply the declaration's
|
|
168
|
+
`userGroups` limit: that check happens on the platform before your handler is called.
|
|
169
|
+
|
|
170
|
+
## Who is in a user group (server code)
|
|
171
|
+
|
|
172
|
+
`ctx.sl.userGroups.members(keys?)` lists the people in the named user groups (every keyed group when
|
|
173
|
+
`keys` is empty): `[{ userId, name, email, userGroups }]`, sorted by name, at most 500. For staff lists
|
|
174
|
+
("who is on QC") and for addressing a team (a notification to every Manager). Needs
|
|
175
|
+
`sl:usergroups:read` and collection authority. Members whose account no longer exists are left out.
|