devise-automaticlogout 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +48 -0
- data/Rakefile +27 -0
- data/app/views/automatic_logout/_timer.html.erb +4 -0
- data/config/routes.rb +4 -0
- data/lib/assets/javascripts/automatic_logout.js +151 -0
- data/lib/devise/automaticlogout.rb +9 -0
- data/lib/devise/automaticlogout/controllers.rb +37 -0
- data/lib/devise/automaticlogout/engine.rb +15 -0
- data/lib/devise/automaticlogout/helpers.rb +9 -0
- data/lib/devise/automaticlogout/version.rb +5 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 60c16b638b9295ea0c311d1017b1f87a92b77637
|
4
|
+
data.tar.gz: 3b6bbc12b9e7f595f1af9241bc71c335f37e2e9c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3731202a8922acc897e944296e9fa90ab21fdf927f29b7618bdbc939fe7a2822bdc66394e57a0f852ceb1d8a296ce3a164a874ae73d1f43e12b8865a7767f890
|
7
|
+
data.tar.gz: 0e0ef041a0dbe58c85015c9a318640db805f52c69a6713ea89c78524bd6066f707241027286d761ff86b0629a565219c74390590340d9c29b2a4e8ab8df9d795
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Thadeu Esteves Jr
|
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.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Devise Automatic Logout
|
2
|
+
By Thadeu Esteves Jr.
|
3
|
+
|
4
|
+
Provides automatic session timeout in a Rails application. Very easy to install and configure. Have you ever wanted to force your users off your app if they go idle for a certain period of time? Many online banking sites use this technique. If your app is used on any kind of public computer system, this plugin is a necessity.
|
5
|
+
|
6
|
+
* Force your users power off session
|
7
|
+
* show for they the regressive time
|
8
|
+
|
9
|
+
## Getting started
|
10
|
+
|
11
|
+
You can add it to your Gemfile with:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'devise-automaticlogout'
|
15
|
+
```
|
16
|
+
|
17
|
+
Then run bundle install
|
18
|
+
|
19
|
+
## How to use?
|
20
|
+
|
21
|
+
## Configure Timeoutable in /config/initializers/devise.rb
|
22
|
+
|
23
|
+
Besides :stretches, you can define :pepper, :encryptor, :confirm_within, :remember_for, :timeout_in, :unlock_in among other options. For more details, see the initializer file that was created when you invoked the "devise:install" generator described above. This file is usually located at /config/initializers/devise.rb.
|
24
|
+
|
25
|
+
[Configure time](https://github.com/plataformatec/devise#configuring-models)
|
26
|
+
|
27
|
+
[More informations](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Timeoutable)
|
28
|
+
|
29
|
+
### Configure Javascript
|
30
|
+
Add in your application.js this require
|
31
|
+
|
32
|
+
```javascript
|
33
|
+
//= require automatic_logout
|
34
|
+
```
|
35
|
+
### Configure View (OPTIONAL)
|
36
|
+
|
37
|
+
Use Helper in your view, for show regressive timer. Add this in your file application.html.erb
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
<%= regressive_timer %>
|
41
|
+
```
|
42
|
+
|
43
|
+
## TODO
|
44
|
+
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
We have a long list of valued contributors. Check them all at: https://github.com/Thadeu/devise-automaticlogout.
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Devise::Automaticlogout'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
require 'rake/testtask'
|
19
|
+
|
20
|
+
Rake::TestTask.new(:test) do |t|
|
21
|
+
t.libs << 'lib'
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
data/config/routes.rb
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
/**
|
2
|
+
* Module AutomaticLogout for JS Files
|
3
|
+
* @type {[type]}
|
4
|
+
*/
|
5
|
+
var AutomaticLogout = AutomaticLogout || {};
|
6
|
+
|
7
|
+
$(document).ready(function(){
|
8
|
+
// Change to $() for document.querySelector(".regressive-timer")
|
9
|
+
var $regressiveTimer = $('.regressive-timer');
|
10
|
+
|
11
|
+
($regressiveTimer.length > 0 &&
|
12
|
+
$regressiveTimer.data('expires-at') !== '')
|
13
|
+
? AutomaticLogout.regressiveTimer()
|
14
|
+
: AutomaticLogout.ajaxSessionTimeout();
|
15
|
+
});
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Send request for destroy session current_user
|
19
|
+
* @return {[type]} [description]
|
20
|
+
*/
|
21
|
+
AutomaticLogout.destroySession = function(){
|
22
|
+
return window.location.href = '/devise/automatic_logout/destroy';
|
23
|
+
}
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Method regressive time based in session auto expired at.
|
27
|
+
* @param {[type]} date_session [description]
|
28
|
+
* @return {[type]} [description]
|
29
|
+
*/
|
30
|
+
AutomaticLogout.regressiveTimer = function(){;
|
31
|
+
/**
|
32
|
+
* Variables used
|
33
|
+
* @param {[type]} '.regressive-timer' [description]
|
34
|
+
* @return {[type]} [description]
|
35
|
+
*/
|
36
|
+
var data_message = $('.regressive-timer').data('message'),
|
37
|
+
data_seconds = $('.regressive-timer').data('seconds'),
|
38
|
+
current_time = new Date().getTime();
|
39
|
+
|
40
|
+
var time_expired = new Date();
|
41
|
+
time_expired.setSeconds(time_expired.getSeconds() + data_seconds);
|
42
|
+
|
43
|
+
if (data_seconds != 0 && data_seconds != ''){
|
44
|
+
var timerDecrement = setInterval(function(){
|
45
|
+
|
46
|
+
if (data_seconds == 0) {
|
47
|
+
clearInterval(timerDecrement);
|
48
|
+
|
49
|
+
// limpa a sessão após o ok
|
50
|
+
if (confirm(data_message)) {
|
51
|
+
AutomaticLogout.destroySession();
|
52
|
+
};
|
53
|
+
}else{
|
54
|
+
//tempo descrecente
|
55
|
+
time_expired.setSeconds(time_expired.getSeconds() - 1);
|
56
|
+
|
57
|
+
var
|
58
|
+
// converte pra segundos
|
59
|
+
seconds_integer = (time_expired.getTime() - current_time) / 1000,
|
60
|
+
//faz o parse
|
61
|
+
date_format = AutomaticLogout.parseDate(seconds_integer);
|
62
|
+
|
63
|
+
// atualiza o DOM
|
64
|
+
$('.regressive-timer').text(date_format.hours + ':' + date_format.minutes + ':' + date_format.seconds);
|
65
|
+
}
|
66
|
+
|
67
|
+
data_seconds -= 1;
|
68
|
+
}, 1000)
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
/**
|
73
|
+
* Parse Date
|
74
|
+
* @param float seconds seconds_float [description]
|
75
|
+
* @return {
|
76
|
+
* days: days,
|
77
|
+
* hours: hours,
|
78
|
+
* minutes: minutes,
|
79
|
+
* seconds: seconds
|
80
|
+
* }
|
81
|
+
*/
|
82
|
+
AutomaticLogout.parseDate = function(seconds_float){
|
83
|
+
|
84
|
+
var days,
|
85
|
+
hours,
|
86
|
+
minutes,
|
87
|
+
seconds;
|
88
|
+
|
89
|
+
days = parseInt(seconds_float / 86400);
|
90
|
+
seconds_float = seconds_float % 86400;
|
91
|
+
|
92
|
+
hours = parseInt(seconds_float / 3600);
|
93
|
+
seconds_float = seconds_float % 3600;
|
94
|
+
|
95
|
+
minutes = parseInt(seconds_float / 60);
|
96
|
+
seconds = parseInt(seconds_float % 60);
|
97
|
+
|
98
|
+
if(hours < 10){
|
99
|
+
hours = "0"+hours;
|
100
|
+
hours = hours.substr(0, 2);
|
101
|
+
}
|
102
|
+
|
103
|
+
if(minutes < 10){
|
104
|
+
minutes = "0"+minutes;
|
105
|
+
minutes = minutes.substr(0, 2);
|
106
|
+
}
|
107
|
+
|
108
|
+
if(seconds <=9){
|
109
|
+
seconds = "0"+seconds;
|
110
|
+
}
|
111
|
+
|
112
|
+
return {
|
113
|
+
days: days,
|
114
|
+
hours: hours,
|
115
|
+
minutes: minutes,
|
116
|
+
seconds: seconds
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
/**
|
121
|
+
* Method for request ajax, check value e logout
|
122
|
+
* @return {[type]} [description]
|
123
|
+
*/
|
124
|
+
AutomaticLogout.ajaxSessionTimeout = function(){
|
125
|
+
setTimeout(function () {
|
126
|
+
//TODO: Change to ES6/7 fetch() feature
|
127
|
+
$.ajax({
|
128
|
+
url: '/devise/automatic_logout/status',
|
129
|
+
success: function(data) {
|
130
|
+
if (data.seconds !== "null" && data.live === true){
|
131
|
+
|
132
|
+
var data_message = data.message, data_seconds = data.seconds;
|
133
|
+
|
134
|
+
if (data_seconds != 0 && data_seconds != ''){
|
135
|
+
var timerDecrement = setInterval(function(){
|
136
|
+
if (data_seconds == 0) {
|
137
|
+
clearInterval(timerDecrement);
|
138
|
+
// limpa a sessão após o ok do alertify
|
139
|
+
if (confirm(data_message)) {
|
140
|
+
AutomaticLogout.destroySession();
|
141
|
+
}
|
142
|
+
}
|
143
|
+
|
144
|
+
data_seconds -= 1;
|
145
|
+
}, 1000)
|
146
|
+
}
|
147
|
+
}
|
148
|
+
}
|
149
|
+
});
|
150
|
+
}, 1000); //1s
|
151
|
+
};
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Devise
|
2
|
+
module AutomaticLogout
|
3
|
+
module Controllers
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
::ApplicationController.before_action do |c|
|
8
|
+
if current_user.present?
|
9
|
+
key_classify = Devise.mappings.keys.first
|
10
|
+
resource_name = Devise.mappings[key_classify].class_name.to_s
|
11
|
+
# get instance for class using devise
|
12
|
+
c.session[:seconds] = resource_name.classify.constantize.timeout_in.seconds.to_i
|
13
|
+
# I18n.t devise.automaticlogout.message
|
14
|
+
c.session[:message] = I18n.t 'devise.automaticlogout.message' || 'Session expired!'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
::ApplicationController.send :define_method, :automatic_logout_status do
|
19
|
+
Rails.logger.info 'Sessão atualizada'
|
20
|
+
render json: {
|
21
|
+
live: !current_user.nil?,
|
22
|
+
message: session[:message],
|
23
|
+
seconds: session[:seconds]
|
24
|
+
}, status: :ok
|
25
|
+
end
|
26
|
+
|
27
|
+
::ApplicationController.send :define_method, :automatic_logout_destroy do
|
28
|
+
Rails.logger.info 'Sessão destruída'
|
29
|
+
session[:seconds] = ''
|
30
|
+
session[:live] = !current_user.nil?
|
31
|
+
reset_session
|
32
|
+
redirect_to '/', notice: session[:message]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Devise
|
2
|
+
module AutomaticLogout
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
require 'jquery-rails'
|
5
|
+
|
6
|
+
ActiveSupport.on_load :action_controller do
|
7
|
+
include Devise::AutomaticLogout::Controllers
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveSupport.on_load :action_view do
|
11
|
+
include Devise::AutomaticLogout::Helpers
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: devise-automaticlogout
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thadeu Esteves Jr
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: devise
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: jquery-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Provides automatic session timeout in a Rails Devise application.
|
84
|
+
email:
|
85
|
+
- tadeuu@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- MIT-LICENSE
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
93
|
+
- app/views/automatic_logout/_timer.html.erb
|
94
|
+
- config/routes.rb
|
95
|
+
- lib/assets/javascripts/automatic_logout.js
|
96
|
+
- lib/devise/automaticlogout.rb
|
97
|
+
- lib/devise/automaticlogout/controllers.rb
|
98
|
+
- lib/devise/automaticlogout/engine.rb
|
99
|
+
- lib/devise/automaticlogout/helpers.rb
|
100
|
+
- lib/devise/automaticlogout/version.rb
|
101
|
+
homepage: https://github.com/Thadeu/devise-automaticlogout
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.5.1
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Provides automatic session timeout in a Rails Devise application.
|
125
|
+
test_files: []
|