@extantape/payload-plugin-membership 0.4.0 → 0.5.1
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/README.md +16 -3
- package/dist/access.d.ts +32 -1
- package/dist/access.js +70 -14
- package/dist/collections.js +28 -26
- package/dist/endpoints.js +8 -7
- package/dist/guard.js +34 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,12 +29,16 @@ Patreon, or let members support **categories** with **their own amount** — or
|
|
|
29
29
|
|
|
30
30
|
**On its own.** Membership needs a Stripe account (a secret key and a webhook) and an auth collection for members. It doesn't import any other plugin. Perks, votes and credits are whatever your own `onPayment` and `onStatusChange` do.
|
|
31
31
|
|
|
32
|
+
**Needs.** Payload `^3.94.2` as a peer dependency. No other EXTANT APE plugin is required.
|
|
33
|
+
|
|
32
34
|
**Works with:**
|
|
33
35
|
|
|
34
36
|
- **Sponsors** (`@extantape/payload-plugin-sponsors`): tick "Members are listed as sponsors" on a tier, and call Sponsors' `upsertListing()` and `retireListing()` from `onStatusChange`. Your site makes the call; neither plugin imports the other.
|
|
35
37
|
- **Roadmap** (`@extantape/payload-plugin-roadmap`): Roadmap's extra votes come from a number your site works out (`allocation.earned`), and a membership can be where they come from. There's no adapter for this. It's a few lines of your own code.
|
|
36
38
|
- **Multi-tenant** (`@payloadcms/plugin-multi-tenant`): set `scopeField` and `scopeOf`, and each site has its own tiers and members. The member endpoints answer only for the site `scopeOf` names: another site's membership, or a request from a site `scopeOf` doesn't know, gets a 404, and so does a request where `scopeOf` throws (only the error's name is logged). Managers also need the collection's read access, so the tenant plugin's rules apply to them. See [Sites](#sites) for what `scopeOf` returns and how `getBackers` and `myMembership` take a site.
|
|
37
39
|
|
|
40
|
+
**Color Theory.** None needed. It has no screens of its own, and its collections use Payload's standard admin, which takes Color Theory's admin colors when its `applyToAdmin` option is on.
|
|
41
|
+
|
|
38
42
|
**Family.** Membership is in the Patronage family, with Sponsors, Backing, Credits, Printify and Roadmap. Backing and Credits are named but not built yet, and the families are a working draft that may change.
|
|
39
43
|
|
|
40
44
|
## Install
|
|
@@ -124,8 +128,9 @@ refuses to build with only one of them.
|
|
|
124
128
|
plugin applies it through each payment's `membership`.
|
|
125
129
|
- Anything else is no: `false`, `null`, `0`, `''`, `1`, `'yes'`, an array, an empty `{}` or `{ and: [] }`,
|
|
126
130
|
a field with no operator, an operator Payload doesn't have or the plugin doesn't take (a typo such as
|
|
127
|
-
`equal` included), a value that would match every row (`like
|
|
128
|
-
`all: []`), a query that doesn't run against the memberships collection
|
|
131
|
+
`equal` included), a value that would match every row (`like` or `contains` with nothing or only whitespace, `not_in: []`,
|
|
132
|
+
`all: []`), a query that doesn't run against the memberships collection or against the tiers,
|
|
133
|
+
categories or payments it is used on (an unknown field), or an
|
|
129
134
|
error thrown or rejected. The request is refused cleanly, never a 500. An empty `{}` would match
|
|
130
135
|
every row, so it is a no. Return `true` to mean all. The accepted operators are `equals`,
|
|
131
136
|
`not_equals`, `in`, `not_in`, `all`, `exists`, `greater_than`, `greater_than_equal`, `less_than`,
|
|
@@ -163,6 +168,7 @@ example a membership they hold as a member there).
|
|
|
163
168
|
| Payments | Reads and deletes within their scope | Reads their own | Nothing |
|
|
164
169
|
| `cancel` endpoint | A membership within their scope, on this request's site | Their own | 404 |
|
|
165
170
|
| `wall` endpoint | Only to hide (`show: false`), within their scope | Their own, show and name | 404 |
|
|
171
|
+
| A membership's `wall.show` (field, the admin form included) | Only to hide, within their scope | Through the `wall` endpoint | Nothing |
|
|
166
172
|
| `change`, `portal` endpoints | Never | Their own | 404 |
|
|
167
173
|
|
|
168
174
|
Only the member changes their own plan, opens their own Stripe billing portal, shows themselves on the
|
|
@@ -173,7 +179,14 @@ request). Managers keep `cancel`. (Decided 2026-11-23.)
|
|
|
173
179
|
This is enforced in field access too, not just in the endpoints, so editing a membership through
|
|
174
180
|
Payload's own REST, GraphQL or admin can't get around it. A manager can't write a membership's `user`,
|
|
175
181
|
`kind`, `status`, `interval`, `tier`, `pledges`, `amount`, `currency`, `label`, the period dates or the
|
|
176
|
-
`stripe` group, or `wall.name`, and can only set `wall.show` to `false`. Only `notes` is theirs to edit.
|
|
182
|
+
`stripe` group, or `wall.name`, and can only set `wall.show` to `false`. Only `notes` is theirs to edit, plus hiding a wall entry.
|
|
183
|
+
|
|
184
|
+
A manager of a membership hides its wall entry in the admin form by clearing the `show` box and
|
|
185
|
+
saving. The box is open to them only while the entry is shown. Once it is hidden the box is
|
|
186
|
+
read-only, and only the member can show it again, through the wall endpoint. The wall endpoint
|
|
187
|
+
(`POST /api/memberships/:id/wall` with `show: false`), REST and the Local API hide an entry too. One
|
|
188
|
+
check decides all of them, so the admin form allows what the wall endpoint allows. A save
|
|
189
|
+
from a manager that sets `show` to `true` leaves the entry hidden and saves the other fields.
|
|
177
190
|
Server code that writes with `overrideAccess` (checkout, Stripe events) is unchanged. A manager can
|
|
178
191
|
set a category's `parent` only to a category in their scope. That is judged on the parent the row
|
|
179
192
|
ends up with, whether the save is a create, an update, a duplicate (which keeps the original's parent
|
package/dist/access.d.ts
CHANGED
|
@@ -15,7 +15,10 @@ export declare function usableWhere(w: unknown, depth?: number): w is Where;
|
|
|
15
15
|
* and anyone not signed in, is `null`.
|
|
16
16
|
*/
|
|
17
17
|
export type ManagerScope = true | Where | null;
|
|
18
|
-
export declare function managerScope(o: Pick<ResolvedMembership, 'canManage' | 'memberships'>, req: PayloadRequest
|
|
18
|
+
export declare function managerScope(o: Pick<ResolvedMembership, 'canManage' | 'memberships'>, req: PayloadRequest, on?: {
|
|
19
|
+
collection: string;
|
|
20
|
+
via?: (w: Where) => Where;
|
|
21
|
+
}): Promise<ManagerScope>;
|
|
19
22
|
/** A manager of anything at all (a scope or all): for callers that go on to check a particular row. */
|
|
20
23
|
export declare function isManager(o: Pick<ResolvedMembership, 'canManage' | 'memberships'>, req: PayloadRequest): Promise<boolean>;
|
|
21
24
|
/** The query as it applies to payments: written against memberships, so every field goes through `membership`. */
|
|
@@ -35,5 +38,33 @@ export declare function matchesWhere(where: Where, data: Record<string, unknown>
|
|
|
35
38
|
* (checked against the database), nobody else. The answer is kept for the request.
|
|
36
39
|
*/
|
|
37
40
|
export declare function managerReaches(o: Pick<ResolvedMembership, 'canManage' | 'memberships'>, req: PayloadRequest, collection: string, id: unknown, via?: (w: Where) => Where): Promise<boolean>;
|
|
41
|
+
/** Who the signed-in user is to one membership: its member, a manager who reaches it, or nobody. */
|
|
42
|
+
export type MembershipRole = 'member' | 'manager' | null;
|
|
43
|
+
type Row = {
|
|
44
|
+
id?: unknown;
|
|
45
|
+
user?: unknown;
|
|
46
|
+
} | null | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* The one answer to who someone is to a membership. The member's own row comes first, so a manager
|
|
49
|
+
* who is also the member counts as the member. A manager is judged on this very row.
|
|
50
|
+
*/
|
|
51
|
+
export declare function membershipRole(o: Pick<ResolvedMembership, 'canManage' | 'memberships'>, req: PayloadRequest, row: Row): Promise<MembershipRole>;
|
|
52
|
+
/**
|
|
53
|
+
* The one rule for a membership's `wall.show`, used by the wall endpoint and by the field's access
|
|
54
|
+
* (REST, GraphQL, the Local API and the admin form): the member may set anything, a manager who
|
|
55
|
+
* reaches the row may only hide it (exactly `false`), and nobody else may change it.
|
|
56
|
+
*/
|
|
57
|
+
export declare function wallShowChange(o: Pick<ResolvedMembership, 'canManage' | 'memberships'>, req: PayloadRequest, row: Row, show: unknown): Promise<{
|
|
58
|
+
role: MembershipRole;
|
|
59
|
+
allowed: boolean;
|
|
60
|
+
}>;
|
|
61
|
+
/**
|
|
62
|
+
* Whether the admin form's show box is open to this user for a stored row. A checkbox can only flip
|
|
63
|
+
* the stored value, so the answer is `wallShowChange` for the opposite of what is stored: a manager
|
|
64
|
+
* gets the box while the entry is shown and loses it once it is hidden. The stored row is read here,
|
|
65
|
+
* because the data Payload passes at this point can be unsaved form values. A row that can't be read is no.
|
|
66
|
+
*/
|
|
67
|
+
export declare function wallShowOpen(o: Pick<ResolvedMembership, 'canManage' | 'memberships'>, req: PayloadRequest, id: unknown): Promise<boolean>;
|
|
38
68
|
/** Checkout is allowed here: no `canCheckout`, or `canCheckout` answered exactly `true`. */
|
|
39
69
|
export declare const checkoutAllowed: (o: Pick<ResolvedMembership, "canCheckout">, payload: Payload) => Promise<boolean>;
|
|
70
|
+
export {};
|
package/dist/access.js
CHANGED
|
@@ -15,7 +15,8 @@ const isPlain = (v) => {
|
|
|
15
15
|
};
|
|
16
16
|
const scalar = (v) => v === null || ['string', 'number', 'boolean'].includes(typeof v);
|
|
17
17
|
const scalars = (v, minimum) => Array.isArray(v) && v.length >= minimum && v.every(scalar);
|
|
18
|
-
|
|
18
|
+
// Whitespace and zero-width characters alone would match nearly every row, so they don't count as text.
|
|
19
|
+
const nonEmptyText = (v) => typeof v === 'string' && /[^\s\u200b-\u200d\u2060\ufeff]/.test(v);
|
|
19
20
|
const number = (v) => typeof v === 'number' || typeof v === 'string';
|
|
20
21
|
/**
|
|
21
22
|
* The operators the plugin accepts, each with a check on its value. An unknown operator (a typo
|
|
@@ -57,7 +58,7 @@ export function usableWhere(w, depth = 0) {
|
|
|
57
58
|
return ops.length > 0 && ops.every(([op, arg]) => Object.hasOwn(OPERATORS, op) && OPERATORS[op](arg));
|
|
58
59
|
});
|
|
59
60
|
}
|
|
60
|
-
export async function managerScope(o, req) {
|
|
61
|
+
export async function managerScope(o, req, on) {
|
|
61
62
|
if (!req.user)
|
|
62
63
|
return null;
|
|
63
64
|
try {
|
|
@@ -66,19 +67,27 @@ export async function managerScope(o, req) {
|
|
|
66
67
|
return true;
|
|
67
68
|
if (!usableWhere(answer))
|
|
68
69
|
return null;
|
|
70
|
+
// The query must run on the memberships collection and on the collection it is used on (a field
|
|
71
|
+
// that only memberships have can't be queried on tiers or categories), or the answer is no.
|
|
69
72
|
const checked = (req.context.membershipWhereChecked ??= new Map());
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
const runsOn = async (collection, where) => {
|
|
74
|
+
const key = `${collection}:${JSON.stringify(where)}`;
|
|
75
|
+
let ok = checked.get(key);
|
|
76
|
+
if (ok === undefined) {
|
|
77
|
+
try {
|
|
78
|
+
await req.payload.count({ collection: collection, where, overrideAccess: true, req });
|
|
79
|
+
ok = true;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
ok = false;
|
|
83
|
+
}
|
|
84
|
+
checked.set(key, ok);
|
|
76
85
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
86
|
+
return ok;
|
|
87
|
+
};
|
|
88
|
+
let ok = await runsOn(o.memberships, answer);
|
|
89
|
+
if (ok && on && on.collection !== o.memberships)
|
|
90
|
+
ok = await runsOn(on.collection, on.via ? on.via(answer) : answer);
|
|
82
91
|
return ok ? answer : null;
|
|
83
92
|
}
|
|
84
93
|
catch {
|
|
@@ -152,7 +161,7 @@ export function matchesWhere(where, data) {
|
|
|
152
161
|
* (checked against the database), nobody else. The answer is kept for the request.
|
|
153
162
|
*/
|
|
154
163
|
export async function managerReaches(o, req, collection, id, via = (w) => w) {
|
|
155
|
-
const scope = await managerScope(o, req);
|
|
164
|
+
const scope = await managerScope(o, req, { collection, via });
|
|
156
165
|
if (scope === true)
|
|
157
166
|
return true;
|
|
158
167
|
if (!scope || id == null || id === '')
|
|
@@ -178,5 +187,52 @@ export async function managerReaches(o, req, collection, id, via = (w) => w) {
|
|
|
178
187
|
cache.set(key, reached);
|
|
179
188
|
return reached;
|
|
180
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* The one answer to who someone is to a membership. The member's own row comes first, so a manager
|
|
192
|
+
* who is also the member counts as the member. A manager is judged on this very row.
|
|
193
|
+
*/
|
|
194
|
+
export async function membershipRole(o, req, row) {
|
|
195
|
+
if (!req.user || !row)
|
|
196
|
+
return null;
|
|
197
|
+
if (same(row.user, req.user.id))
|
|
198
|
+
return 'member';
|
|
199
|
+
return row.id != null && (await managerReaches(o, req, o.memberships, row.id)) ? 'manager' : null;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The one rule for a membership's `wall.show`, used by the wall endpoint and by the field's access
|
|
203
|
+
* (REST, GraphQL, the Local API and the admin form): the member may set anything, a manager who
|
|
204
|
+
* reaches the row may only hide it (exactly `false`), and nobody else may change it.
|
|
205
|
+
*/
|
|
206
|
+
export async function wallShowChange(o, req, row, show) {
|
|
207
|
+
const role = await membershipRole(o, req, row);
|
|
208
|
+
return { role, allowed: role === 'member' || (role === 'manager' && show === false) };
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Whether the admin form's show box is open to this user for a stored row. A checkbox can only flip
|
|
212
|
+
* the stored value, so the answer is `wallShowChange` for the opposite of what is stored: a manager
|
|
213
|
+
* gets the box while the entry is shown and loses it once it is hidden. The stored row is read here,
|
|
214
|
+
* because the data Payload passes at this point can be unsaved form values. A row that can't be read is no.
|
|
215
|
+
*/
|
|
216
|
+
export async function wallShowOpen(o, req, id) {
|
|
217
|
+
if (!req.user || id == null || id === '')
|
|
218
|
+
return false;
|
|
219
|
+
try {
|
|
220
|
+
const stored = (await req.payload.findByID({
|
|
221
|
+
collection: o.memberships,
|
|
222
|
+
id: id,
|
|
223
|
+
depth: 0,
|
|
224
|
+
overrideAccess: true,
|
|
225
|
+
disableErrors: true,
|
|
226
|
+
select: { user: true, wall: true },
|
|
227
|
+
req,
|
|
228
|
+
}));
|
|
229
|
+
if (!stored)
|
|
230
|
+
return false;
|
|
231
|
+
return (await wallShowChange(o, req, stored, stored.wall?.show !== true)).allowed;
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
181
237
|
/** Checkout is allowed here: no `canCheckout`, or `canCheckout` answered exactly `true`. */
|
|
182
238
|
export const checkoutAllowed = (o, payload) => o.canCheckout ? exactlyTrue(() => o.canCheckout({ payload })) : Promise.resolve(true);
|
package/dist/collections.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { APIError } from 'payload';
|
|
2
2
|
import { generateKeyBetween } from 'payload/shared';
|
|
3
|
-
import { inVersions, managerReaches, managerScope, matchesWhere, viaMembership } from './access.js';
|
|
3
|
+
import { inVersions, managerReaches, managerScope, matchesWhere, viaMembership, wallShowChange, wallShowOpen } from './access.js';
|
|
4
4
|
const idOf = (v) => (v && typeof v === 'object' ? String(v.id) : v ? String(v) : null);
|
|
5
5
|
/** Drag-and-drop ordered (Payload `orderable`); a new record goes between its alphabetical neighbours. */
|
|
6
6
|
const placeAlphabetically = (o, slug, field) => async ({ data, operation, req }) => {
|
|
@@ -47,29 +47,31 @@ const stripeProductField = (read) => ({
|
|
|
47
47
|
const fresh = (value = null) => ({ beforeDuplicate: [() => value] });
|
|
48
48
|
/** Only the member whose row it is. */
|
|
49
49
|
const memberOnly = ({ req, doc }) => Boolean(req.user) && String(idOf(doc?.user)) === String(req.user.id);
|
|
50
|
-
/**
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Update access for `wall.show`, by the rule the wall endpoint uses (`wallShowChange`): the member
|
|
52
|
+
* sets anything, a manager who reaches the row may only hide it (`false`), e.g. for an offensive name
|
|
53
|
+
* or a takedown request. On a save Payload passes the incoming group as `siblingData`, and the new
|
|
54
|
+
* value is judged. When Payload works out what the admin form may edit it passes no `siblingData`,
|
|
55
|
+
* and the box is open when flipping the stored value would be allowed.
|
|
56
|
+
*/
|
|
57
|
+
const wallShow = (o) => async ({ req, id, doc, siblingData }) => {
|
|
58
|
+
const row = doc;
|
|
59
|
+
const rowId = id ?? row?.id;
|
|
60
|
+
if (siblingData === undefined)
|
|
61
|
+
return wallShowOpen(o, req, rowId);
|
|
62
|
+
return (await wallShowChange(o, req, { id: rowId, user: row?.user }, siblingData?.show)).allowed;
|
|
61
63
|
};
|
|
62
64
|
/**
|
|
63
65
|
* Manager access by `canManage`'s answer: `true` is every row, a query is only the rows it matches
|
|
64
66
|
* (`via` rewrites it for a collection that holds the rows indirectly), anything else is no. A new
|
|
65
67
|
* row (`create`) must match the query in its own data.
|
|
66
68
|
*/
|
|
67
|
-
function scopedManager(o, via = (w) => w, collection) {
|
|
69
|
+
function scopedManager(o, via = (w) => w, collection, target = collection) {
|
|
68
70
|
// An update by a limited manager must leave the row inside their query: the existing row with the
|
|
69
71
|
// incoming data merged over it is matched in memory, so a row can't be moved out of scope or into
|
|
70
72
|
// another site. Nothing to check unless the data touches a field the query names.
|
|
71
73
|
const update = async ({ req, id, data }) => {
|
|
72
|
-
const scope = await managerScope(o, req);
|
|
74
|
+
const scope = await managerScope(o, req, target ? { collection: target, via } : undefined);
|
|
73
75
|
if (scope === true)
|
|
74
76
|
return true;
|
|
75
77
|
if (!scope)
|
|
@@ -89,11 +91,11 @@ function scopedManager(o, via = (w) => w, collection) {
|
|
|
89
91
|
return where;
|
|
90
92
|
};
|
|
91
93
|
const rows = () => async ({ req }) => {
|
|
92
|
-
const scope = await managerScope(o, req);
|
|
94
|
+
const scope = await managerScope(o, req, target ? { collection: target, via } : undefined);
|
|
93
95
|
return scope === true ? true : scope ? via(scope) : false;
|
|
94
96
|
};
|
|
95
97
|
const create = async ({ req, data }) => {
|
|
96
|
-
const scope = await managerScope(o, req);
|
|
98
|
+
const scope = await managerScope(o, req, target ? { collection: target, via } : undefined);
|
|
97
99
|
return scope === true || (scope !== null && matchesWhere(scope, data));
|
|
98
100
|
};
|
|
99
101
|
return { create, update: collection ? update : rows(), remove: rows() };
|
|
@@ -103,12 +105,12 @@ function referencedFields(where) {
|
|
|
103
105
|
return Object.entries(where).flatMap(([k, v]) => (k === 'and' || k === 'or' ? v.flatMap(referencedFields) : [k.split('.')[0]]));
|
|
104
106
|
}
|
|
105
107
|
/** Managers read what `canManage` reaches and members their own rows; anyone else gets nothing. */
|
|
106
|
-
function ownOrScoped(o, via = (w) => w) {
|
|
108
|
+
function ownOrScoped(o, via = (w) => w, target) {
|
|
107
109
|
return async ({ req }) => {
|
|
108
110
|
if (!req.user)
|
|
109
111
|
return false;
|
|
110
112
|
const own = { user: { equals: req.user.id } };
|
|
111
|
-
const scope = await managerScope(o, req);
|
|
113
|
+
const scope = await managerScope(o, req, target ? { collection: target, via } : undefined);
|
|
112
114
|
if (scope === true)
|
|
113
115
|
return true;
|
|
114
116
|
return scope ? { or: [via(scope), own] } : own;
|
|
@@ -202,7 +204,7 @@ function everyLocaleInScope(o, target) {
|
|
|
202
204
|
const codes = localeCodes(req);
|
|
203
205
|
if (!('localized' in field && field.localized) || !codes.length)
|
|
204
206
|
return value;
|
|
205
|
-
const scope = await managerScope(o, req);
|
|
207
|
+
const scope = await managerScope(o, req, { collection: target });
|
|
206
208
|
if (scope === true || !scope)
|
|
207
209
|
return value; // no scope at all is refused by access
|
|
208
210
|
const existing = (siblingDocWithLocales?.[field.name] ?? {});
|
|
@@ -229,7 +231,7 @@ function publicRead(o, slug) {
|
|
|
229
231
|
if (!(versions && typeof versions === 'object' && versions.drafts))
|
|
230
232
|
return true;
|
|
231
233
|
const published = { or: [{ _status: { equals: 'published' } }, { _status: { exists: false } }] };
|
|
232
|
-
const scope = await managerScope(o, req);
|
|
234
|
+
const scope = await managerScope(o, req, { collection: slug });
|
|
233
235
|
if (scope === true)
|
|
234
236
|
return true;
|
|
235
237
|
return scope ? { or: [published, scope] } : published;
|
|
@@ -238,7 +240,7 @@ function publicRead(o, slug) {
|
|
|
238
240
|
/** Field access for managers: judged on the particular row, never on a bare yes. */
|
|
239
241
|
function managerFieldOn(o, collection) {
|
|
240
242
|
return async ({ req, id, doc, data }) => {
|
|
241
|
-
const scope = await managerScope(o, req);
|
|
243
|
+
const scope = await managerScope(o, req, { collection });
|
|
242
244
|
if (scope === true)
|
|
243
245
|
return true;
|
|
244
246
|
if (!scope)
|
|
@@ -317,7 +319,7 @@ export function membershipsCollection(o) {
|
|
|
317
319
|
const { update, remove } = scopedManager(o, undefined, o.memberships);
|
|
318
320
|
const managerField = managerFieldOn(o, o.memberships);
|
|
319
321
|
// A manager reads what `canManage` reaches; members read their own (the plugin's own query, never the site's answer).
|
|
320
|
-
const ownOrManager = ownOrScoped(o);
|
|
322
|
+
const ownOrManager = ownOrScoped(o, undefined, o.memberships);
|
|
321
323
|
const nobody = () => false;
|
|
322
324
|
return {
|
|
323
325
|
slug: o.memberships,
|
|
@@ -325,7 +327,7 @@ export function membershipsCollection(o) {
|
|
|
325
327
|
admin: {
|
|
326
328
|
useAsTitle: 'label',
|
|
327
329
|
defaultColumns: ['label', 'kind', 'amount', 'interval', 'status', 'currentPeriodEnd'],
|
|
328
|
-
description: 'Members’ subscriptions. Created and updated by checkout and Stripe; edit the wall
|
|
330
|
+
description: 'Members’ subscriptions. Created and updated by checkout and Stripe; edit the staff notes here. A manager can hide a backers wall entry by clearing its box.',
|
|
329
331
|
},
|
|
330
332
|
// Created by checkout and updated by Stripe webhooks; members manage theirs through the plugin's endpoints.
|
|
331
333
|
access: { read: ownOrManager, readVersions: managersReadVersions(o, o.memberships), create: nobody, update, delete: remove },
|
|
@@ -401,7 +403,7 @@ export function membershipsCollection(o) {
|
|
|
401
403
|
name: 'wall',
|
|
402
404
|
label: 'Backers wall',
|
|
403
405
|
type: 'group',
|
|
404
|
-
admin: { description: 'Members choose whether and how they appear; nothing is public otherwise.' },
|
|
406
|
+
admin: { description: 'Members choose whether and how they appear; nothing is public otherwise. A manager can clear the box to hide an entry. Only the member can show it again.' },
|
|
405
407
|
fields: [
|
|
406
408
|
{ name: 'show', label: 'Show on the backers wall', type: 'checkbox', defaultValue: false, access: { update: wallShow(o) }, hooks: fresh(false) },
|
|
407
409
|
{ name: 'name', label: 'Name on the wall', type: 'text', maxLength: 80, access: { update: memberOnly }, hooks: fresh() },
|
|
@@ -425,8 +427,8 @@ export function membershipsCollection(o) {
|
|
|
425
427
|
};
|
|
426
428
|
}
|
|
427
429
|
export function paymentsCollection(o) {
|
|
428
|
-
const { remove } = scopedManager(o, viaMembership);
|
|
429
|
-
const ownOrManager = ownOrScoped(o, viaMembership);
|
|
430
|
+
const { remove } = scopedManager(o, viaMembership, undefined, o.payments);
|
|
431
|
+
const ownOrManager = ownOrScoped(o, viaMembership, o.payments);
|
|
430
432
|
return {
|
|
431
433
|
slug: o.payments,
|
|
432
434
|
labels: { singular: 'Membership payment', plural: 'Membership payments' },
|
package/dist/endpoints.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { membershipRole, wallShowChange } from './access.js';
|
|
2
2
|
import { cancelMembership, changeMembership, handleStripeEvent, knownScope, membershipOf, portalLink, startCheckout } from './api.js';
|
|
3
3
|
const json = (body, status = 200) => Response.json(body, { status });
|
|
4
4
|
const fail = (err) => {
|
|
@@ -47,11 +47,10 @@ async function ownMembership(req, { memberOnly = false } = {}) {
|
|
|
47
47
|
if (here === null || idOf(m[o.scopeField]) !== here)
|
|
48
48
|
return null;
|
|
49
49
|
}
|
|
50
|
-
|
|
50
|
+
const role = await membershipRole(o, req, m);
|
|
51
|
+
if (role === 'member')
|
|
51
52
|
return m;
|
|
52
|
-
if (memberOnly)
|
|
53
|
-
return null;
|
|
54
|
-
if (!(await managerReaches(o, req, o.memberships, m.id)))
|
|
53
|
+
if (memberOnly || role !== 'manager')
|
|
55
54
|
return null;
|
|
56
55
|
// And the collection's own read access, so rules the host added there (a tenant plugin's, say) apply too.
|
|
57
56
|
const visible = await req.payload.count({ collection, where: { id: { equals: m.id } }, overrideAccess: false, user: req.user, req });
|
|
@@ -144,10 +143,12 @@ export const wallEndpoint = {
|
|
|
144
143
|
if (!m)
|
|
145
144
|
return json({ error: 'Not found.' }, 404);
|
|
146
145
|
const b = await body(req);
|
|
147
|
-
const member = idOf(m.user) === String(req.user.id);
|
|
148
146
|
// Only the member shows themselves on the wall or changes the name; a manager may only hide it.
|
|
149
|
-
|
|
147
|
+
// The same check answers for the field in the admin form and the other APIs.
|
|
148
|
+
const { role, allowed } = await wallShowChange(membershipOf(req.payload), req, m, b.show);
|
|
149
|
+
if (!allowed)
|
|
150
150
|
return json({ error: 'Only the member can do that.' }, 403);
|
|
151
|
+
const member = role === 'member';
|
|
151
152
|
const name = member ? (typeof b.name === 'string' ? b.name.trim().slice(0, 80) : '') : (m.wall?.name ?? '');
|
|
152
153
|
const updated = await req.payload.update({
|
|
153
154
|
collection: membershipOf(req.payload).memberships,
|
package/dist/guard.js
CHANGED
|
@@ -78,6 +78,39 @@ function checkFieldValue(field, v, req, depth, locales) {
|
|
|
78
78
|
}
|
|
79
79
|
refuseBadKeys(v, null, req, depth);
|
|
80
80
|
}
|
|
81
|
+
// A group is an object. Payload's field hooks read inside it before any access rule runs, so a null
|
|
82
|
+
// (or any non-object) for a group, `wall: null` on a membership for one, would end as a 500. It is a
|
|
83
|
+
// clean 400 instead, for every caller, and nothing is saved.
|
|
84
|
+
function refuseNullGroups(data, fields, req) {
|
|
85
|
+
if (!data || typeof data !== 'object' || Array.isArray(data))
|
|
86
|
+
return;
|
|
87
|
+
for (const [name, f] of named(fields)) {
|
|
88
|
+
if (!(name in data))
|
|
89
|
+
continue;
|
|
90
|
+
const v = data[name];
|
|
91
|
+
if (v === undefined)
|
|
92
|
+
continue; // undefined means not sent
|
|
93
|
+
if (f.type === 'group') {
|
|
94
|
+
if (v === null || typeof v !== 'object' || Array.isArray(v))
|
|
95
|
+
throw new APIError(`${name} must be an object.`, 400, undefined, true);
|
|
96
|
+
refuseNullGroups(v, f.fields, req);
|
|
97
|
+
}
|
|
98
|
+
else if (f.type === 'array' && Array.isArray(v)) {
|
|
99
|
+
for (const row of v)
|
|
100
|
+
refuseNullGroups(row, f.fields, req);
|
|
101
|
+
}
|
|
102
|
+
else if (f.type === 'blocks' && Array.isArray(v)) {
|
|
103
|
+
// A block row is checked against the block its blockType names, whether the host listed the block here or by reference.
|
|
104
|
+
const blocks = [...(f.blocks ?? []), ...(f.blockReferences ?? []).map((b) => (typeof b === 'string' ? req.payload.config.blocks?.find((x) => x.slug === b) : b))];
|
|
105
|
+
for (const row of v) {
|
|
106
|
+
const type = row && typeof row === 'object' ? row.blockType : undefined;
|
|
107
|
+
const block = blocks.find((b) => b && b.slug === type);
|
|
108
|
+
if (block)
|
|
109
|
+
refuseNullGroups(row, block.fields, req);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
81
114
|
/**
|
|
82
115
|
* Added at the front of the collection's `beforeOperation` hooks after the host's overrides, so it
|
|
83
116
|
* sees the data as the request sent it, ahead of the collection's other hooks. A plugin that runs
|
|
@@ -95,6 +128,7 @@ export function writeGuard(o) {
|
|
|
95
128
|
// passes a request body straight to it gets the same check.
|
|
96
129
|
if (operation === 'create' || operation === 'update') {
|
|
97
130
|
refuseBadKeys(a.data, collection.fields, req);
|
|
131
|
+
refuseNullGroups(a.data, collection.fields, req);
|
|
98
132
|
return args;
|
|
99
133
|
}
|
|
100
134
|
if (operation === 'restoreVersion' && !overrideAccess) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@extantape/payload-plugin-membership",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Memberships for Payload: Patreon-style tiers or pick-your-own-amount pledges to categories, billed as Stripe subscriptions, with a backers wall and hooks for perks.",
|
|
5
5
|
"homepage": "https://github.com/ExtantApe/payload-plugin-membership#readme",
|
|
6
6
|
"bugs": {
|