Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Created March 23, 2009 03:47
Show Gist options
  • Select an option

  • Save xaviershay/83420 to your computer and use it in GitHub Desktop.

Select an option

Save xaviershay/83420 to your computer and use it in GitHub Desktop.
# Change login from script/console
# Grab the session_id from your server log
module ConsoleHelpers
def set_session(session_id)
Thread.current[:session_id] = session_id
end
def login(user)
session_id = Thread.current[:session_id]
raise "Must set current session: set_session(session_id)" unless session_id
session_class = Class.new(ActiveRecord::Base) do
set_table_name "sessions"
attr_accessor :hash_data
def hash_data
@hash_data ||= Marshal.load(Base64.decode64(data))
end
def save_data!
self.data = Base64.encode64(Marshal.dump(hash_data))
self.save!
end
end
user = User.find_by_user_name(user) unless user.is_a?(User)
current_session = session_class.find_by_session_id(session_id)
current_session.hash_data[:user_id] = user.id
current_session.save_data!
end
end
# Usage
>> extend ConsoleHelpers
=> #<Object:0x3799c @controller=#<ApplicationController:0x4825984>
>> set_session('2a19ba7bdf80a275d6215b218ddfe486')
=> "2a19ba7bdf80a275d6215b218ddfe486"
>> login('username')
=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment