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
feat: add access control for jobs queue and cancel operations (#14404)
Adds access control for jobs `queue` and `cancel` operations, similar to
what we have for `run` operations.
**Implementation:**
Both operations now support `overrideAccess` parameter (defaults to
`true`) and respect `jobsConfig.access.queue` and
`jobsConfig.access.cancel` functions. When `overrideAccess: false` is
passed without an authenticated request, operations throw `Forbidden`
error.
**Configuration:**
Access control functions can be defined in the Jobs Config under
`jobs.access.queue`, `jobs.access.run`, and `jobs.access.cancel`. Each
function receives `{ req }` and returns a boolean. If no custom access
control is defined, the default allows any authenticated user to perform
the operation.
**Example:**
```ts
// Configure access control
jobs: {
access: {
queue: ({ req }) => req.user?.roles?.includes('admin'),
cancel: ({ req }) => req.user?.roles?.includes('admin'),
}
}
// Use in Local API
await payload.jobs.cancel({
where: { workflowSlug: { equals: 'sync' } },
overrideAccess: false,
req,
})
```
By default, Payload's job operations bypass access control when used from the Local API. You can enable access control by passing `overrideAccess: false` to any job operation.
53
+
54
+
To define custom access control for jobs, add an `access` property to your Jobs Config:
55
+
56
+
```ts
57
+
importtype { SanitizedConfig } from'payload'
58
+
59
+
const config:SanitizedConfig= {
60
+
// ...
61
+
jobs: {
62
+
access: {
63
+
// Control who can queue new jobs
64
+
queue: ({ req }) => {
65
+
returnreq.user?.roles?.includes('admin')
66
+
},
67
+
// Control who can run jobs
68
+
run: ({ req }) => {
69
+
returnreq.user?.roles?.includes('admin')
70
+
},
71
+
// Control who can cancel jobs
72
+
cancel: ({ req }) => {
73
+
returnreq.user?.roles?.includes('admin')
74
+
},
75
+
},
76
+
},
77
+
}
78
+
```
79
+
80
+
Each access control function receives the current `req` object and should return a boolean. If no access control is defined, the default behavior allows any authenticated user to perform the operation.
81
+
82
+
To use access control in the Local API:
83
+
84
+
```ts
85
+
const req =awaitcreateLocalReq({ user }, payload)
86
+
87
+
awaitpayload.jobs.queue({
88
+
workflow: 'createPost',
89
+
input: { title: 'My Post' },
90
+
overrideAccess: false, // Enable access control
91
+
req, // Pass the request with user context
92
+
})
93
+
```
94
+
50
95
#### Cancelling Jobs
51
96
52
97
Payload allows you to cancel jobs that are either queued or currently running. When cancelling a running job, the current task will finish executing, but no subsequent tasks will run. This happens because the job checks its cancellation status between tasks.
0 commit comments