Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check cancan permission on ActiveAdmin custom page?

I've the following Ability:

can :manage, ActiveAdmin::Page, name: 'My Page'

And it is working fine, but I want to check if the user has the ability to manage this ActiveAdmin::Page in different pages. So, when I used the following:

can? :manage, ActiveAdmin::Page, name: 'My Page'

It returned true for any page even if it does not exist!

like image 677
Mahmoud M. Abdel-Fattah Avatar asked Jan 25 '17 11:01

Mahmoud M. Abdel-Fattah


1 Answers

The code

can? :manage, ActiveAdmin::Page, name: 'My Page'

checks if the current user can manage an instance of ActiveAdmin::Page whose name attribute is equal to My Page. That is exactly the condition you stated in your abilities file, and that is why it always returns true.

In case you want to know if a user can or cannot access a specific page (which is what I think you are trying to do) you should ask "can I manage this page?" instead of "can I manage a page whose name is My Page?". In the former you are talking about a specific page, and in the later you are talking about pages with a certain characteristic.

In order to ask CanCan whether the current user can access a specific page, just ask:

can? :manage, my_specific_page

where my_specific_page needs to be an instance of ActiveAdmin::Page.

like image 94
Bustikiller Avatar answered Oct 19 '22 11:10

Bustikiller