@proveanything/smartlinks 2.1.16 → 2.1.18
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 +85 -0
- package/dist/adminViews.js +259 -0
- package/dist/api/errors.d.ts +10 -0
- package/dist/api/errors.js +17 -0
- package/dist/api/index.d.ts +3 -0
- package/dist/api/index.js +3 -0
- package/dist/api/operations.d.ts +36 -0
- package/dist/api/operations.js +56 -0
- package/dist/api/order.d.ts +12 -1
- package/dist/api/order.js +15 -0
- package/dist/api/partner.d.ts +7 -1
- package/dist/api/partner.js +9 -0
- package/dist/api/userGroups.d.ts +78 -0
- package/dist/api/userGroups.js +124 -0
- package/dist/docs/API_SUMMARY.md +699 -46
- package/dist/docs/admin-views.md +125 -0
- package/dist/docs/app-manifest.md +2 -0
- package/dist/docs/app-objects.md +23 -0
- package/dist/docs/operations.md +281 -0
- package/dist/docs/overview.md +3 -0
- package/dist/docs/server-functions.md +106 -0
- package/dist/docs/tag-ranges.md +8 -0
- package/dist/docs/user-groups.md +168 -0
- package/dist/docs/utils.md +17 -0
- package/dist/http.js +4 -0
- package/dist/iframeResponder.d.ts +15 -0
- package/dist/iframeResponder.js +39 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/openapi.yaml +1426 -276
- package/dist/testing/index.d.ts +29 -0
- package/dist/testing/index.js +178 -1
- package/dist/types/appManifest.d.ts +170 -0
- package/dist/types/appObjects.d.ts +22 -1
- package/dist/types/claimSet.d.ts +2 -0
- package/dist/types/clientLog.d.ts +46 -0
- package/dist/types/clientLog.js +1 -0
- package/dist/types/containers.d.ts +8 -0
- package/dist/types/iframeResponder.d.ts +16 -1
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +3 -0
- package/dist/types/operations.d.ts +598 -0
- package/dist/types/operations.js +8 -0
- package/dist/types/order.d.ts +35 -6
- package/dist/types/userGroups.d.ts +103 -0
- package/dist/types/userGroups.js +5 -0
- package/dist/utils/conditions.d.ts +7 -0
- package/dist/utils/conditions.js +16 -9
- package/docs/API_SUMMARY.md +699 -46
- package/docs/admin-views.md +125 -0
- package/docs/app-manifest.md +2 -0
- package/docs/app-objects.md +23 -0
- package/docs/operations.md +281 -0
- package/docs/overview.md +3 -0
- package/docs/server-functions.md +106 -0
- package/docs/tag-ranges.md +8 -0
- package/docs/user-groups.md +168 -0
- package/docs/utils.md +17 -0
- package/openapi.yaml +1426 -276
- package/package.json +1 -1
|
@@ -0,0 +1,168 @@
|
|
|
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.
|
package/docs/utils.md
CHANGED
|
@@ -479,6 +479,23 @@ await utils.validateCondition({
|
|
|
479
479
|
|
|
480
480
|
User types: `'valid'`, `'invalid'`, `'owner'`, `'admin'`, `'group'`
|
|
481
481
|
|
|
482
|
+
`'group'` passes when the signed-in user is in at least one of the named [user groups](user-groups.md).
|
|
483
|
+
Name them in `groupIds` (keys or ids), and pass the user's own in `user.groups`:
|
|
484
|
+
|
|
485
|
+
```typescript
|
|
486
|
+
const { userGroups: keys, userGroupIds } = await userGroups.mine(collectionId)
|
|
487
|
+
|
|
488
|
+
await utils.validateCondition({
|
|
489
|
+
condition: {
|
|
490
|
+
type: 'and',
|
|
491
|
+
conditions: [{ type: 'user', userType: 'group', groupIds: ['qc-controller'] }]
|
|
492
|
+
},
|
|
493
|
+
user: { valid: true, uid: 'user123', groups: [...keys, ...userGroupIds] }
|
|
494
|
+
})
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
A condition only shows or hides. To protect an action, limit the server function behind it.
|
|
498
|
+
|
|
482
499
|
#### Date & Time Conditions
|
|
483
500
|
Show/hide content based on dates:
|
|
484
501
|
|