Commit 3326fe906a3d5195e51484c3218f28a98dec9c8b

Authored by Braulio Bhavamitra
1 parent e46033bd

Associate user with sessions

app/models/session.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +class Session < ActiveRecord::SessionStore::Session
  2 +
  3 + # removed and redefined on super class
  4 + def self.find_by_session_id session_id
  5 + super
  6 + end
  7 +
  8 + belongs_to :user
  9 +
  10 + before_save :copy_to_columns
  11 +
  12 + protected
  13 +
  14 + def copy_to_columns
  15 + self.user_id = self.data['user']
  16 + end
  17 +
  18 +end
... ...
app/models/user.rb
... ... @@ -96,6 +96,8 @@ class User &lt; ActiveRecord::Base
96 96 has_one :person, :dependent => :destroy
97 97 belongs_to :environment
98 98  
  99 + has_many :sessions, dependent: :destroy
  100 +
99 101 attr_protected :activated_at
100 102  
101 103 # Virtual attribute for the unencrypted password
... ...
config/application.rb
... ... @@ -126,7 +126,7 @@ module Noosfero
126 126 # Make sure the secret is at least 30 characters and all random,
127 127 # no regular words or you'll be exposed to dictionary attacks.
128 128 config.secret_token = noosfero_session_secret
129   - config.session_store :cookie_store, :key => '_noosfero_session'
  129 + config.session_store :active_record_store, key: '_noosfero_session'
130 130  
131 131 config.paths['db/migrate'] += Dir.glob "#{Rails.root}/{baseplugins,config/plugins}/*/db/migrate"
132 132 config.i18n.load_path += Dir.glob "#{Rails.root}/{baseplugins,config/plugins}/*/locales/*.{rb,yml}"
... ...
config/initializers/session.rb 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +ActionDispatch::Reloader.to_prepare do
  2 + ActiveRecord::SessionStore.session_class = Session
  3 +end
  4 +
... ...
db/migrate/20150625234824_add_user_id_to_session.rb 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +class AddUserIdToSession < ActiveRecord::Migration
  2 +
  3 + def change
  4 + add_column :sessions, :user_id, :integer
  5 + add_index :sessions, :user_id
  6 + end
  7 +
  8 + def up
  9 + Session.reset_column_information
  10 +
  11 + # cleanup data: {}
  12 + Session.where(data: "BAh7AA==\n").delete_all
  13 + # cleanup data with lang key only
  14 + Session.where("data ~ 'BAh7BjoJbGFuZyIH.{3,3}=\n'").delete_all
  15 +
  16 + # very slow migration, only do for the last month
  17 + Session.where('updated_at > ?', 1.month.ago).find_each batch_size: 50 do |session|
  18 + begin
  19 + # this calls Session#copy_to_columns
  20 + session.save!
  21 + rescue ArgumentError
  22 + # old ActionController::Flash::FlashHash from rails 2.3
  23 + session.destroy
  24 + end
  25 +
  26 + # limit limitless allocations
  27 + GC.start
  28 + end
  29 + end
  30 +
  31 +end
... ...
db/schema.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(:version => 20150603182105) do
  14 +ActiveRecord::Schema.define(:version => 20150625234824) do
15 15  
16 16 create_table "abuse_reports", :force => true do |t|
17 17 t.integer "reporter_id"
... ... @@ -645,10 +645,12 @@ ActiveRecord::Schema.define(:version =&gt; 20150603182105) do
645 645 t.text "data"
646 646 t.datetime "created_at"
647 647 t.datetime "updated_at"
  648 + t.integer "user_id"
648 649 end
649 650  
650 651 add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
651 652 add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"
  653 + add_index "sessions", ["user_id"], :name => "index_sessions_on_user_id"
652 654  
653 655 create_table "suggestion_connections", :force => true do |t|
654 656 t.integer "suggestion_id", :null => false
... ...