任何人都知道如何通过RoR上的session_id找到会话? 我在我的项目中使用Authlogic我不知道这是否相关
答案 0 :(得分:1)
我自己没有必要这样做,而且我不确定为什么有人可能需要这样做。
查看源代码,我可以看到可能有这样做的方法。
在Authlogic :: Session :: Persistence Module中有一个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不是nil,那么它会使用该键查找会话。
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的来源收集了大部分内容。如果您需要更多帮助,这可能是开始寻找的最佳场所。
希望这有帮助