devise-stalkable 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +8 -0
- data/devise-stalkable.gemspec +27 -0
- data/lib/devise/hooks/stalkable.rb +26 -0
- data/lib/devise/models/stalkable.rb +55 -0
- data/lib/devise/stalkable/rails.rb +6 -0
- data/lib/devise/stalkable/version.rb +5 -0
- data/lib/devise/stalkable.rb +7 -0
- data/lib/generators/devise_stalkable/devise_stalkable_generator.rb +60 -0
- data/lib/generators/devise_stalkable/templates/migration.rb +20 -0
- data/spec/devise-stalkable/models_spec.rb +58 -0
- data/spec/spec_helper.rb +7 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a3bba72a13bc3bd2d7d6cbb871ca4bfb684d5b8f
|
4
|
+
data.tar.gz: 955be64c8caa5d8413035c4a696a19888f8e3476
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d06806b40ae774d9d51be70d25b82c9c4e29c11d95cc8c281eaf99046cf4889cba320cfb30fecaeeffa28015ddb4096faa1897933d47adae89c318067d76d759
|
7
|
+
data.tar.gz: c230d1398e6dc3ad42471548115777ff5d3260c2e749e13173ce1471b74268b1d492eeb4b269d3c97ce0e6b57bf46894cc7fd04b28b8e509353b2bfb88e8f962
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Chris Cameron
|
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,54 @@
|
|
1
|
+
# Devise::Stalkable
|
2
|
+
|
3
|
+
Stalkable is a gem that tracks logins of each user. Based on [devise-login_tracker](https://github.com/blueberryapps/devise-login_tracker)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile (not on rubygems yet!):
|
8
|
+
|
9
|
+
gem 'devise-stalkable', github: "unchris/stalkable"
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Run the generator with the model name (User in this example):
|
18
|
+
|
19
|
+
$ rails g devise_stalkable User
|
20
|
+
|
21
|
+
Add `:stalkable` to `devise` in your model and association
|
22
|
+
to the login records. Example for User model:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class User < ActiveRecord::Base
|
26
|
+
devise :database_authenticatable, ..... , :stalkable
|
27
|
+
|
28
|
+
has_many :logins, class_name: 'UserLogin'
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
## What is being tracked
|
33
|
+
|
34
|
+
For each login new record is created with following attributes:
|
35
|
+
|
36
|
+
* `ip_address` - IP address
|
37
|
+
* `user_agent` - User agent
|
38
|
+
* `signed_in_at` - Signed in at
|
39
|
+
* `last_seen_at` - Last seen at (on new requests)
|
40
|
+
* `signed_in_at` - Signed out at (upon sign out)
|
41
|
+
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
50
|
+
|
51
|
+
## Copyright
|
52
|
+
|
53
|
+
Copyright © 2014 Chris Cameron. See LICENSE for details.
|
54
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'devise/stalkable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "devise-stalkable"
|
8
|
+
spec.version = Devise::Stalkable::VERSION
|
9
|
+
spec.authors = ["Chris Cameron"]
|
10
|
+
spec.email = ["unchris@outlook.com"]
|
11
|
+
spec.description = %q{Tracks a user's login, logout, and requests}
|
12
|
+
spec.summary = %q{Tracks a user's login, logout, and requests}
|
13
|
+
spec.homepage = ""
|
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 'rspec'
|
24
|
+
spec.add_runtime_dependency 'devise', '>= 3.0.0'
|
25
|
+
spec.add_runtime_dependency 'rails', '>= 3.1.0'
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'warden'
|
2
|
+
|
3
|
+
Warden::Manager.after_authentication do |record, warden, opts|
|
4
|
+
if record.respond_to?(:mark_login!)
|
5
|
+
login_record = record.mark_login!(warden.request)
|
6
|
+
scope = opts[:scope]
|
7
|
+
warden.session["#{scope}.login_id"] = login_record.id
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
Warden::Manager.after_set_user do |record, warden, opts|
|
12
|
+
if record.respond_to?(:mark_last_seen!)
|
13
|
+
scope = opts[:scope]
|
14
|
+
login_record_id = warden.session["#{scope}.login_id"]
|
15
|
+
record.mark_last_seen!(login_record_id) if login_record_id
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Warden::Manager.before_logout do |record, warden, opts|
|
20
|
+
if record.respond_to?(:mark_logout!)
|
21
|
+
scope = opts[:scope]
|
22
|
+
login_record_id = warden.session["#{scope}.login_id"]
|
23
|
+
record.mark_logout!(login_record_id) if login_record_id
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Models
|
5
|
+
module Stalkable
|
6
|
+
|
7
|
+
# Creates a new login record based on information in provided request
|
8
|
+
# object.
|
9
|
+
# Marks the last seen time as well.
|
10
|
+
#
|
11
|
+
# @param [ActionDispatch::Request] request HTTP request which was used
|
12
|
+
# to login
|
13
|
+
# @return [UserLogin]
|
14
|
+
def mark_login!(request)
|
15
|
+
login_class.create(attrs_for_login(request))
|
16
|
+
end
|
17
|
+
|
18
|
+
# Marks the time when the user was last seen in the system on a given
|
19
|
+
# login record ID.
|
20
|
+
#
|
21
|
+
# @param [Fixnum, String] login_record_id ID of login record.
|
22
|
+
def mark_last_seen!(login_record_id)
|
23
|
+
login_record = login_class.find(login_record_id)
|
24
|
+
login_record.update_column :last_seen_at, Time.now
|
25
|
+
end
|
26
|
+
|
27
|
+
# Marks the time when user has logged out on given login record ID.
|
28
|
+
# Marks the last seen time as well.
|
29
|
+
#
|
30
|
+
# @param [Fixnum, String] login_record_id ID of login record.
|
31
|
+
def mark_logout!(login_record_id)
|
32
|
+
login_record = login_class.find(login_record_id)
|
33
|
+
t = Time.now
|
34
|
+
login_record.update_column :last_seen_at, t
|
35
|
+
login_record.update_column :signed_out_at, t
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
def attrs_for_login(request)
|
41
|
+
t = Time.now
|
42
|
+
{ user_id: id, ip_address: request.remote_ip,
|
43
|
+
user_agent: request.user_agent,
|
44
|
+
signed_in_at: t,
|
45
|
+
last_seen_at: t }
|
46
|
+
end
|
47
|
+
|
48
|
+
def login_class
|
49
|
+
"#{self.class.name}Login".safe_constantize
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
class DeviseStalkableGenerator < Rails::Generators::NamedBase
|
4
|
+
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
desc "Generates a model with the given NAME (if one does not exist) with" <<
|
8
|
+
"devise configuration plus a migration file and devise routes."
|
9
|
+
|
10
|
+
def self.source_root
|
11
|
+
@_devise_source_root ||= File.expand_path('../templates', __FILE__)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.orm_has_migration?
|
15
|
+
Rails::Generators.options[:rails][:orm] == :active_record
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.next_migration_number(dirname)
|
19
|
+
if ActiveRecord::Base.timestamped_migrations
|
20
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
21
|
+
else
|
22
|
+
'%.3d' % (current_migration_number(dirname) + 1)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class_option :orm
|
27
|
+
class_option :migration, :type => :boolean, :default => orm_has_migration?
|
28
|
+
|
29
|
+
def invoke_orm_model
|
30
|
+
if model_exists?
|
31
|
+
say "* Model #{name}Login already exists."
|
32
|
+
elsif options[:orm].present?
|
33
|
+
invoke "model", ["#{name}Login", "#{name}:belongs_to"], :migration => false,
|
34
|
+
:orm => options[:orm]
|
35
|
+
|
36
|
+
unless model_exists?
|
37
|
+
abort "Tried to invoke the model generator for '#{options[:orm]}' but could not find it.\n" <<
|
38
|
+
"Please create your model by hand before calling `rails g devise_stalkable #{name}`."
|
39
|
+
end
|
40
|
+
else
|
41
|
+
abort "Cannot create a devise model because config.generators.orm is blank.\n" <<
|
42
|
+
"Please create your model by hand or configure your generators orm before calling `rails g devise_stalkable #{name}`."
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_migration_file
|
47
|
+
migration_template 'migration.rb', "db/migrate/devise_create_#{name.downcase}_logins.rb"
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
def model_exists?
|
53
|
+
File.exists?(File.join(destination_root, model_path))
|
54
|
+
end
|
55
|
+
|
56
|
+
def model_path
|
57
|
+
@model_path ||= File.join('app', 'models', "#{file_path}_login.rb")
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class DeviseCreate<%= table_name.camelize.singularize %>Logins < ActiveRecord::Migration
|
2
|
+
|
3
|
+
def up
|
4
|
+
create_table :<%= table_name.singularize %>_logins do |t|
|
5
|
+
t.integer :<%= table_name.classify.foreign_key %>
|
6
|
+
t.string :ip_address
|
7
|
+
t.string :user_agent
|
8
|
+
t.datetime :signed_in_at
|
9
|
+
t.datetime :last_seen_at
|
10
|
+
t.datetime :signed_out_at
|
11
|
+
end
|
12
|
+
|
13
|
+
add_index :<%= table_name.singularize %>_logins, :<%= table_name.classify.foreign_key %>
|
14
|
+
end
|
15
|
+
|
16
|
+
def down
|
17
|
+
drop_table :<%= table_name.singularize %>_logins
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestModel
|
4
|
+
include Devise::Models::Stalkable
|
5
|
+
|
6
|
+
def id
|
7
|
+
1
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class TestModelLogin
|
12
|
+
end
|
13
|
+
|
14
|
+
describe Devise::Models::Stalkable do
|
15
|
+
|
16
|
+
let(:model) { TestModel.new }
|
17
|
+
|
18
|
+
describe '#mark_login!' do
|
19
|
+
|
20
|
+
it 'creates a new login record' do
|
21
|
+
expect(TestModelLogin).to receive(:create)
|
22
|
+
request = double.as_null_object
|
23
|
+
model.mark_login!(request)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'uses model id' do
|
27
|
+
expect(TestModelLogin).to receive(:create).with(hash_including(user_id: 1))
|
28
|
+
request = double.as_null_object
|
29
|
+
model.mark_login!(request)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#mark_logout!' do
|
35
|
+
|
36
|
+
let(:record) { double update_column: nil }
|
37
|
+
|
38
|
+
before do
|
39
|
+
allow(TestModelLogin).to receive(:find).and_return record
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'finds the record by given ID' do
|
43
|
+
expect(TestModelLogin).to receive(:find).with(1)
|
44
|
+
model.mark_logout!(1)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'finds the record by given ID' do
|
48
|
+
now = double
|
49
|
+
allow(Time).to receive(:now).and_return now
|
50
|
+
expect(record).to receive(:update_column).with(:signed_out_at, now)
|
51
|
+
expect(record).to receive(:update_column).with(:last_seen_at, now)
|
52
|
+
model.mark_logout!(1)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: devise-stalkable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Cameron
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
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: devise
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.1.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.1.0
|
83
|
+
description: Tracks a user's login, logout, and requests
|
84
|
+
email:
|
85
|
+
- unchris@outlook.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- devise-stalkable.gemspec
|
96
|
+
- lib/devise/hooks/stalkable.rb
|
97
|
+
- lib/devise/models/stalkable.rb
|
98
|
+
- lib/devise/stalkable.rb
|
99
|
+
- lib/devise/stalkable/rails.rb
|
100
|
+
- lib/devise/stalkable/version.rb
|
101
|
+
- lib/generators/devise_stalkable/devise_stalkable_generator.rb
|
102
|
+
- lib/generators/devise_stalkable/templates/migration.rb
|
103
|
+
- spec/devise-stalkable/models_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: ''
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.2.2
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Tracks a user's login, logout, and requests
|
129
|
+
test_files:
|
130
|
+
- spec/devise-stalkable/models_spec.rb
|
131
|
+
- spec/spec_helper.rb
|