views:

140

answers:

1

any one know how to find a session by a session_id on RoR? I'm using Authlogic in my project i don't know if that is correlated

A: 

I haven't had to do this myself, and I'm not sure exactly why somebody might need to do this.

Looking through the source code I can see that there might be a way of doing this.

In the Authlogic::Session::Persistence Module there is a find method. You can call this method using UserSession.find and it appears to have the ability to search based on 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

The documentation for that method refers to the Authlogic::Session class.

In Authlogic::Session::Session::Config it says that the session key can be a cookie key, a string, or a symbol.

  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

So, in the method that follows, which tries to find the current session, we can see that if the record_id is not nil then it looks up the session using that key.

      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 is created with the session_credentials method. Which seems to build a session key based on the key provided to the controller

      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

I gathered most of this by browsing through the source at Github. If you need more help, that may be the best place to start looking.

Hope this helps

Chase M Gray