ScopeAuth Plugin - Prevent User From Accessing Other Group's Data #845
-
Hi all! I'm currently working on an application that consists of users belonging organizations. While there are different roles inside the organization, no one should be able to access data from other organizations. My original thought, which probably works, is to have the following authScope on each object:
However, this is a little repetitive and could be error prone to implement and maintain on every single object. Is there an easier way to do this on a global scope? Another idea worth mentioning would be to add the user's organization ID to each query's Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Generally having auth enforced as part of your queries so content the user isn't authorized for is never loaded is ideal (adding conditions to your There isn't a good way to global define and enforce these rules across multiple objects, because Pothos doesn't really understand the relations between the different objects in a way that would allow knowing if a particular relation denotes ownership/access. What you can do share/simplify how the auth scope for organizations is defined: You can create a scope like authScopes: (ctx) => ({
accessOrganization: (id) => ctx.user.organizationId === id,
}) Then on your objects: builder.prismaObject('User', {
runScopesOnType: true,
authScopes: (user) => ({ accessOrganization: user.organizationId }),
...
}) Not sure if this helps |
Beta Was this translation helpful? Give feedback.
Generally having auth enforced as part of your queries so content the user isn't authorized for is never loaded is ideal (adding conditions to your
where
clause). Pothos doesn't really provide anything that helps with this though.There isn't a good way to global define and enforce these rules across multiple objects, because Pothos doesn't really understand the relations between the different objects in a way that would allow knowing if a particular relation denotes ownership/access.
What you can do share/simplify how the auth scope for organizations is defined:
You can create a scope like
accessOrganization: string
, that does something like