Sha256: 6cf4d7b37497cb117dc51f2a4d50d9d0ef959afce94acd0997d38f37d1683563

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

module ActionDispatch
  module Session
    class MongoidStore < AbstractStore

      class Session
        include Mongoid::Document
        include Mongoid::Timestamps

        field :data, type: String, default: [Marshal.dump({})].pack("m*")

        index updated_at: 1
      end

      # The class used for session storage.
      cattr_accessor :session_class
      self.session_class = Session

      SESSION_RECORD_KEY = 'rack.session.record'.freeze

      private
        def generate_sid
          Moped::BSON::ObjectId.new
        end

        def get_session(env, sid)
          sid ||= generate_sid
          session = find_session(sid)
          env[SESSION_RECORD_KEY] = session
          [sid, unpack(session.data)]
        end

        def set_session(env, sid, session_data, options)
          record = env[SESSION_RECORD_KEY] ||= find_session(sid)
          record.data = pack(session_data)
          # Rack spec dictates that set_session should return true or false
          # depending on whether or not the session was saved or not.
          # However, ActionPack seems to want a session id instead.
          record.save ? sid : false
        end

        def find_session(id)
          id = Moped::BSON::ObjectId.from_string(id.to_s)
          @@session_class.where(_id: id).first || @@session_class.new(id: id)
        end

        def pack(data)
          [Marshal.dump(data)].pack("m*")
        end

        def unpack(packed)
          return nil unless packed
          Marshal.load(packed.unpack("m*").first)
        end

        def destroy(env)
          session = @@session_class.first(conditions: { _id: env[SESSION_RECORD_KEY].id })
          session.try(:destroy)

          env[SESSION_RECORD_KEY] = nil
        end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
locomotive_cms-2.1.4 lib/locomotive/session_store.rb
locomotive_cms-2.1.3 lib/locomotive/session_store.rb
locomotive_cms-2.1.2 lib/locomotive/session_store.rb
locomotive_cms-2.1.1 lib/locomotive/session_store.rb
locomotive_cms-2.1.0 lib/locomotive/session_store.rb