Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some modifications to the devise login tracker gem #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
*.swp

4 changes: 2 additions & 2 deletions lib/devise/hooks/login_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
if record.respond_to?(:mark_login!)
login_record = record.mark_login!(warden.request)
scope = opts[:scope]
warden.session["#{scope}.login_id"] = login_record.id
warden.session(scope)['login_id'] = login_record.id
end
end

Warden::Manager.before_logout do |record, warden, opts|
if record.respond_to?(:mark_logout!)
scope = opts[:scope]
login_record_id = warden.session["#{scope}.login_id"]
login_record_id = warden.session(scope)['login_id']
record.mark_logout!(login_record_id) if login_record_id
end
end
Expand Down
18 changes: 5 additions & 13 deletions lib/devise/models/login_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,20 @@ module LoginTracker
# to login
# @return [UserLogin]
def mark_login!(request)
login_class.create(attrs_for_login(request))
logins.create( ip_address: request.remote_ip,
user_agent: request.user_agent,
signed_in_at: Time.now
)
end

# Marks the time when user has logged out on given login record ID.
#
# @param [Fixnum, String] login_record_id ID of login record.
def mark_logout!(login_record_id)
login_record = login_class.find(login_record_id)
login_record = logins.find(login_record_id)
login_record.update_column :signed_out_at, Time.now
end

protected

def attrs_for_login(request)
{ user_id: id, ip_address: request.remote_ip,
user_agent: request.user_agent,
signed_in_at: Time.now }
end

def login_class
"#{self.class.name}Login".safe_constantize
end

end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/devise_login_tracker/templates/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class DeviseCreate<%= table_name.camelize.singularize %>Logins < ActiveRecord::M
def up
create_table :<%= table_name.singularize %>_logins do |t|
t.integer :<%= table_name.classify.foreign_key %>
t.string :ip_address
t.string :user_agent
t.inet :ip_address
t.string :user_agent
t.datetime :signed_in_at
t.datetime :signed_out_at
end
Expand Down