1

我在我的应用程序中使用 authlogic。我也对 A/B 测试进行了拆分。我想拆分使用 authlogic 进行身份验证,而不是 BasicAuthentication 或不进行身份验证。

因此,我将身份验证类设置为拆分 sinatra 应用程序的中间件:

Split::Dashboard.use SinatraAuthlogic

那么这应该是我的 authlogic 中间件:

class SinatraAuthlogic
  def initialize(app)
    @app = app
  end

  def call(env)
    if is_user_logged_in?
      puts @app.class
      response = @app.call env
    else
      response = Rack::Response.new
      response.redirect '/login'
      response.finish
    end
  end

private

  def is_user_logged_in?
    logged_in = UserSession.find && UserSession.find.user
  end
end

问题是我应该在 is_user_logged_in 中放入什么才能使用 authlogic?

4

1 回答 1

0

经过一番尝试,我想出了这个解决方案:

class Authlogic::RackAdapter < Authlogic::ControllerAdapters::AbstractAdapter
  def initialize(env)
    request = Rack::Request.new(env)
    super(request)
    Authlogic::Session::Base.controller = self
  end
end

在我的中间件类中,我修改了“is_user_logged_in?” 这个方法:

def is_user_logged_id?(env)
  begin
    adapter = Authlogic::RackAdapter.new(env)
    logged_in = UserSession.find && UserSession.find.user
    return true
  rescue Exception => e
    return false      
  end
end

这样我就可以找到用户......但是,当用户没有登录时,如果 nil 我得到一个异常,这就是为什么我将整个代码放在救援块中......

欢迎任何更好的方法。

于 2012-11-20T09:00:52.420 回答