这里有一些想法:
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
# ...
http_basic_authenticate_with :name => "frodo", :password => "thering", :if => :admin_controller?
def admin_controller?
self.class < ActiveAdmin::BaseController
end
或者,monkeypatching 版本
# config/initializers/active_admin.rb
# somewhere outside the setup block
class ActiveAdmin::BaseController
http_basic_authenticate_with :name => "frodo", :password => "thering"
end
如果只想保护特定资源,可以使用控制器块:
# app/admin/users.rb
ActiveAdmin.register Users do
controller do
http_basic_authenticate_with :name => "frodo", :password => "thering"
end
# ...
end
我希望能够在config/initializers/active_admin.rb
设置块中以这种方式扩展控制器,但这对我不起作用:
# app/admin/users.rb
ActiveAdmin.setup do |config|
config.controller do
http_basic_authenticate_with :name => "frodo", :password => "thering"
end
# ...
end
不过,您可以尝试一下,因为它可能是 ActiveAdmin 版本的东西(我可以发誓我在某处看到了文档......)
祝你好运,我希望这会有所帮助。
更新:更多选择:
在那之前我没有意识到 :before_filter 在 activeadmin 配置中占用了一个块。
# config/initializers/active_admin.rb
ActiveAdmin.setup do |config|
# ...
config.before_filter do
authenticate_or_request_with_http_basic("Whatever") do |name, password|
name == "frodo" && password == "thering"
end
end
end
而且……还有一个想法。听起来您并不热衷于向 application_controller 添加任何内容,但此版本不像上面的第一个版本那样有条件:
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def authenticate_admin
authenticate_or_request_with_http_basic("Whatever") do |name, password|
name == "frodo" && password == "thering"
end
end
end
# config/initializers/active_admin.rb
ActiveAdmin.setup do |config|
# ...
config.authentication_method = :authenticate_admin
end