Changeset 225

Show
Ignore:
Timestamp:
09/26/2007 03:52:10 (14 months ago)
Author:
zimbatm
Message:

Working on Sessions and database :

  • FIX: Session table was always created
  • FIX: Sessions weren't stored correctly because of the custom
    non-autoincrement id. Using SQL injection to bypass the forced "id" :-]
    (aka. where opinionated software is not always good)
  • ENHANCEMENT: Added index for sessions's hashid for faster lookup
  • FIX: 'sqlite3_api' was loaded after the connection.
Location:
trunk/lib/camping
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/camping/db.rb

    r214 r225  
    7575Camping::S.sub! /def\s*Y[;\s]*self[;\s]*end/, $AR_EXTRAS 
    7676Object.constants.map{|c|Object.const_get(c)}.each do |c| 
    77   c::Models.module_eval $AR_EXTRAS if c.respond_to?(:run) 
     77  c::Models.module_eval $AR_EXTRAS if c.respond_to?(:goes) 
    7878end 
  • trunk/lib/camping/reloader.rb

    r208 r225  
    137137            # If database models are present, `autoload?` will return nil. 
    138138            unless Camping::Models.autoload? :Base 
    139                 require 'logger' 
    140                 require 'camping/session' 
    141                 Camping::Models::Base.establish_connection @database if @database 
    142  
    143                 case @log 
    144                 when Logger 
    145                     Camping::Models::Base.logger = @log 
    146                 when String 
    147                     Camping::Models::Base.logger = Logger.new(@log == "-" ? STDOUT : @log) 
    148                 end 
    149  
    150                 Camping::Models::Session.create_schema 
    151  
    152139                if @database and @database[:adapter] == 'sqlite3' 
    153140                    begin 
     
    158145                    end 
    159146                end 
     147 
     148                case @log 
     149                when Logger 
     150                    Camping::Models::Base.logger = @log 
     151                when String 
     152                    require 'logger' 
     153                    Camping::Models::Base.logger = Logger.new(@log == "-" ? STDOUT : @log) 
     154                end 
     155 
     156                Camping::Models::Base.establish_connection @database if @database 
     157 
     158                if Camping::Models.const_defined?(:Session) 
     159                  Camping::Models::Session.create_schema 
     160                end 
    160161            end 
    161162        end 
  • trunk/lib/camping/session.rb

    r205 r225  
    1212# For a basic tutorial, see the *Getting Started* section of the Camping::Session module. 
    1313require 'camping' 
     14require 'camping/db' 
    1415 
    1516module Camping::Models 
     
    1819class Session < Base 
    1920    serialize :ivars 
     21    # SQL injection to bypass id field checks 
     22    set_primary_key '"="" OR "' 
     23 
    2024    def []=(k, v) # :nodoc: 
    2125        self.ivars[k] = v 
     
    3943    # If none is found, generates a new session. 
    4044    def self.persist cookies 
     45        session = nil 
    4146        if cookies.camping_sid 
    4247            session = Camping::Models::Session.find_by_hashid cookies.camping_sid 
     
    6469            ActiveRecord::Schema.define do 
    6570                create_table :sessions, :force => true, :id => false do |t| 
    66                     t.column :hashid,      :string,  :limit => 32 
     71                    t.column :hashid,      :string,  :limit => 32, :null => false 
    6772                    t.column :created_at,  :datetime 
    6873                    t.column :ivars,       :text 
    6974                end 
     75                add_index :sessions, [:hashid], :unique => true 
    7076            end 
    7177            reset_column_information 
     
    109115        @state = (session[app] ||= Camping::H[]) 
    110116        hash_before = Marshal.dump(@state).hash 
    111         s = super(*a) 
     117        return super(*a) 
     118    ensure 
    112119        if session 
    113120            hash_after = Marshal.dump(@state).hash 
     
    117124            end 
    118125        end 
    119         s 
    120126    end 
    121127end