Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pundit authorization on activeadmin custom page

In a Rails 4 app with activeadmin gem (current master branch) I use Pundit for authorization. It works well for ressources but I don't manage to make it work for pages.

Given for example :

ActiveAdmin.register_page "Home" do
    content do
        para "some text"
    end
end

How would I authorize it for specific user ?

By reading the Pundit readme I tried with the following code but it does not work

class HomePolicy < Struct.new(:user, :home)
  def index?
    true
  end

  def show?
    true
  end
end

Any idea ?

like image 955
Alex Avatar asked Apr 07 '15 09:04

Alex


1 Answers

Here is an example policy that I'm using for the dashboard. I have placed it under policies/active_admin/page_policy.rb. Maybe this might be of some help

class ActiveAdmin::PagePolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record
  end

  def show?
    case record.name
    when 'Dashboard'
      true
    else
      user.admin?
    end
  end
end
like image 165
bsvin33t Avatar answered Nov 07 '22 06:11

bsvin33t