Access control for unpublishing #1996
-
Hello! Maybe I am missing something, but I could not find anything related to access control of the 'unpublish' action. Is it possible? |
Beta Was this translation helpful? Give feedback.
Answered by
jacobsfletch
Feb 1, 2023
Replies: 1 comment 7 replies
-
@cerize Hi! You can use the import APIError from "payload/dist/errors/APIError";
const beforeChangeHook: GlobalBeforeChangeHook = async ({
data, // incoming data to update or create with
req, // full express request
originalDoc, // original document
}) => {
if (originalDoc._status === 'published' && data._status !== 'published') throw new APIError('You cannot do this!');
return data; // Return data to update the document with
} Here are the docs for this hook: https://payloadcms.com/docs/hooks/globals#beforechange |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
zubricks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@cerize Hi! You can use the
beforeChange
hook to check the document's_status
. If it went frompublished
todraft
then you can throw an API error. Something like this (untested):Here are the docs for this hook: https://payloadcms.com/docs/hooks/globals#beforechange