I'm currently trying to integrate inherited_resources and authority into my Rails app.
I'm a little bit stuck as to the best place to check the ability to do a controller action based on the resource. This code is given as an example in authority:
def edit
@llama = Llama.find(params[:id])
authorize_action_for(@llama) # Check to see if you're allowed to edit this llama. failure == SecurityViolation
end
def update
@llama = Llama.find(params[:id])
authorize_action_for(@llama) # Check to see if you're allowed to edit this llama.
@llama.attributes = params[:llama] # Don't save the attributes before authorizing
authorize_action_for(@llama) # Check again, to see if the changes are allowed.
if @llama.save?
# etc
end
Because in inherited_resources the finders are abstracted away, I thought it'd be nice to also tack the authorise_action_for
checks onto these abstracted finders.
Note authority's double check in the case of an update (and presumably a create).
I'm relying on
ActiveSupport::Concern
to simplify the module. I store my concerns in a directory calledconcerns
underapp
. I've called this oneinherited_resources_with_authority.rb
and you may need to modify yourautoload_paths
inapplication.rb
to load files from this folder.We're basically chaining important
inherited_resources
' abstract methods and inserting our authorisation code where necessary. The last one is the trickiest as we can't call the original method that we're chaining on to so we have to duplicate some ofinherited_resources
' code here.To use this concern simply call
include InheritedResourcesWithAuthority
from your controller.Note that you must not use the class inheritance method of activating
inherited_resources
on your controller as we're already using the other method in this concern.Full writeup here: https://coderwall.com/p/tp5sig
Suggestions are definitely welcome :D