Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Allow the user to have "update" rights only to the articles he has created

default discord avatar
mihanc2 years ago
7

Hello everyone! I'd like to allow the user to have "update" rights only to the articles he has created. I have a simple function that checks if the user pseudonym is equal to the article field but seems I'm doing something wrong because the pseudonym field is undefined.



Perhaps it's possible to somehow pass an article object to this function?



Okay. Seems it doesn't work because the article author is an object



I've updated the code but it gives the same result

  • default discord avatar
    mihanc2 years ago

    This part is not working in my case because I have an object and need to compare a key.

  • default discord avatar
    jessrynkar2 years ago

    hi

    @187948379920138240

    - you can access the full doc using

    data

    , take a look at this example:

  • default discord avatar
    mihanc2 years ago

    Why the data can be undefined?

  • default discord avatar
    jessrynkar2 years ago

    The afterRead hook runs when you save the doc - so if you hit save and check your console.log again you should see the data

  • default discord avatar
    mihanc2 years ago

    Is there any way to access the data before the afterRead hook? I'd like to disable Save button if the user is not the author of this article.

  • default discord avatar
    jessrynkar2 years ago

    My apologies, you're not using a hook I was mixing this up with another similar thread. However, I would suggest adding a collection hook then you can access the document data before saving with beforeValidate or beforeChange

    https://payloadcms.com/docs/hooks/collections
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.