0

So, Here are the relevant routes

 map.namespace "admin" do |admin|
    admin.root :controller => :site_prefs, :action => :index
    admin.resources :site_prefs
    admin.resources :link_pages
    admin.resources :menu_bars
    admin.resources :services
    admin.resources :users
  end

And I have this for one controller:

before_filter :authenticate

  protected

    def authenticate
    authenticate_or_request_with_http_basic do |username, password|
      username == "1234" && password == "1234"
    end
  end

How do I set up my admin controllers to authenticate no matter what page within any of those controllers is navigated to, yet only have it authenticate once among all the admin controllers, and have the code all in one spot.

Right now, the only I can think of to authenticate is to copy the auth code into each controller, and I hate having duplicate code... so.... yeah

3 Answers 3

3

Create a "Admin::BaseController" that inherits from the ApplicationController. Put the before_filter in that controller that handels the basic auth. Then have all your other admin controllers inherit from this BaseController.

Sign up to request clarification or add additional context in comments.

3 Comments

I also like this idea, but apparently i'm failing at creating the new controller. I made by Admin::BaseController < ApplicationController. I added resources :base to the admin routes, the file name of the controller is base_controller.rb... what am I missing?
I fixed my problem by moving teh BaseController to not be in teh admin folder
You don't need to add the base controller as a resource. Resource routing is only to map a URI to a controller. We don't want to use this as a controller, we just want it to exhibit OO principles on a controller. So, for the sake of good design & file layout, I would suggest dropping the "base route" from your routes.rb and move the base controller back into the admin folder. Rails will see it and load it properly as an inherited class, but routes.rb will ignore it, because it won't be declared in there.
1

You could move the authentication code and the before_filter in the ApplicationController.

Your filter code could look at the request.fullpath to see if begins with /admin and if so, authenticate.

Comments

1

Here's a nice clean way to create an Admin::BaseController class in a Rails 3+ app:

  1. Create an admin folder in your controllers folder (app/controllers/admin)
  2. Create a new file in the admin folder: base.rb

Place your controller class in a module:

module Admin
  class Base < ApplicationController

    http_basic_authenticate_with :name => "name", :password => "password"

  end
end

Then for all your other controllers in your admin folder, inherit from Admin::BaseController:

module Admin
  class Users < BaseController
    # method definitions
  end
end

By doing this, you can keep all your admin-related files nice and tidy inside the admin directory, and they will all be authenticated because they inherit from Admin::BaseController.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.