-
I'm in need of access control (per field and per collection) that is based on the collection item's originalDoc. Now I need to write an access functions that will resolve true only if user.id === doc.owner. I stuck here as access functions don't pass doc data. With update op I didn't even find a combination of hooks that would make such access control possible (even if there is any it feels like wrong unmaintainable approach). I understand that current access implementation is only for controlling access in whole collection based on user and input data. Any suggestions how to go about this? If this is not currently possible, would it make sense to introduce such features? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey @chladog — question for you. When you say:
Do you want to use field hooks, or collection-level hooks here? If collection level, this is actually super easy. In access control, you can return a Here's an example of how we would normally do something like this: const adminOwnerAccess = ({ req: { user } }) => {
// If user is admin, return true
if (user && user.roles.includes('admin')) {
return true;
}
// If there is a user, return a query constraint
// that only allows users to perform actions against
// documents where the owner is equal to their user ID
if (user) {
return {
owner: {
equals: user.id,
},
};
}
// If neither of the above conditions are met,
// Block access
return false;
}; Is this what you need? Note, we might need to expand on our docs if you didn't see that this is possible via documentation. It's one of the strongest features of our access control! |
Beta Was this translation helpful? Give feedback.
Hey @chladog — question for you.
When you say:
Do you want to use field hooks, or collection-level hooks here?
If collection level, this is actually super easy. In access control, you can return a
query constraint
to restrict which documents that a user can perform actions against.Here's an example of how we would normally do something like this: