mongoid_session_store_json 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,43 @@
1
+ = mongoid_session_store_json
2
+
3
+ Stores rails 3 sessions in mongoDB using mongoid, after converting
4
+ the session to json, and applying some base64 action.
5
+
6
+ == Installation
7
+ This gem supports rails 3, rails 3.1 and mongoid 2.3+
8
+
9
+ gem install mongoid_session_store
10
+
11
+ == Setup
12
+
13
+ In your Gemfile:
14
+
15
+ gem "mongoid", "~> 2.3.3"
16
+ gem "bson_ext", "1.3.1"
17
+ gem "mongoid_session_store_json"
18
+
19
+ In the session_store initializer (config/initializers/session_store.rb) you can comment out every line as
20
+ mongoid session store will be setup for use when added to the gemfile.
21
+
22
+ If you want to be more explicit you can comment the currently setup store and add:
23
+
24
+ Example::Application.config.session_store :mongoid_store
25
+
26
+ == Rake Tasks
27
+ Mongoid Session Store comes with a rake task to clear out the sessions it stores in mongoDB
28
+
29
+ rake db:mongoid:sessions:clear
30
+
31
+ == Note on Patches/Pull Requests
32
+
33
+ * Fork the project.
34
+ * Make your feature addition or bug fix.
35
+ * Add tests for it. This is important so I don't break it in a
36
+ future version unintentionally.
37
+ * Commit, do not mess with rakefile, version, or history.
38
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
39
+ * Send me a pull request. Bonus points for topic branches.
40
+
41
+ == Copyright
42
+
43
+ Copyright (c) 2010 Ryan Fitzgerald. See MIT-LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ require 'bundler/gem_tasks'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+ begin
9
+ require 'rdoc/task'
10
+ rescue LoadError
11
+ require 'rdoc/rdoc'
12
+ require 'rake/rdoctask'
13
+ RDoc::Task = Rake::RDocTask
14
+ end
15
+
16
+ RDoc::Task.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'MongoidSessionStore'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+
25
+ require 'rake/testtask'
26
+
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'lib'
29
+ t.libs << 'test'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = false
32
+ end
33
+
34
+
35
+ task :default => :test
@@ -0,0 +1,16 @@
1
+ require 'rails'
2
+
3
+ module MongoidSessionStore
4
+
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ load "tasks/mongoid_session_store_tasks.rake"
8
+ end
9
+
10
+ initializer "setup mongoid session store" do |app|
11
+ require 'mongoid_session_store/mongoid_store'
12
+ app.config.session_store :mongoid_store
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,87 @@
1
+ require 'json'
2
+ require 'base64'
3
+
4
+ module ActionDispatch
5
+ module Session
6
+ class MongoidStore < AbstractStore
7
+
8
+ class Session
9
+ include Mongoid::Document
10
+
11
+ store_in :sessions
12
+
13
+ identity :type => String
14
+
15
+ field :data, :type => String
16
+ end
17
+
18
+ # The class used for session storage.
19
+ cattr_accessor :session_class
20
+ self.session_class = Session
21
+
22
+ SESSION_RECORD_KEY = 'rack.session.record'
23
+ ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY if ::Rails.version >= "3.1"
24
+
25
+ private
26
+
27
+ def get_session(env, sid)
28
+ sid ||= generate_sid
29
+ session = find_session(sid)
30
+ env[SESSION_RECORD_KEY] = session
31
+ [sid, unpack(session.data)]
32
+ end
33
+
34
+ def set_session(env, sid, session_data, options = nil)
35
+ record = get_session_model(env, sid)
36
+ record.data = pack(session_data)
37
+
38
+ # Rack spec dictates that set_session should return true or false
39
+ # depending on whether or not the session was saved or not.
40
+ # However, ActionPack seems to want a session id instead.
41
+ record.save ? sid : false
42
+ end
43
+
44
+ def find_session(id)
45
+ @@session_class.find_or_create_by(:id => id)
46
+ end
47
+
48
+ # def destroy(env)
49
+ # if sid = current_session_id(env)
50
+ # find_session(sid).destroy
51
+ # end
52
+ # end
53
+
54
+ def destroy(env)
55
+ destroy_session(env, current_session_id(env), {})
56
+ end
57
+
58
+ def destroy_session(env, session_id, options)
59
+ if sid = current_session_id(env)
60
+ get_session_model(env, sid).destroy
61
+ env[SESSION_RECORD_KEY] = nil
62
+ end
63
+
64
+ generate_sid unless options[:drop]
65
+ end
66
+
67
+ def get_session_model(env, sid)
68
+ if env[ENV_SESSION_OPTIONS_KEY][:id].nil?
69
+ env[SESSION_RECORD_KEY] = find_session(sid)
70
+ else
71
+ env[SESSION_RECORD_KEY] ||= find_session(sid)
72
+ end
73
+ end
74
+
75
+ def pack(data)
76
+ return nil unless data
77
+ Base64.encode64(JSON.generate(data))
78
+ end
79
+
80
+ def unpack(packed)
81
+ return nil unless packed and packed.length > 0
82
+ JSON.parse(Base64.decode64(packed))
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,10 @@
1
+ namespace :db do
2
+ namespace :mongoid do
3
+ namespace :sessions do
4
+ desc "Clears sessions stored in mongoDB"
5
+ task :clear => :environment do
6
+ ActionDispatch::Session::MongoidStore::Session.destroy_all
7
+ end
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_session_store_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Cammer
9
+ - StyleSaint, Inc.
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-11-15 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: &70192890864340 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70192890864340
26
+ - !ruby/object:Gem::Dependency
27
+ name: mongoid
28
+ requirement: &70192890863860 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - =
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *70192890863860
37
+ - !ruby/object:Gem::Dependency
38
+ name: bson
39
+ requirement: &70192890863400 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - =
43
+ - !ruby/object:Gem::Version
44
+ version: 1.3.1
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *70192890863400
48
+ - !ruby/object:Gem::Dependency
49
+ name: bson_ext
50
+ requirement: &70192890862940 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - =
54
+ - !ruby/object:Gem::Version
55
+ version: 1.3.1
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *70192890862940
59
+ - !ruby/object:Gem::Dependency
60
+ name: mongo
61
+ requirement: &70192889726080 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - =
65
+ - !ruby/object:Gem::Version
66
+ version: 1.3.1
67
+ type: :runtime
68
+ prerelease: false
69
+ version_requirements: *70192889726080
70
+ description: Store rails 3 sessions in mongoDB using a cross-platform format.
71
+ email:
72
+ - ryan@stylesaint.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - lib/mongoid_session_store/mongoid_store.rb
78
+ - lib/mongoid_session_store.rb
79
+ - lib/tasks/mongoid_session_store_tasks.rake
80
+ - MIT-LICENSE
81
+ - Rakefile
82
+ - README.rdoc
83
+ homepage: http://github.com/codebrew/mongoid_session_store
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.11
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Yanked from http://github.com/codebrew/mongoid_session_store, and Ryan Fitzgerald.
107
+ Thanks Ryan!
108
+ test_files: []