ruby on rails 3.2 - Integrating inherited_resources and authority -
i'm trying integrate inherited_resources , authority rails app.
i'm little bit stuck best place check ability controller action based on resource. code given example in authority:
def edit @llama = llama.find(params[:id]) authorize_action_for(@llama) # check see if you're allowed edit llama. failure == securityviolation end def update @llama = llama.find(params[:id]) authorize_action_for(@llama) # check see if you're allowed edit llama. @llama.attributes = params[:llama] # don't save attributes before authorizing authorize_action_for(@llama) # check again, see if changes allowed. if @llama.save? # etc end
because in inherited_resources finders abstracted away, thought it'd nice tack authorise_action_for
checks onto these abstracted finders.
note authority's double check in case of update (and presumably create).
i'm relying on activesupport::concern
simplify module. store concerns in directory called concerns
under app
. i've called 1 inherited_resources_with_authority.rb
, may need modify autoload_paths
in application.rb
load files folder.
module inheritedresourceswithauthority extend activesupport::concern included inherit_resources authorize_actions_for :resource_class alias_method_chain :resource, :authority alias_method_chain :build_resource, :authority alias_method_chain :update_resource, :authority end protected def resource_with_authority resource_without_authority authorize_action_for(get_resource_ivar) end def build_resource_with_authority build_resource_without_authority authorize_action_for(get_resource_ivar) end def update_resource_with_authority(object, attributes) object.assign_attributes(*attributes) authorize_action_for(object) object.save end end
we're chaining important inherited_resources
' abstract methods , inserting our authorisation code necessary. last 1 trickiest can't call original method we're chaining on have duplicate of inherited_resources
' code here.
to use concern call include inheritedresourceswithauthority
controller.
note must not use class inheritance method of activating inherited_resources
on controller we're using other method in concern.
full writeup here: https://coderwall.com/p/tp5sig
suggestions welcome :d
Comments
Post a Comment