Have CanCan use Active Admin user?
Asked Answered
M

1

6

I'm using Devise and CanCan for authorization on the frontend of a Rails 3 app. I also have Active Admin as the interface for the backend. I'm trying to create different roles for admins in the backend. Both ends have a login form that uses different 'user' models & tables. The problem is that CanCan fetches the current user from the frontend (grabbing the current user object) and uses that to see if someone in the backend has the correct permissions.

So, how I can have CanCan correctly grab the admin user that's logged in?

If anyone needs more information, I'll be glad to supply it.

Mordacious answered 20/7, 2011 at 12:2 Comment(3)
Why dont you want just restrict access to backend for all except the admin role? Than you dont need to split user model etc.Duda
Because Active Admin uses a separate model and table for its admin users. It uses an AdminUser model while the rest of the site uses a User model. It's set up this way because the db has two distinctly separate tables of admin users and and accounts users for specific reasons of the system.Mordacious
@Mordacious Please also take a look at the blog post mentioned in the comment to this question. There it is described how you can merge the AdminUser and the User models.Bisutun
N
9

I have not used ActiveAdmin before, but have used Devise and Cancan in a couple of projects before.

Having looked at Active Admin Documentation,

Set the method that controllers should call to authenticate the current user with:

  # config/initializers/active_admin.rb
  config.authentication_method = :authenticate_admin_user!

Set the method to call within the view to access the current admin user

  # config/initializers/active_admin.rb
  config.current_user_method = :current_admin_user

You can override Cancan behaviour in your application, by looking at :current_admin_user instead of :current_user.

Refer here Cancan changing defaults.
If you still can't get it, post your problems, where you are stuck.

Nimesh answered 20/7, 2011 at 13:27 Comment(2)
Thanks, Jatin. Actually had to add controller do def current_ability @current_ability ||= AdminAbility.new(current_admin_user) end end to the specific RB file, and then add a 'admin_ability' model to differentiate from CanCan's default Ability model.Mordacious
As it was mentioned in the Cancan Changing Defaults link. Glad that you got it working.Nimesh

© 2022 - 2024 — McMap. All rights reserved.