You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can now specify exactly who can change the constraints within a
query preset.
For example, you want to ensure that only "admins" are allowed to set a
preset to "everyone".
To do this, you can use the new `queryPresets.filterConstraints`
property. When a user lacks the permission to change a constraint, the
option will either be hidden from them or disabled if it is already set.
```ts
import { buildConfig } from 'payload'
const config = buildConfig({
// ...
queryPresets: {
// ...
filterConstraints: ({ req, options }) =>
!req.user?.roles?.includes('admin')
? options.filter(
(option) =>
(typeof option === 'string' ? option : option.value) !==
'everyone',
)
: options,
},
})
```
The `filterConstraints` functions takes the same arguments as
`reduceOptions` property on select fields introduced in #12487.
|`access`| Used to define custom collection-level access control that applies to all presets. [More details](#access-control). |
52
+
|`filterConstraints`| Used to define which constraints are available to users when managing presets. [More details](#constraint-access-control). |
53
+
|`constraints`| Used to define custom document-level access control that apply to individual presets. [More details](#document-access-control). |
54
+
|`labels`| Custom labels to use for the Query Presets collection. |
54
55
55
56
## Access Control
56
57
@@ -59,7 +60,7 @@ Query Presets are subject to the same [Access Control](../access-control/overvie
59
60
Access Control for Query Presets can be customized in two ways:
60
61
61
62
1.[Collection Access Control](#collection-access-control): Applies to all presets. These rules are not controllable by the user and are statically defined in the config.
62
-
2.[Document Access Control](#document-access-control): Applies to each individual preset. These rules are controllable by the user and are saved to the document.
63
+
2.[Document Access Control](#document-access-control): Applies to each individual preset. These rules are controllable by the user and are dynamically defined on each record in the database.
63
64
64
65
### Collection Access Control
65
66
@@ -97,7 +98,7 @@ This example restricts all Query Presets to users with the role of `admin`.
97
98
98
99
### Document Access Control
99
100
100
-
You can also define access control rules that apply to each specific preset. Users have the ability to define and modify these rules on the fly as they manage presets. These are saved dynamically in the database on each document.
101
+
You can also define access control rules that apply to each specific preset. Users have the ability to define and modify these rules on the fly as they manage presets. These are saved dynamically in the database on each record.
101
102
102
103
When a user manages a preset, document-level access control options will be available to them in the Admin Panel for each operation.
103
104
@@ -150,8 +151,8 @@ const config = buildConfig({
150
151
}),
151
152
},
152
153
],
153
-
// highlight-end
154
154
},
155
+
// highlight-end
155
156
},
156
157
})
157
158
```
@@ -171,3 +172,39 @@ The following options are available for each constraint:
171
172
|`value`| The value to store in the database when this constraint is selected. |
172
173
|`fields`| An array of fields to render when this constraint is selected. |
173
174
|`access`| A function that determines the access control rules for this constraint. |
175
+
176
+
### Constraint Access Control
177
+
178
+
Used to dynamically filter which constraints are available based on the current user, document data, or other criteria.
179
+
180
+
Some examples of this might include:
181
+
182
+
- Ensuring that only "admins" are allowed to make a preset available to "everyone"
183
+
- Preventing the "onlyMe" option from being selected based on a hypothetical "disablePrivatePresets" checkbox
184
+
185
+
When a user lacks the permission to set a constraint, the option will either be hidden from them, or disabled if it is already saved to that preset.
186
+
187
+
To do this, you can use the `filterConstraints` property in your [Payload Config](../configuration/overview):
188
+
189
+
```ts
190
+
import { buildConfig } from'payload'
191
+
192
+
const config =buildConfig({
193
+
// ...
194
+
queryPresets: {
195
+
// ...
196
+
// highlight-start
197
+
filterConstraints: ({ req, options }) =>
198
+
!req.user?.roles?.includes('admin')
199
+
?options.filter(
200
+
(option) =>
201
+
(typeofoption==='string'?option:option.value) !==
202
+
'everyone',
203
+
)
204
+
:options,
205
+
// highlight-end
206
+
},
207
+
})
208
+
```
209
+
210
+
The `filterConstraints` function receives the same arguments as [`filterOptions`](../fields/select#filterOptions) in the [Select field](../fields/select).
'The following fields are invalid: Sharing settings > Read > Specify who can read this Preset, Sharing settings > Update > Specify who can update this Preset',
0 commit comments