-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature support for Issue 25 #106
Comments
Hi! Thanks for opening this issue first to discuss new additions Could you tell me a bit more about your use case? What does your create policy methods look like if you need the resource to be there? |
Sure! One of our apps is an educational dashboard that teachers can use to create and manage assignments for their students. If we consider authorization from only a class perspective, then it makes sense that anyone tagged as an educator can create, modify, and delete assignments. For resource-specific validation, we can do most of it with model-level level constraints. If a teacher manages students 1 and 2 but tries to create an assignment for student 3, that's not an action that makes sense to perform regardless of who initiated the action. However, certain actions can look completely valid from the model level but could be performed by an unauthorized user which means model constraints aren't really an option. For example, a teacher could mark an unrelated student's assignment as completed (ie, update a single timestamp). The only way we can tell that this action is invalid is knowing who performed the action and which resource it's being performed on, hence we need more than just the model's class in the policy. I'm open to suggestions on how to handle such a use case that doesn't involve my feature suggestion. I just haven't found any myself. I also feel like only accepting a class is somewhat misleading if you already understand vanilla Pundit since Pundit's docs show many examples that incorporate the resource directly into create and update authorization methods. Oh, and I forgot the last part of you question. This is an example of what my policies look like:
It have it accepting both a class and a record for now. I have some custom controller code for |
Thanks for explaining the use case 😊. How does the record.instructor_profile get populated before the I'm mainly asking to gauge whether Authorization of operations touching relationships would work for your use case. Can you give that doc a look? I'd be curious to hear if there's some edge case I'm missing that the relationship authorizations can't handle |
Also, if you could provide an example API request for the create call + tell me what ActiveRecord models and associations are being touched there, I think I'd have all the information about your use case that I need right now 😊 |
I'll need a day or so to take a look at the docs and form a proper response. It looks like they may actually cover the uses cases I need, I just want to make sure I fully understand how JSONAPI::Authorization ties into relationship features first. |
Thanks! Take your time 😊. I'd be super happy if you find any weirdnesses in the docs and take some time to improve them or ask questions. This is the first version of the docs so there might be some unanswered questions, or some confusing ways of saying things. If we can improve them, everyone would benefit. Let me also know if relationship authorization indeed solves your problems. I haven't heard much feedback about it, and would love to hear how it goes |
Alright, sorry it took me a while! I'll discuss create and update separately since they end up being rather different. I'll stick to using the
For authorizing creation of an assignment, it looks like the authorization of relationships does indeed work for our use case. We would need to define the methods For updating assignments, I still don't see how we could use this feature. My original example where a teacher wants to modify any arbitrary student's assignment doesn't touch any relationships on the resource. There's two custom relationship methods available for PATCH requests according to the docs: I'm also confused about the examples given at https://github.com/venuu/jsonapi-authorization/blob/master/docs/relationship-authorization.md#custom-relationship-authorization-method since the code explicitly passes a resource instance instead of a class to the policy initialization ( Overall, the docs are quite thorough which I appreciate. I feel like my use case for the creation authorization should be touched on to help newcomers understand how to switch from the paradigm of accessing resources in the policy itself to relying on relationship-specific methods. This is something I can help contribute myself if you would like 😊 |
Thanks for the thorough reply. It indeed seems like the existing implementation would work for your use case. For the PATCH/update case, JA does indeed pass the record instance, not just the record class, to the JA can use the existing record there as the Does this make sense? |
Hmm, I can see from the source code that you're correct, but there's something very odd happening in my request specs that is giving my policies a class instead of a resource during PATCH requests. I'll figure out what's going on and post again shortly, sorry for the misunderstanding! |
I haven't had much time to look into my above issue in the past few days, but if it really does end up being a bug I'll just make a new issue. Concerning wrapping up this issue, would you be open to me contributing some documentation to help address the misconception that users like myself have about |
Sure, that would be great! I'm not sure what's the good place for that documentation, but I suppose it won't hurt even if it would be in a suboptimal place.
There ain't much guidelines to follow regarding docs. If you want to provide more reasoning for why it is so, my comments in #25 (comment) can be referred to — particularly, that JA runs authorization checks in a before hook just to make sure no unauthorized changes ever go all the way to database. |
Hiya @jeremyjaybaker, what's the situation regarding this thread? Have you been able to resolve your issues? |
Hello, sorry I completely vanished! My company just moved across the US and things have been so hectic that this slipped past me. I'll address this issue this week so we can get it closed out. |
No problem, take your time |
Just checking up @jeremyjaybaker, is this issue still timely for you? |
Hello, I'm working on a version of the authorizing processor that can support accept a resource instead of a class for
create?
andupdate?
actions as referenced in #25 and several other issues.The current behavior (only a class is given to
create?
andupdate?
) is on by default. Users may switch to a resource-orientedcreate?
andupdate?
by toggling a configuration option. I was thinking about something like this:The configuration option changes how the callbacks are defined. That way we don't have to worry about introducing breaking changes to the default behavior, ie in
authorizing_processor.rb
:Assuming that I address edge cases that you mentioned in issue 25 (with the article.comments = comments example) and write corresponding tests and documentation, is this a feature that you would be willing to add to this gem? It's something that we need at my company so I plan on working on it regardless, but I know several other people here have also requested similar functionality.
The text was updated successfully, but these errors were encountered: