2009-11-12 51 views

回答

0

我自己没有必须这样做,我不确定为什么有人可能需要这样做。

翻阅源代码,我可以看到有可能是这样做的一种方式。

在Authlogic :: Session :: Persistence模块中有一个find方法。您可以拨打使用UserSession.find这种方法,它似乎具有搜索能力基础上的session_id

# This is how you persist a session. This finds the record for the current session using 
    # a variety of methods. It basically tries to "log in" the user without the user having 
    # to explicitly log in. Check out the other Authlogic::Session modules for more information. 
    # 
    # The best way to use this method is something like: 
    # 
    # helper_method :current_user_session, :current_user 
    # 
    # def current_user_session 
    # return @current_user_session if defined?(@current_user_session) 
    # @current_user_session = UserSession.find 
    # end 
    # 
    # def current_user 
    # return @current_user if defined?(@current_user) 
    # @current_user = current_user_session && current_user_session.user 
    # end 
    # 
    # Also, this method accepts a single parameter as the id, to find session that you marked with an id: 
    # 
    # UserSession.find(:secure) 
    # 
    # See the id method for more information on ids. 
    def find(id = nil, priority_record = nil) 
     session = new({:priority_record => priority_record}, id) 
     session.priority_record = priority_record 
     if session.persisting? 
     session 
     else 
     nil 
     end 
    end 
    end 

该方法的文档是指Authlogic :: Session类。

在Authlogic :: Session :: Session :: Config中,它表示会话密钥可以是cookie密钥,字符串或符号。

module Config 
    # Works exactly like cookie_key, but for sessions. See cookie_key for more info. 
    # 
    # * <tt>Default:</tt> cookie_key 
    # * <tt>Accepts:</tt> Symbol or String 
    def session_key(value = nil) 
     rw_config(:session_key, value, cookie_key) 
    end 
    alias_method :session_key=, :session_key 
    end 

因此,在随后它试图找到当前会话的方法,我们可以看到,如果RECORD_ID不为零然后查找使用该密钥对会话。

 def persist_by_session 
     persistence_token, record_id = session_credentials 
     if !persistence_token.nil? 
      # Allow finding by persistence token, because when records are created the session is maintained in a before_save, when there is no id. 
      # This is done for performance reasons and to save on queries. 
      record = record_id.nil? ? 
      search_for_record("find_by_persistence_token", persistence_token) : 
      search_for_record("find_by_#{klass.primary_key}", record_id) 
      self.unauthorized_record = record if record && record.persistence_token == persistence_token 
      valid? 
     else 
      false 
     end 
     end 

record_id是使用session_credentials方法创建的。这似乎是基于提供给控制器

 def session_credentials 
     [controller.session[session_key], controller.session["#{session_key}_#{klass.primary_key}"]].compact 
     end 

     def session_key 
     build_key(self.class.session_key) 
     end 

我收集这个最由Github通过源浏览键生成会话密钥。如果您需要更多帮助,那可能是开始寻找的最佳地点。

希望这会有所帮助