auto-session-timeout-warning 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.project ADDED
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>auto-session-timeout-warning</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ </buildSpec>
9
+ <natures>
10
+ <nature>org.radrails.rails.core.railsnature</nature>
11
+ <nature>com.aptana.ruby.core.rubynature</nature>
12
+ </natures>
13
+ </projectDescription>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in auto-session-timeout.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Matthew Bass (http://www.matthewbass.com)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,111 @@
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
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'auto-session-timeout'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install auto-session-timeout
22
+
23
+ ## Usage
24
+
25
+ After installing, tell your application controller to use auto timeout:
26
+
27
+ class ApplicationController < ActionController::Base
28
+ auto_session_timeout 1.hour
29
+ ...
30
+ end
31
+
32
+ You will also need to insert this line inside the body tags in your
33
+ views. The easiest way to do this is to insert it once inside your
34
+ default or application-wide layout. Make sure you are only rendering
35
+ it if the user is logged in, otherwise the plugin will attempt to force
36
+ non-existent sessions to timeout, wreaking havoc:
37
+
38
+ <body>
39
+ <% if current_user %>
40
+ <%= auto_session_timeout_js %>
41
+ <% end %>
42
+ </body>
43
+
44
+ You need to setup two actions: one to return the session status and
45
+ another that runs when the session times out. You can use the default
46
+ actions included with the plugin by inserting this line in your target
47
+ controller (most likely your user or session controller):
48
+
49
+ class SessionsController < ApplicationController
50
+ auto_session_timeout_actions
51
+ end
52
+
53
+ To customize the default actions, simply override them. You can call
54
+ the render_session_status and render_session_timeout methods to use
55
+ the default implementation from the plugin, or you can define the
56
+ actions entirely with your own custom code:
57
+
58
+ class SessionsController < ApplicationController
59
+ def active
60
+ render_session_status
61
+ end
62
+
63
+ def timeout
64
+ render_session_timeout
65
+ end
66
+ end
67
+
68
+ In any of these cases, make sure to properly map the actions in
69
+ your routes.rb file:
70
+
71
+ match 'active' => 'sessions#active', via: :get
72
+ match 'timeout' => 'sessions#timeout', via: :get
73
+
74
+ You're done! Enjoy watching your sessions automatically timeout.
75
+
76
+ ## Additional Configuration
77
+
78
+ By default, the JavaScript code checks the server every 60 seconds for
79
+ active sessions. If you prefer that it check more frequently, pass a
80
+ frequency attribute to the helper method. The frequency is given in
81
+ seconds. The following example checks the server every 15 seconds:
82
+
83
+ <html>
84
+ <head>...</head>
85
+ <body>
86
+ <% if current_user %>
87
+ <%= auto_session_timeout_js frequency: 15 %>
88
+ <% end %>
89
+ ...
90
+ </body>
91
+ </html>
92
+
93
+ ## TODO
94
+
95
+ * current_user must be defined
96
+ * using Prototype vs. jQuery
97
+ * setting timeout in controller vs. user
98
+
99
+ ## Contributing
100
+
101
+ 1. Fork it
102
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
103
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
104
+ 4. Push to the branch (`git push origin my-new-feature`)
105
+ 5. Create new Pull Request
106
+
107
+ ## Resources
108
+
109
+ * Repository: http://github.com/pelargir/auto-session-timeout/
110
+ * Blog: http://www.matthewbass.com
111
+ * Author: Matthew Bass
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+ require 'bundler/gem_tasks'
5
+
6
+ task default: :test
7
+
8
+ Rake::TestTask.new('test') do |t|
9
+ t.pattern = 'test/*_test.rb'
10
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'auto/session/timeout/warning/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "auto-session-timeout-warning"
8
+ spec.version = Auto::Session::Timeout::Warning::VERSION
9
+ spec.authors = ["Matthew Bass","Krishna Srihari"]
10
+ spec.email = ["krishna.srihari@gmail.com"]
11
+ spec.description = %q{jQuery automatic session timeout warning in a Rails application.}
12
+ spec.summary = %q{jQuery automatic session timeout in a Rails application.}
13
+ spec.homepage = "https://github.com/krishnasrihari/jquery-auto-session-timeout"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest", "~> 4.2"
24
+ spec.add_development_dependency "actionpack", "~> 3.2"
25
+ end
@@ -0,0 +1,2 @@
1
+ require 'auto_session_timeout_warning'
2
+ require 'auto_session_timeout_warning_helper'
@@ -0,0 +1,11 @@
1
+ require "auto/session/timeout/warning/version"
2
+
3
+ module Auto
4
+ module Session
5
+ module Timeout
6
+ module Warning
7
+ # Your code goes here...
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module Auto
2
+ module Session
3
+ module Timeout
4
+ module Warning
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,40 @@
1
+ module AutoSessionTimeoutWarning
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=nil)
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.url_for(c.params).start_with?(c.send(:active_url))
15
+ offset = seconds || (current_user.respond_to?(:auto_timeout) ? current_user.auto_timeout : nil)
16
+ c.session[:auto_session_expires_at] = Time.now + offset if offset && offset > 0
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ def auto_session_timeout_actions
23
+ define_method(:active) { render_session_status }
24
+ define_method(:timeout) { render_session_timeout }
25
+ end
26
+ end
27
+
28
+ def render_session_status
29
+ response.headers["Etag"] = "" # clear etags to prevent caching
30
+ render text: !!current_user, status: 200
31
+ end
32
+
33
+ def render_session_timeout
34
+ flash[:notice] = "Your session has timed out."
35
+ redirect_to "/login"
36
+ end
37
+
38
+ end
39
+
40
+ ActionController::Base.send :include, AutoSessionTimeoutWarning
@@ -0,0 +1,66 @@
1
+ module AutoSessionTimeoutWarningHelper
2
+ def auto_session_timeout_js(options={})
3
+ frequency = options[:frequency] || 60
4
+ timeout = options[:timeout] || 60
5
+ start = options[:start] || 60
6
+ warning = options[:warning] || 20
7
+ code = <<JS
8
+ if(typeof(jQuery) != 'undefined'){
9
+ $("#logout_dialog").dialog({
10
+ modal: true,
11
+ bgiframe: true,
12
+ width: 500,
13
+ height: 180,
14
+ autoOpen: false,
15
+ dialogClass: "no-close"
16
+ });
17
+
18
+ $(".logout_dialog").click(function (e) {
19
+ e.preventDefault();
20
+
21
+ $("#logout_dialog").dialog('option', 'buttons', {
22
+ "Continue": function () {
23
+ window.location.reload();
24
+ }
25
+ });
26
+
27
+ $("#logout_dialog").dialog("open");
28
+
29
+ });
30
+
31
+ function PeriodicalQuery() {
32
+ $.ajax({
33
+ url: '/active',
34
+ success: function(data) {
35
+ if(data == 'false'){
36
+ showDialog();
37
+ setTimeout(doTimeout, (#{warning} * 1000));
38
+ }
39
+ }
40
+ });
41
+ setTimeout(PeriodicalQuery, (#{frequency} * 1000));
42
+ }
43
+ setTimeout(PeriodicalQuery, (#{start} * 1000));
44
+
45
+ function showDialog(){
46
+ $('.logout_dialog').trigger('click');
47
+ }
48
+
49
+ function doTimeout(){
50
+ window.location.href = '/timeout';
51
+ }
52
+ }
53
+ JS
54
+ javascript_tag(code)
55
+ end
56
+
57
+ def auto_session_warning_tag(options={})
58
+ "<div id='logout_dialog' title='Logout Message' style='display:none;'>
59
+ You are about to be logged out of this system.
60
+ <br/><br/>
61
+ Please click Continue if you want to stay logged in.
62
+ </div> <div class='logout_dialog'></div>".html_safe
63
+ end
64
+ end
65
+
66
+ ActionView::Base.send :include, AutoSessionTimeoutWarningHelper
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ describe AutoSessionTimeoutWarningHelper do
4
+
5
+ subject { Class.new(ActionView::Base).new }
6
+
7
+ describe "#auto_session_timeout_js" do
8
+ it "returns correct JS" do
9
+ assert_equal "<script type=\"text/javascript\">
10
+ //<![CDATA[
11
+ if (typeof(Ajax) != 'undefined') {
12
+ new Ajax.PeriodicalUpdater('', '/active', {frequency:60, method:'get', onSuccess: function(e) {
13
+ if (e.responseText == 'false') window.location.href = '/timeout';
14
+ }});
15
+ } else {
16
+ $.PeriodicalUpdater('/active', {minTimeout:60000, multiplier:0, method:'get', verbose:2}, function(remoteData, success) {
17
+ if (success == 'success' && remoteData == 'false')
18
+ window.location.href = '/timeout';
19
+ });
20
+ }
21
+
22
+ //]]>
23
+ </script>", subject.auto_session_timeout_js
24
+ end
25
+
26
+ it "uses custom frequency when given" do
27
+ assert_match /frequency:120/, subject.auto_session_timeout_js(frequency: 120)
28
+ end
29
+
30
+ it "uses 60 when custom frequency is nil" do
31
+ assert_match /frequency:60/, subject.auto_session_timeout_js(frequency: nil)
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ describe AutoSessionTimeoutWarning do
4
+
5
+ it "tests something" do
6
+ assert true
7
+ end
8
+
9
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require 'action_controller'
4
+
5
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/auto-session-timeout-warning')
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto-session-timeout-warning
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matthew Bass
9
+ - Krishna Srihari
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-08-28 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.3'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '4.2'
63
+ - !ruby/object:Gem::Dependency
64
+ name: actionpack
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: '3.2'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: '3.2'
79
+ description: jQuery automatic session timeout warning in a Rails application.
80
+ email:
81
+ - krishna.srihari@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .project
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - auto-session-timeout-warning.gemspec
92
+ - lib/auto-session-timeout-warning.rb
93
+ - lib/auto/session/timeout/warning.rb
94
+ - lib/auto/session/timeout/warning/version.rb
95
+ - lib/auto_session_timeout_warning.rb
96
+ - lib/auto_session_timeout_warning_helper.rb
97
+ - test/auto_session_timeout_warning_helper_test.rb
98
+ - test/auto_session_timeout_warning_test.rb
99
+ - test/test_helper.rb
100
+ homepage: https://github.com/krishnasrihari/jquery-auto-session-timeout
101
+ licenses:
102
+ - MIT
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.24
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: jQuery automatic session timeout in a Rails application.
125
+ test_files:
126
+ - test/auto_session_timeout_warning_helper_test.rb
127
+ - test/auto_session_timeout_warning_test.rb
128
+ - test/test_helper.rb