Replies: 3 comments 7 replies
-
Please check this docs -> https://casl.js.org/v6/en/api/casl-ability-extra#permitted-fields-of |
Beta Was this translation helpful? Give feedback.
-
Thanks @stalniy for your quick answer, I hadn't mentioned it but I have already considered using the The problem with this one is that in order to validate read, update and delete operations, all the rows selected by the
Any other idea ? :) |
Beta Was this translation helpful? Give feedback.
-
I do not understand why you need to select all rows in order to validate that. If we are talking about update only operation, then what you need is to restrict what fields client may update. for that case If you want to restrict what fields can be selected, then it's a bit harder when there is a condition because different records may have different allowed fields based on condition. In this case you need to take the most wide subset of allowed fields for a subject type const allAllowedFieldsForArticleIgnoringConditions = permittedFieldsOf(ability, 'update', 'Article', {
fieldsFrom: rule => rule.fields || [/* list of all fields for Article */]
}); This will restrict amount of data read from db and at the end, you additionally need to preprocess all fetched records in memory in order to ensure that private fields are not exposed publicly. Another thought on per field permission, is that it should be used wise, I mean in most cases when per field permission become complex, it means that we should extract additional domain model/resource/type from the model we are trying to set permission on. usually such models/tables have too much fields which is additional sign that entity should be split into multiple. When you split entity into N, you may find out that you actually don't need per field permission anymore or they become much simpler. |
Beta Was this translation helpful? Give feedback.
-
Hi !
I'm currently building Prismary, which is actually a prototype, but aims to became "the missing glue" between Prisma, tRPC, CASL and Zod.
In short, Prismary auto-generates tRPC procedures from a Prisma schema file, where each procedure is validated and permissioned respectively by Zod and CASL.
I'm trying to enforce field-level access rules efficiently, actually I've thought to many solutions which are individually pretty complex and sometimes inefficient.
@stalniy While developing
@casl/prisma
had you thought of a way to enforce field restrictions on Prisma queries ?For example, how would you validate / restrict a call to:
considering the following CASL rules ?
I know that the
{ authorId: user.id }
rule can be easily enforced by puttingaccessibleBy(ability, "update").Article
in thewhere
clause of theupdate()
call.But how to ensure the user isn't allowed to update fields that aren't in
['title', 'description']
if it is not the author of this article ?Beta Was this translation helpful? Give feedback.
All reactions