You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We want to stop someone from setting a ForeignKeyon a record that they do have access to, to a record that they do not have access to.
We're not really doing this because of what we perceive to be 'unique' business rules. It seems like it'd be fairly common for a data model to be such that it'd be undesired to, e.g., attach a 'child' record to a 'parent' that the user does not have access to, just because the user knows the parent record's ID (which is easily possible with e.g. sequential integer IDs).
We can't really do this kind of validation in our model-layer, as we need certain request context in order to determine if the user has access to the record they're trying to attach, such as the current user, and which related fields are actually being set. In other frameworks that we've used, we've been able to hook into the layer where primary keys are resolved to do this kind of validation, but this can't currently be done in strawberry-django.
Example
Take for example a simple project management application where users can create projects and assign tasks to those projects. Some models for this application might look like this:
We only want to allow users to create tasks for projects that they own. This means that when setting the project field on a task, the allowed values should only be the subset that they own:
@strawberry_django.type(Task)classTaskType:
id: strawberry.autoname: strawberry.autodescription: strawberry.auto@strawberry_django.input(Task)classCreateTaskInput:
name: strawberry.autodescription: strawberry.autoproject: strawberry.auto# The allowed projects here should be filtered@strawberry.typeclassMutation:
create_task: Task=strawberry_django.mutations.create(CreateTaskInput)
Feature Request Type
Core functionality
Alteration (enhancement/optimization) of existing feature(s)
New behavior
Potential Solutions
At risk of muddying the waters, here are some potential approaches we see which could solve this (although we're open to other suggestions if they're more in line with the project's goals):
a) Allow the queryset for a related field to be customized
In some way or another, allow the queryset that primary keys are resolved from to be customized.
This would allow for the enforcement of permissions for related fields, and the flexibility of other filtering as well.
b) Allow a hook for resolving primary keys to be customized
In some way or another, allow the whole resolution of primary keys to be customized.
This would also allow for the enforcement of permissions for related fields, and the flexibility of other filtering as well.
c) Implement permissions field extensions for input fields
Continue the current pattern of using field extensions to add permissions to input fields.
This is consistent with the project's current approach, but doesn't provide the added flexibility of queryset filtering.
Setting aside doing the actual work, are any of these options inline with your vision for the project and how it should work? Otherwise, do have you any other ideas or suggestions?
Upvote & Fund
We're using Polar.sh so you can upvote and help fund this issue.
We receive the funding once the issue is completed & confirmed by you.
Thank you in advance for helping prioritize & fund our backlog.
The text was updated successfully, but these errors were encountered:
In this scenario, the parent field on the MyChildInput is a NodeInput (i.e., a Global ID) which is ultimately resolved via the MyParentType type using its get_queryset method. If the get_queryset method is set up to filter for permissions based on the current user, then you implicitly get the functionality requested in this ticket.
@SupImDos sorry for taking long to reply, but glad you found out how to solve this.
So, its possible this issue is resolved, but maybe we could do with some documentation to make it clearer how this works?
Yes, strawberry-django unfortunately is lacking a lot of documentation. I'm marking this with a documentation label and will try to tackle this in the future. But anyone is very welcomed to open a PR to contribute to this :)
Description
At a high-level our problem is:
ForeignKey
on a record that they do have access to, to a record that they do not have access to.We can't really do this kind of validation in our model-layer, as we need certain request context in order to determine if the user has access to the record they're trying to attach, such as the current user, and which related fields are actually being set. In other frameworks that we've used, we've been able to hook into the layer where primary keys are resolved to do this kind of validation, but this can't currently be done in
strawberry-django
.Example
Take for example a simple project management application where users can create projects and assign tasks to those projects. Some models for this application might look like this:
We only want to allow users to create tasks for projects that they own. This means that when setting the project field on a task, the allowed values should only be the subset that they own:
Feature Request Type
Potential Solutions
At risk of muddying the waters, here are some potential approaches we see which could solve this (although we're open to other suggestions if they're more in line with the project's goals):
a) Allow the queryset for a related field to be customized
In some way or another, allow the queryset that primary keys are resolved from to be customized.
This would allow for the enforcement of permissions for related fields, and the flexibility of other filtering as well.
b) Allow a hook for resolving primary keys to be customized
In some way or another, allow the whole resolution of primary keys to be customized.
This would also allow for the enforcement of permissions for related fields, and the flexibility of other filtering as well.
c) Implement permissions field extensions for input fields
Continue the current pattern of using field extensions to add permissions to input fields.
This is consistent with the project's current approach, but doesn't provide the added flexibility of queryset filtering.
Setting aside doing the actual work, are any of these options inline with your vision for the project and how it should work? Otherwise, do have you any other ideas or suggestions?
Upvote & Fund
The text was updated successfully, but these errors were encountered: