-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
activity_logging.rb
89 lines (75 loc) · 3.79 KB
/
activity_logging.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module Sorcery
module Model
module Submodules
# This submodule keeps track of events such as login, logout, and last activity time, per user.
# It helps in estimating which users are active now in the site.
# This cannot be determined absolutely because a user might be reading a page without clicking anything
# for a while.
# This is the model part of the submodule, which provides configuration options.
module ActivityLogging
def self.included(base)
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
base.sorcery_config.class_eval do
# last login attribute name.
attr_accessor :last_login_at_attribute_name
# last logout attribute name.
attr_accessor :last_logout_at_attribute_name
# last activity attribute name.
attr_accessor :last_activity_at_attribute_name
# last activity login source
attr_accessor :last_login_from_ip_address_name
# how long since last activity is the user defined offline
attr_accessor :activity_timeout
end
base.sorcery_config.instance_eval do
@defaults.merge!(:@last_login_at_attribute_name => :last_login_at,
:@last_logout_at_attribute_name => :last_logout_at,
:@last_activity_at_attribute_name => :last_activity_at,
:@last_login_from_ip_address_name => :last_login_from_ip_address,
:@activity_timeout => 10 * 60)
reset!
end
base.sorcery_config.after_config << :define_activity_logging_fields
end
module InstanceMethods
def set_last_login_at(time)
sorcery_adapter.update_attribute(sorcery_config.last_login_at_attribute_name, time)
end
def set_last_logout_at(time)
sorcery_adapter.update_attribute(sorcery_config.last_logout_at_attribute_name, time)
end
def set_last_activity_at(time)
sorcery_adapter.update_attribute(sorcery_config.last_activity_at_attribute_name, time)
end
def set_last_ip_address(ip_address)
sorcery_adapter.update_attribute(sorcery_config.last_login_from_ip_address_name, ip_address)
end
# online method shows if user is active (logout action makes user inactive too)
def online?
return false if send(sorcery_config.last_activity_at_attribute_name).nil?
logged_in? && send(sorcery_config.last_activity_at_attribute_name) > sorcery_config.activity_timeout.seconds.ago
end
# shows if user is logged in, but it not show if user is online - see online?
def logged_in?
return false if send(sorcery_config.last_login_at_attribute_name).nil?
return true if send(sorcery_config.last_login_at_attribute_name).present? && send(sorcery_config.last_logout_at_attribute_name).nil?
send(sorcery_config.last_login_at_attribute_name) > send(sorcery_config.last_logout_at_attribute_name)
end
def logged_out?
!logged_in?
end
end
module ClassMethods
protected
def define_activity_logging_fields
sorcery_adapter.define_field sorcery_config.last_login_at_attribute_name, Time
sorcery_adapter.define_field sorcery_config.last_logout_at_attribute_name, Time
sorcery_adapter.define_field sorcery_config.last_activity_at_attribute_name, Time
sorcery_adapter.define_field sorcery_config.last_login_from_ip_address_name, String
end
end
end
end
end
end