Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin - using the current_admin_user to filter available actions

I have an active admin resource like this:

ActiveAdmin.register Snippet do

  menu label: "Text Snippets"

  config.clear_sidebar_sections!

  index download_links: false do
    column :key if current_admin_user.developer?
    column :description
    column :contents
    default_actions
  end

  form do |f|
    f.inputs do
      f.input :description
      f.input :contents
    end
    f.buttons
  end

end

Notice in the index block, I'm only adding the key column if the current admin user is a developer. I want to apply this kind of filtering to the available actions.

I tried added this at the top of the resource definition:

actions current_admin_user.developer ? :all : :index, :edit

But I get a NameError on current_admin_user. For some reason, outside of the configuration blocks, the active admin current_admin_user helper doesn't exist.

So, how could I go about filtering actions based on the current user's priviliges?

like image 777
Alex Coplan Avatar asked Feb 20 '23 17:02

Alex Coplan


1 Answers

you have to use a proc... current_admin_user works only when the app it's running, not when you declare your class..

example..

action_item :only => [:edit], :if => proc { current_admin_user.developer? } do
  #enter code here
end

You can also use CanCan for this.. and place controller.authorize_resource at the beginning. Check the activeadmin documentation for this.

Another way to do it is overriding the action_methods in the ActiveAdmin controller.. like this

actions :all

controller do
  def action_methods
    if current_admin_user.developer?
      super
    else
      super - ['show', 'destroy', 'new', 'create']
    end
  end
end

this works cool if you have multiple roles.

btw you should use developer? instead of developer (off course if the developer method returns a boolean as I suspect)

UPDATE

in version 0.6+ you should stick with CanCan, for more information check the activeadmin documentation http://www.activeadmin.info/docs/13-authorization-adapter.html#using_the_cancan_adapter

like image 168
Orlando Avatar answered May 01 '23 11:05

Orlando