Search code examples
ruby-on-railsruby-on-rails-5

Rails Admin -> only access if user have role admin


Hello I just discovered the gem railsadmin, I configured it but currently all users have access to the administration part by typing admin in their url, I would like to limit access and allow it only to users with the admin role.

In my user table, I already have a role column and I write "admin" to make a user -> super user

RailsAdmin.config do |config|
 ## == Devise ==
  config.authenticate_with do
    warden.authenticate! scope: :user
  end
  config.current_user_method(&:current_user)
end

Actually all users have access to the administration dashboard


Solution

  • If you are using devise, you can restrict access via routes.rb

    authenticate :user, lambda { |u| u.role == "admin" } do
      mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
    end
    

    Or you can use an authorization gems like cancancan (how to use)