@quitecustard/payload 0.3.1 → 0.3.2
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.
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Field, FieldAccess, RelationshipField } from 'payload';
|
|
1
|
+
import type { Field, FieldAccess, RelationshipField, TypedUser } from 'payload';
|
|
2
|
+
type GetUserId = (user: TypedUser) => unknown;
|
|
2
3
|
/**
|
|
3
4
|
* A relationship field to the auth collection, auto-stamped to the creating user: forced on create,
|
|
4
5
|
* and refused on update by field-level access, so ownership can't be reassigned through the admin
|
|
@@ -11,9 +12,16 @@ import type { Field, FieldAccess, RelationshipField } from 'payload';
|
|
|
11
12
|
* ```ts
|
|
12
13
|
* ownerField({ relationTo: 'users' }, { readAccess: staffFieldLevel })
|
|
13
14
|
* ```
|
|
15
|
+
*
|
|
16
|
+
* `getUserId` resolves the id to stamp from the caller, defaulting to `user.id`. A project with
|
|
17
|
+
* more than one auth collection passes its own, returning null for a caller that is not an
|
|
18
|
+
* account `relationTo` can point at - an API-key client, say - so that caller's id is never
|
|
19
|
+
* written into a relationship to a different collection.
|
|
14
20
|
*/
|
|
15
|
-
export declare const ownerField: ({ relationTo }: {
|
|
21
|
+
export declare const ownerField: ({ relationTo, getUserId, }: {
|
|
16
22
|
relationTo: RelationshipField["relationTo"];
|
|
23
|
+
getUserId?: GetUserId;
|
|
17
24
|
}, { readAccess, ...overrides }?: Partial<Omit<RelationshipField, "type" | "relationTo">> & {
|
|
18
25
|
readAccess?: FieldAccess;
|
|
19
26
|
}) => Field;
|
|
27
|
+
export {};
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
const
|
|
1
|
+
const defaultUserId = (user) => user.id;
|
|
2
|
+
const makeSetOwner = (getUserId) => {
|
|
2
3
|
const setOwner = ({ req, operation, value }) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
if (operation !== 'create')
|
|
5
|
+
return value;
|
|
6
|
+
const user = req?.user;
|
|
7
|
+
// No caller at all: a Local API create (a seed, a migration, an import) carries no user, and
|
|
8
|
+
// discarding an explicitly supplied owner there would leave the row unowned.
|
|
9
|
+
if (!user)
|
|
10
|
+
return value ?? null;
|
|
11
|
+
// A caller who is not an owner-able account (`getUserId` answers null) stamps nobody. The
|
|
12
|
+
// supplied value is NOT a fallback here, or that caller could name any owner it liked.
|
|
13
|
+
return getUserId(user) ?? null;
|
|
8
14
|
};
|
|
9
15
|
return setOwner;
|
|
10
16
|
};
|
|
@@ -20,8 +26,13 @@ const makeSetOwner = () => {
|
|
|
20
26
|
* ```ts
|
|
21
27
|
* ownerField({ relationTo: 'users' }, { readAccess: staffFieldLevel })
|
|
22
28
|
* ```
|
|
29
|
+
*
|
|
30
|
+
* `getUserId` resolves the id to stamp from the caller, defaulting to `user.id`. A project with
|
|
31
|
+
* more than one auth collection passes its own, returning null for a caller that is not an
|
|
32
|
+
* account `relationTo` can point at - an API-key client, say - so that caller's id is never
|
|
33
|
+
* written into a relationship to a different collection.
|
|
23
34
|
*/
|
|
24
|
-
export const ownerField = ({ relationTo }, { readAccess, ...overrides } = {}) => {
|
|
35
|
+
export const ownerField = ({ relationTo, getUserId = defaultUserId, }, { readAccess, ...overrides } = {}) => {
|
|
25
36
|
const { admin, hooks, access, ...rest } = overrides;
|
|
26
37
|
return {
|
|
27
38
|
name: 'owner',
|
|
@@ -42,7 +53,7 @@ export const ownerField = ({ relationTo }, { readAccess, ...overrides } = {}) =>
|
|
|
42
53
|
...admin,
|
|
43
54
|
},
|
|
44
55
|
hooks: {
|
|
45
|
-
beforeChange: [makeSetOwner()],
|
|
56
|
+
beforeChange: [makeSetOwner(getUserId)],
|
|
46
57
|
...hooks,
|
|
47
58
|
},
|
|
48
59
|
...rest,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quitecustard/payload",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Payload v3 helpers: relation narrowing, transactions, autosave-vs-unpublish detection, sync locks, unique slugs and usernames",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"payload",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@quitecustard/core": "^0.
|
|
41
|
+
"@quitecustard/core": "^0.4.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"@payloadcms/drizzle": "^3.0.0",
|