auto-session-timeout 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ 4/22/09 - Initial import [Matthew Bass]
@@ -0,0 +1,16 @@
1
+ Copyright (c) 2009 Matthew Bass (http://matthewbass.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4
+ and associated documentation files (the "Software"), to deal in the Software without
5
+ restriction, including without limitation the rights to use, copy, modify, merge, publish,
6
+ distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ Software is furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or
10
+ substantial portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13
+ BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,107 @@
1
+ = auto-session-timeout
2
+
3
+ Provides automatic session timeout in a Rails application. Very easy
4
+ to install and configure. Have you ever wanted to force your users
5
+ off your app if they go idle for a certain period of time? Many
6
+ online banking sites use this technique. If your app is used on any
7
+ kind of public computer system, this plugin is a necessity.
8
+
9
+
10
+ == Installation
11
+
12
+ Install the gem directly:
13
+
14
+ sudo gem install pelargir-auto-session-timeout --source=http://gems.github.com
15
+
16
+ Or install the gem in your Rails project:
17
+
18
+ script/plugin install git://github.com/pelargir/auto-session-timeout.git
19
+
20
+ Or clone the project:
21
+
22
+ git clone git://github.com/pelargir/auto-session-timeout.git
23
+
24
+
25
+ == Usage
26
+
27
+ After installing, tell your application controller to use auto timeout:
28
+
29
+ class ApplicationController < ActionController::Base
30
+ auto_session_timeout 1.hour
31
+ ...
32
+ end
33
+
34
+ You will also need to insert this line inside the <body></body> tags in
35
+ your views. The easiest way to do this is to insert it once inside your
36
+ default or application-wide layout. Make sure you are only rendering
37
+ it if the user is logged in, otherwise the plugin will attempt to force
38
+ non-existent sessions to timeout, wreaking havoc:
39
+
40
+ <html>
41
+ <head>...</head>
42
+ <body>
43
+ <% if logged_in? -%>
44
+ <%= auto_session_timeout_js %>
45
+ <% end -%>
46
+ ...
47
+ </body>
48
+ </html>
49
+
50
+ You need to setup two actions: one to return the session status and
51
+ another that runs when the session times out. You can use the default
52
+ actions included with the plugin by inserting this line in your target
53
+ controller (most likely your user or session controller):
54
+
55
+ class SessionsController < ApplicationController
56
+ auto_session_timeout_actions
57
+ ...
58
+ end
59
+
60
+ To customize the default actions, simply override them. You can call
61
+ the render_session_status and render_session_timeout methods to use
62
+ the default implementation from the plugin, or you can define the
63
+ actions entirely with your own custom code:
64
+
65
+ class SessionsController < ApplicationController
66
+ def active
67
+ render_session_status
68
+ end
69
+
70
+ def timeout
71
+ render_session_timeout
72
+ end
73
+ ...
74
+ end
75
+
76
+ In any of these cases, make sure to properly map the actions in
77
+ your routes.rb file:
78
+
79
+ map.active '/active', :controller => 'sessions', :action => 'active'
80
+ map.timeout '/timeout', :controller => 'sessions', :action => 'timeout'
81
+
82
+ You're done! Enjoy watching your sessions automatically timeout.
83
+
84
+
85
+ == Additional Configuration
86
+
87
+ By default, the JavaScript code checks the server every 60 seconds for
88
+ active sessions. If you prefer that it check more frequently, pass a
89
+ frequency attribute to the helper method. The frequency is given in
90
+ seconds. The following example checks the server every 15 seconds:
91
+
92
+ <html>
93
+ <head>...</head>
94
+ <body>
95
+ <% if logged_in? -%>
96
+ <%= auto_session_timeout_js :frequency => 15 %>
97
+ <% end -%>
98
+ ...
99
+ </body>
100
+ </html>
101
+
102
+
103
+ == Resources
104
+
105
+ Repository: http://github.com/pelargir/auto-session-timeout/
106
+ Blog: http://matthewbass.com
107
+ Author: Matthew Bass
@@ -0,0 +1,9 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new('test') do |t|
8
+ t.pattern = 'test/*_test.rb'
9
+ end
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "auto-session-timeout"
3
+ s.version = "0.5"
4
+ s.date = "2009-04-22"
5
+ s.summary = "Provides automatic session timeout in a Rails application."
6
+ s.email = "pelargir@gmail.com"
7
+ s.homepage = "http://github.com/pelargir/auto-session-timeout"
8
+ s.description = "Adds several handy expectations for testing ActiveRecord model validations."
9
+ s.has_rdoc = true
10
+ s.authors = ["Matthew Bass"]
11
+ s.files = [
12
+ "auto_session_timeout.gemspec",
13
+ "CHANGELOG",
14
+ "init.rb",
15
+ "lib/auto_session_timeout.rb",
16
+ "lib/auto_session_timeout_helper.rb",
17
+ "MIT-LICENSE",
18
+ "Rakefile",
19
+ "README",
20
+ "test/auto_session_timeout_test.rb",
21
+ "test/test_helper.rb"
22
+ ]
23
+ s.rdoc_options = ["--main", "README"]
24
+ s.extra_rdoc_files = ["README"]
25
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'auto_session_timeout'
2
+ require 'auto_session_timeout_helper'
@@ -0,0 +1,39 @@
1
+ module AutoSessionTimeout
2
+
3
+ def self.included(controller)
4
+ controller.extend ClassMethods
5
+ controller.hide_action :render_auto_session_timeout
6
+ end
7
+
8
+ module ClassMethods
9
+ def auto_session_timeout(seconds)
10
+ prepend_before_filter do |c|
11
+ if c.session[:auto_session_expires_at] && c.session[:auto_session_expires_at] < Time.now
12
+ c.send :reset_session
13
+ else
14
+ unless c.send(:active_url) == c.url_for(c.params)
15
+ c.session[:auto_session_expires_at] = Time.now + seconds
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ def auto_session_timeout_actions
22
+ define_method(:active) { render_session_status }
23
+ define_method(:timeout) { render_session_timeout }
24
+ end
25
+ end
26
+
27
+ def render_session_status
28
+ response.headers["Etag"] = "" # clear etags to prevent caching
29
+ render :text => logged_in?, :status => 200
30
+ end
31
+
32
+ def render_session_timeout
33
+ flash[:notice] = "Your session has timed out."
34
+ redirect_to "/login"
35
+ end
36
+
37
+ end
38
+
39
+ ActionController::Base.send :include, AutoSessionTimeout
@@ -0,0 +1,13 @@
1
+ module AutoSessionTimeoutHelper
2
+ def auto_session_timeout_js(options={})
3
+ frequency = options[:frequency] || 60
4
+ code = <<JS
5
+ new Ajax.PeriodicalUpdater('', '/active', {frequency:#{frequency}, method:'get', onSuccess: function(e) {
6
+ if (e.responseText == 'false') window.location.href = '/timeout';
7
+ }});
8
+ JS
9
+ javascript_tag(code)
10
+ end
11
+ end
12
+
13
+ ActionView::Base.send :include, AutoSessionTimeoutHelper
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class AutoSessionTimeoutTest < Test::Unit::TestCase
4
+
5
+ def test_something
6
+ assert true
7
+ end
8
+
9
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'action_controller'
4
+
5
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/auto_session_timeout')
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto-session-timeout
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.5"
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Bass
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-22 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Adds several handy expectations for testing ActiveRecord model validations.
17
+ email: pelargir@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - auto_session_timeout.gemspec
26
+ - CHANGELOG
27
+ - init.rb
28
+ - lib/auto_session_timeout.rb
29
+ - lib/auto_session_timeout_helper.rb
30
+ - MIT-LICENSE
31
+ - Rakefile
32
+ - README
33
+ - test/auto_session_timeout_test.rb
34
+ - test/test_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/pelargir/auto-session-timeout
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --main
42
+ - README
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.4
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Provides automatic session timeout in a Rails application.
64
+ test_files: []
65
+