Field-level Access Control

Field Access Control is Access Control used to restrict access to specific Fields within a Document.

To add Access Control to a Field, use the access property in your Field Config:

1
import type { Field } from 'payload';
2
3
export const FieldWithAccessControl: Field = {
4
// ...
5
access: {
6
// ...
7
},
8
}

Config Options

Access Control is specific to the operation of the request.

To add Access Control to a Field, use the access property in the Field Config:

1
import { CollectionConfig } from 'payload';
2
3
export const Posts: CollectionConfig = {
4
slug: 'posts',
5
fields: [
6
{
7
name: 'title',
8
type: 'text',
9
access: {
10
create: ({ req: { user } }) => { ... },
11
read: ({ req: { user } }) => { ... },
12
update: ({ req: { user } }) => { ... },
13
},
14
};
15
],
16
};

The following options are available:

FunctionPurpose
createAllows or denies the ability to set a field's value when creating a new document. More details.
readAllows or denies the ability to read a field's value. More details.
updateAllows or denies the ability to update a field's value More details.

Create

Returns a boolean which allows or denies the ability to set a field's value when creating a new document. If false is returned, any passed values will be discarded.

Available argument properties:

OptionDescription
reqThe Request object containing the currently authenticated user
dataThe full data passed to create the document.
siblingDataImmediately adjacent field data passed to create the document.

Read

Returns a boolean which allows or denies the ability to read a field's value. If false, the entire property is omitted from the resulting document.

Available argument properties:

OptionDescription
reqThe Request object containing the currently authenticated user
idid of the document being read
docThe full document data.
siblingDataImmediately adjacent field data of the document being read.

Update

Returns a boolean which allows or denies the ability to update a field's value. If false is returned, any passed values will be discarded.

If false is returned and you attempt to update the field's value, the operation will not throw an error however the field will be omitted from the update operation and the value will remain unchanged.

Available argument properties:

OptionDescription
reqThe Request object containing the currently authenticated user
idid of the document being updated
dataThe full data passed to update the document.
siblingDataImmediately adjacent field data passed to update the document with.
docThe full document data, before the update is applied.
Next

Hooks Overview