-
Notifications
You must be signed in to change notification settings - Fork 107
Authorize ability to access/update specific model attributes #14
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
Comments
In terms of concerns, this definitely is an authorization concern rather than a hydration concern. So the solution is for the Changing the interface will technically be a breaking change, so I'll have to release a v0.5 version with the change in it. I'll see if I can get to that tomorrow or over the weekend. PS: Glad you like the updates! I'm a lot happier with where the library is now and finding the implementation a lot cleaner to use in applications, so it's good to hear you like the updates too. |
To add some context, we previously made our own CrudController to extend our model controllers from. These updates that you've made completely eliminates the need for this and we feel like our API is way more structured now. Just to add some praise 🙏🏻 Thank you for considering and accepting this request :) |
Yeah, I had a CrudController as well on the previous version, which is what has led to this update! Plus I'm a huge fan of composition and separation of concerns, which is where the separate unit classes has come from. Something has occurred to me on this request, which is to point out that the authorization needs to occur before validation - this is logically the correct order and is the same order in which the Laravel However, I think it'd be sensible for me to change the request so that it does things in the following steps:
This at least means that in the authorizer you can interact with the resource knowing that it does conform to the JSON API spec - e.g. you can do To me that makes sense in terms of if the client hasn't sent a valid JSON API request, it can't be authorized. But logical validation shouldn't occur until after authorization because validation messages might reveal something that a client isn't authorized to know. Just wondering if you agree/disagree with any of that? |
I reckon that's the way to do it. My only concern would be the ease with which a check for unauthorized attributes in request payload would be tedious and "difficult" to implement. If it could be as simple as having a getter which returns whitelisted attributes, that'd be my dream scenario I think. Otherwise I believe you're spot on and got every aspect in your description. |
Yep, if you do So you can use |
I would still have to process that array and compare the values to the persisted values. Considering the use case where a client are allowed to pass all attributes, as long as they are not changed. But maybe that's too application specific, so I would just build that logic myself. Any input? |
Yeah, I think at the moment it's too application specific. A resource's attribute values can be anything - including complex sub-objects, arrays, etc. So it'd be quite difficult for a generic library to know how to diff the two. If you're trying to enforce immutability of values, then there are multiple strategies to do this. Diffing in the authorizer is one. The others would be to validate that the provided value matches the existing value. You can do that as you have access to the existing record when creating your attribute rules:
There's limitations (the The other strategy is just not to hydrate values that aren't changeable. E.g. in the hydrator if
The limitation of that is the client gets no feedback that it has tried to change an immutable attribute. It depends on your application as to which approach is going to fit your needs. |
I'd rather do a comparison in the authorizer then to throw an error to the client. I think that a change of an unauthorized attribute would be a programming error, thus it would be more beneficial to respond with an error rather than setting the allowed attributes and not respond with an error. Great feedback and ideas, thanks! |
Closing as this is available via Componser on the |
Uh oh!
There was an error while loading. Please reload this page.
We're in the process of updating our API to use the newest version of this lib. Really great work with the recent updates!
One thing we're in doubt about now though, is how we would go about whitelisting attributes for updating based on which user has been authorized.
It doesn't seem like the dirty attributes are available in the Authorizer. But maybe the correct way would be to check for unauthorized change of attributes via the Hydrator? If that's the case, I'm not sure how to throw an error, in the case that I would like to accept some changed attributes, but return an error to tell which ones didn't update.
Do you have any insight into this?
The text was updated successfully, but these errors were encountered: