2009-11-12 37 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通過源瀏覽鍵生成會話密鑰。如果您需要更多幫助,那可能是開始尋找的最佳地點。

希望這會有所幫助