Class: MongoSessionStorage
- Inherits:
-
Object
- Object
- MongoSessionStorage
- Defined in:
- lib/rsence/session/mongo_sessionstorage.rb
Overview
MongoSessionStorage is the SessionStorage backend for MongoDB Authentication string format is strictly like this for now:
mongodb://username:password@host:port/database
Instance Method Summary collapse
-
#bson_id(_id) ⇒ Object
Converts string-id’s to BSON id’s.
-
#db_close ⇒ Object
Closes database connection.
-
#db_init ⇒ Object
Just binds the collections to their own instance members.
-
#db_open ⇒ Object
Opens database connection.
-
#db_test ⇒ Object
Tests database authentication.
-
#del_upload(upload_id) ⇒ Object
Deletes upload by id.
-
#del_uploads(ticket_id, ses_id) ⇒ Object
Deletes upload by ticket_id and ses_id.
-
#get_upload_data(upload_id) ⇒ Object
Gets upload data.
-
#get_upload_meta(upload_id) ⇒ Object
Gets upload metadata only.
-
#insert_session_data(ses_data) ⇒ Object
Inserts new session and returns its id.
-
#legal_bson_str?(_id) ⇒ Boolean
Checks, whether the string can be converted to BSON::ObjectId.
-
#load_session_data ⇒ Object
Finds every document is the rsence_ses collection and calls #restore_session.
-
#new_upload_data(data) ⇒ Object
Creates upload, returns its id as string.
-
#parse_db_uri ⇒ Object
Poor-man’s connection string parser:.
-
#remove_all_session_data ⇒ Object
Removes all session data.
-
#remove_session_data(ses_id) ⇒ Object
Removes session data of a session.
-
#set_upload_data(upload_id, file_data) ⇒ Object
Sets upload data.
-
#store_session_data(ses_data) ⇒ Object
Stores the session data, requires inserted session.
Instance Method Details
#bson_id(_id) ⇒ Object
Converts string-id’s to BSON id’s
50 51 52 53 54 55 56 57 58 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 50 def bson_id( _id ) if legal_bson_str?( _id ) return BSON::ObjectId.from_string( _id ) elsif _id.class == BSON::ObjectId return _id else return nil end end |
#db_close ⇒ Object
Closes database connection
61 62 63 64 65 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 61 def db_close @conn.close @db = nil @db_auth = false end |
#db_init ⇒ Object
Just binds the collections to their own instance members
82 83 84 85 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 82 def db_init @ses_coll = @db['rsence_ses'] @up_coll = @db['rsence_uploads'] end |
#db_open ⇒ Object
Opens database connection
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 26 def db_open # mongodb://rsence:2N74krTMURIpSr6Y91Hy@localhost:37035/rsence_sessions conn = parse_db_uri @conn = Mongo::Connection.new( conn[:host], conn[:port], { :pool_size => @config[:mongo][:pool_size], :pool_timeout => @config[:mongo][:pool_timeout], :auths => [{ :username => conn[:username], :password => conn[:password], :db_name => conn[:db] }] } ) @conn.apply_saved_authentication @db = @conn.db( conn[:db] ) @db_auth = true # on current versions of Mongo, complains about the pool auths # @db_auth = @db.authenticate( conn[:username], conn[:password], true ) end |
#db_test ⇒ Object
Tests database authentication
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 68 def db_test begin db_open rescue => e warn "MongoDB connection exception: #{e.inspect}" return false end return true if @db_auth db_close warn "MongoDB authentication failed" return false end |
#del_upload(upload_id) ⇒ Object
Deletes upload by id
220 221 222 223 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 220 def del_upload( upload_id ) upload_id = bson_id( upload_id ) @up_coll.remove( { '_id' => upload_id } ) end |
#del_uploads(ticket_id, ses_id) ⇒ Object
Deletes upload by ticket_id and ses_id
226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 226 def del_uploads( ticket_id, ses_id ) if legal_bson_str?( ses_id ) ses_id = bson_id( ses_id ) else ses_id = false end if legal_bson_str?( ticket_id ) ticket_id = bson_id( ticket_id ) else ticket_id = ticket_id end @up_coll.remove( { 'ses_id' => ses_id } ) if ses_id @up_coll.remove( { 'ticket_id' => ticket_id } ) if ticket_id end |
#get_upload_data(upload_id) ⇒ Object
Gets upload data
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 186 def get_upload_data( upload_id ) upload_id = bson_id( upload_id ) up_data = @up_coll.find_one({ '_id' => upload_id }, { :fields => [ 'upload_date', 'upload_done', 'file_name', 'file_size', 'file_mime', 'file_data' ] }) return { :upload_date => up_data['upload_date'], :upload_done => up_data['upload_done'], :file_name => up_data['file_name'], :file_size => up_data['file_size'], :file_mime => up_data['file_mime'], :file_data => up_data['file_data'].to_s } end |
#get_upload_meta(upload_id) ⇒ Object
Gets upload metadata only
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 203 def ( upload_id ) upload_id = bson_id( upload_id ) up_data = @up_coll.find_one({ '_id' => upload_id }, { :fields => [ 'upload_date', 'upload_done', 'file_name', 'file_size', 'file_mime' ] }) return { :upload_date => up_data['upload_date'], :upload_done => up_data['upload_done'], :file_name => up_data['file_name'], :file_size => up_data['file_size'], :file_mime => up_data['file_mime'], :file_data => nil } end |
#insert_session_data(ses_data) ⇒ Object
Inserts new session and returns its id
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 109 def insert_session_data( ses_data ) if legal_bson_str?( ses_data[:user_id] ) user_id = bson_id( ses_data[:user_id] ) else user_id = ses_data[:user_id] end ses_id = @ses_coll.insert({ 'cookie_key' => ses_data[:cookie_key], 'ses_key' => ses_data[:ses_key], 'ses_timeout' => ses_data[:ses_timeout], 'user_id' => user_id }) return ses_id.to_s end |
#legal_bson_str?(_id) ⇒ Boolean
Checks, whether the string can be converted to BSON::ObjectId
45 46 47 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 45 def legal_bson_str?( _id ) ( _id.class == String and BSON::ObjectId.legal?( _id ) ) end |
#load_session_data ⇒ Object
Finds every document is the rsence_ses collection and calls #restore_session
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 89 def load_session_data @ses_coll.find.each do |ses_row| ses_id = ses_row['_id'].to_s ses_data_bin = ses_row['ses_data'] if ses_data_bin.nil? puts "removing #{ses_id}" remove_session_data( ses_id ) else begin ses_data = Marshal.load( ses_data_bin.to_s ) rescue => e warn "Unable to restore session #{ses_id}" remove_session_data( ses_id ) end restore_session( ses_id, ses_data ) end end end |
#new_upload_data(data) ⇒ Object
Creates upload, returns its id as string
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 157 def new_upload_data( data ) ses_id = bson_id( data[:ses_id] ) if legal_bson_str?( data[:ticket_id] ) ticket_id = bson_id( data[:ticket_id] ) else ticket_id = data[:ticket_id] end return @up_coll.insert({ 'ses_id' => ses_id, 'ticket_id' => ticket_id, 'upload_date' => data[:upload_date], 'upload_done' => data[:upload_done], 'file_name' => data[:file_name], 'file_size' => data[:file_size], 'file_mime' => data[:file_mime], 'file_data' => BSON::Binary.new( data[:file_data] ) }).to_s end |
#parse_db_uri ⇒ Object
Poor-man’s connection string parser:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 10 def parse_db_uri db_str = @db_uri.split('mongodb://')[1] ( db_auth_str, db_conn_str ) = db_str.split('@') ( db_username, db_password ) = db_auth_str.split(':') ( db_host, db_port_name_str ) = db_conn_str.split(':') ( db_port, db_name ) = db_port_name_str.split('/') return { :host => db_host, :port => db_port, :username => db_username, :password => db_password, :db => db_name } end |
#remove_all_session_data ⇒ Object
Removes all session data
151 152 153 154 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 151 def remove_all_session_data @ses_coll.remove @up_coll.remove end |
#remove_session_data(ses_id) ⇒ Object
Removes session data of a session
144 145 146 147 148 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 144 def remove_session_data( ses_id ) ses_id = bson_id( ses_id ) @ses_coll.remove({'_id' => ses_id}) @up_coll.remove({'ses_id' => ses_id}) end |
#set_upload_data(upload_id, file_data) ⇒ Object
Sets upload data
177 178 179 180 181 182 183 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 177 def set_upload_data( upload_id, file_data ) upload_id = bson_id( upload_id ) @up_coll.update({'_id' => upload_id}, { '$set' => { 'file_data' => BSON::Binary.new( file_data ), 'upload_done' => true }}) end |
#store_session_data(ses_data) ⇒ Object
Stores the session data, requires inserted session
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rsence/session/mongo_sessionstorage.rb', line 125 def store_session_data( ses_data ) ses_id = bson_id( ses_data[:ses_id] ) if legal_bson_str?( ses_data[:user_id] ) user_id = bson_id( ses_data[:user_id] ) else user_id = ses_data[:user_id] end ses_data_bin = BSON::Binary.new( Marshal.dump( ses_data ) ) @ses_coll.update({'_id' => ses_id}, {'$set' => { 'cookie_key' => ses_data[:cookie_key], 'ses_key' => ses_data[:ses_key], 'user_id' => user_id, 'ses_data' => ses_data_bin, 'ses_timeout' => ses_data[:timeout], 'ses_stored' => Time.now.to_i }}) end |