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
I found an example of authentication in the user-account-app-cookbook, but I'm looking for some guidance on best practices to achieve authorization with resources.
I could achieve simple authorization in the controller, but I'm mostly worried about sideloading relationships. Accessing the current_user in the context for resources is a start.
Example, say resource A has_many of resource B. Resource A is publically accessible, but B is not. I can easily do this in separate controllers for A and B, but only logged in users should be able to sideload B through A. I hope that makes sense.
Hi @ambethia, we typically recommend relying on the base_scope of a resource to scope the lookups to everything a given user should have access to. If your controller has a current_user object and you wanted to only be able to retrieve posts which belonged to that user in a PostResource, you could do the following:
Even if you are sideloading posts through a different resource, this scoping will be applied. If you want to scope the lookup differently based on whether the model is being read, updated, or deleted, you can access the Graphiti.context[:namespace] object in within the base_scope method:
The above would only look up a current user's posts for update or destroy actions but would allow a user to see all published posts as well as their own for reads.
If instead you need to limit certain fields that can be taken on a given model, you can use read or write guards on specific fields or sideloads. If you wanted to let a user update their profile, but only admins could set other users as admins:
# Assume the scoping for retrieving an editable user is already implementedclassUserResource < ApplicationResourceattribute:first_name,:string,writable: trueattribute:last_name,:string,writable: trueattribute:is_admin,:string,readable: :admin?,writable: :admin?defadmin?context.current_user.admin?endend
The guard methods also receive the instance in question as an argument, so you can run more dynamic logic against the specific instance as well.
Let me know if this clears everything up. Would love to update the sample app to include more of these examples at some point as well.
Reacted by Kevin Berthier, Carlos Jerez, Dan Leyden and Rinas
Also, your specific example of not wanting to sideload if a user is not logged in a can most simply be resolved with an empty base scope using ActiveRecord's none query method.
Activity
wadetandy commentedon May 6, 2019
Hi @ambethia, we typically recommend relying on the
base_scopeof a resource to scope the lookups to everything a given user should have access to. If your controller has acurrent_userobject and you wanted to only be able to retrieve posts which belonged to that user in aPostResource, you could do the following:Even if you are sideloading posts through a different resource, this scoping will be applied. If you want to scope the lookup differently based on whether the model is being read, updated, or deleted, you can access the
Graphiti.context[:namespace]object in within thebase_scopemethod:The above would only look up a current user's posts for update or destroy actions but would allow a user to see all published posts as well as their own for reads.
If instead you need to limit certain fields that can be taken on a given model, you can use read or write guards on specific fields or sideloads. If you wanted to let a user update their profile, but only admins could set other users as admins:
The guard methods also receive the instance in question as an argument, so you can run more dynamic logic against the specific instance as well.
Let me know if this clears everything up. Would love to update the sample app to include more of these examples at some point as well.
wadetandy commentedon May 6, 2019
Also, your specific example of not wanting to sideload if a user is not logged in a can most simply be resolved with an empty base scope using ActiveRecord's
nonequery method.ambethia commentedon May 6, 2019
@wadetandy Thank you! this probably covers almost everything I need! Thank you!
richmolj commentedon May 6, 2019
Thanks @ambethia! We'll work on getting an Auth Cookbook up.