开发者

Implementing authlogic for rails_admin

开发者 https://www.devze.com 2023-03-11 03:00 出处:网络
I am using rails_admin and I liked it very much. The only problem is that it is coupled with devise for authentication, but my whole app is implementing authlogic.

I am using rails_admin and I liked it very much. The only problem is that it is coupled with devise for authentication, but my whole app is implementing authlogic. Ain't开发者_JS百科 there any way to remove devise so that I can switch to my existing authlogic authentication. I googled it but couldn't find :( Regards; Kshitiz


I don't know about removing devise, it seems to be a requirement for installing rails_admin. So you need to install it even if you don't use it.

But you can make rails_admin use authlogic for authentication and authorization like so.

  • In your config/initializers folder, create a new file calls rails_admin.rb
  • Put the following inside the file (note that I used nifty_generators so I have some helper methods available to me like logged_in? etc. You might need to use the equivalent methods in your setup):
  • Now restart your server!

Code:

RailsAdmin.authenticate_with{
  unless logged_in?
    session[:return_to] = request.url
    redirect_to login_url, :alert => "You must first log in or sign up before accessing this page."
  end
}

RailsAdmin.authorize_with{
  redirect_to root_path, :alert => "You are not authorized to access that page" unless current_user.admin? #or whatever you use for checking admins
}


Recently got familiar with CanCan, and man, it's a great permissions interface to have around. I'd been wanting to install cancan at some point, and although it's extremely undocumented, I stumbled on this pull request to rails_admin by Ryan Bates in my search for the same answer. I used this as an excuse to install both CanCan and RailsAdmin, and can confirm that so far at least they fit extremely well together.

The commit instructions are a bit dated, so here's what worked for me.

in your Gemfile

gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'

in config/initializers/rails_admin.rb

RailsAdmin.config do |config|
  config.authorize_with :cancan
end

In your abilities:

can :access, :rails_admin
can :history

you probably already know this, but for prudence, in config/routes.rb

mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'

Finally, if you bundle install in the following way, it won't pull in devise! Hackish, but better than the alternative.

AUTHORIZATION_ADAPTER=cancan bundle install

Anyways, you might not want CanCan, but if you do, you'll probably be stoked to have this work in a supported (although undocumented) way.

0

精彩评论

暂无评论...
验证码 换一张
取 消