Pundit authorization on activeadmin custom page
Asked Answered
S

1

10

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 ?

Sundowner answered 7/4, 2015 at 9:50 Comment(0)
U
10

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
Utham answered 7/4, 2015 at 10:41 Comment(1)
@Sundowner you have to dive into Activeadmin codebase to find things like theseUtham

© 2022 - 2024 — McMap. All rights reserved.