4

我正在尝试使用Sorcery而不是设计来安装Rails Admin Gem进行身份验证。

Rails admin 确实提供了一个钩子,您可以使用它来附加您自己的身份验证方法。是他们在文档中提供的示例(使用warden):

config.authenticate_with do
  warden.authenticate! :scope => :admin
end
config.current_user_method { current_admin }

我猜在块内我需要引用before_filterSorcery 用来验证用户的require_login.

但是,当我尝试这样做并/admin在注销时尝试访问时,会出现路由错误:

No route matches {:action=>"new", :controller=>"sessions"}

这可能是因为我在引擎中而不是在主应用程序中被重定向。

如何正确设置?

4

2 回答 2

7
# config/initializers/rails_admin.rb
RailsAdmin.config do |config|
  config.authenticate_with do
    # Use sorcery's before filter to auth users
    require_login
  end
end

# app/controllers/application_controller.rb
class ApplicationController
  # Overwrite the method sorcery calls when it
  # detects a non-authenticated request.
  def not_authenticated
    # Make sure that we reference the route from the main app.
    redirect_to main_app.login_path
  end
end

#config/initializers/rails_admin.rb
RailsAdmin.config do |config|
  ...
  config.parent_controller = 'ApplicationController'
end
于 2012-03-23T06:06:03.103 回答
0

如果您将 Sorcery 与Cancancan gem 一起使用,您还应该config.current_user_method(&:current_user)config/initializers/rails_admin.rb文件中添加,否则您会收到错误:You are not authorized.

于 2016-01-25T14:58:22.147 回答