Skip to content

Commit da17810

Browse files
authored
docs: add publishing access control example (#16021)
Adds info on how to limit access to publishing only.
1 parent f0735b1 commit da17810

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

docs/access-control/collections.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ The following arguments are provided to the `update` function:
209209
| **`id`** | `id` of document requested to update. |
210210
| **`data`** | The data passed to update the document with. |
211211

212+
<Banner type="success">
213+
**Tip:** For Collections with [Drafts](../versions/drafts) enabled, you can
214+
use `update` access to [control who can
215+
publish](../versions/drafts#controlling-who-can-publish) by returning a query
216+
constraint on the `_status` field.
217+
</Banner>
218+
212219
### Delete
213220

214221
Similarly to the Update function, returns a boolean or a [query constraint](/docs/queries/overview) to limit which documents can be deleted by which users.

docs/versions/drafts.mdx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,45 @@ export const Pages: CollectionConfig = {
254254
}
255255
```
256256

257+
## Controlling who can publish
258+
259+
You can use `update` [Access Control](../access-control/collections#update) to restrict who can publish documents by returning a [query constraint](/docs/queries/overview) on the `_status` field. When this constraint prevents publishing, the Admin UI automatically hides the Publish and Unpublish buttons.
260+
261+
Here is an example where admins can publish, but editors can only save drafts:
262+
263+
```ts
264+
import type { CollectionConfig } from 'payload'
265+
266+
export const Posts: CollectionConfig = {
267+
slug: 'posts',
268+
access: {
269+
update: ({ req: { user } }) => {
270+
if (!user) {
271+
// If there is no user, they can't update anything
272+
return false
273+
}
274+
275+
// Admins can update any document, including publishing
276+
if (user?.role === 'admin') return true
277+
278+
// Editors can only update documents that are not published.
279+
return {
280+
_status: {
281+
equals: 'draft',
282+
},
283+
}
284+
},
285+
},
286+
versions: {
287+
drafts: true,
288+
},
289+
}
290+
```
291+
292+
The `update` function returns a query constraint instead of a boolean. Payload appends this constraint to the update query, so only documents where `_status` is not `published` can be updated by editors. This also blocks scheduled publish jobs from executing for those users.
293+
294+
The `create` function uses a `data` check instead since there is no existing document to query against.
295+
257296
## Scheduled publish
258297

259298
Payload provides for an ability to schedule publishing / unpublishing events in the future, which can be helpful if you need to set certain documents to "go live" at a given date in the future, or, vice versa, revert to a draft state after a certain time has passed.

0 commit comments

Comments
 (0)