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

default discord avatar
mihanc
6 months ago
10

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
    mihanc
    6 months ago

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

    image.png
    image.png
  • discord user avatar
    jesschow
    Payload Team
    6 months ago

    hi @mihanc - you can access the full doc using

    data

    , take a look at this example:

    Screen_Shot_2023-02-27_at_10.54.27_PM.png
  • default discord avatar
    mihanc
    6 months ago

    Why the data can be undefined?

    image.png
    image.png
  • discord user avatar
    jesschow
    Payload Team
    6 months 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
    mihanc
    6 months 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.

  • discord user avatar
    jesschow
    Payload Team
    6 months 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
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.