Class: EffectiveLogging::UserLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/effective_logging/user_logger.rb

Class Method Summary collapse

Class Method Details

.create_warden_hooksObject


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
# File 'lib/effective_logging/user_logger.rb', line 3

def self.create_warden_hooks
  Warden::Manager.after_authentication do |user, warden, opts|
    if EffectiveLogging. && !EffectiveLogging.supressed?
      ::EffectiveLogger.('Sign in', user: user, associated: user, request: warden.request)
    end
  end

  Warden::Manager.after_set_user do |user, warden, opts|
    if EffectiveLogging. && !EffectiveLogging.supressed?
      if opts && opts[:event] == :set_user && !opts.key?(:store) # User has just reset their password and signed in
        ::EffectiveLogger.('Sign in', user: user, associated: user, request: warden.request, notes: 'after password reset')
      end
    end
  end

  Warden::Manager.before_logout do |user, warden, opts|
    if EffectiveLogging.sign_out_enabled && !EffectiveLogging.supressed?
      if user.respond_to?(:timedout?) && user.respond_to?(:timeout_in)
        scope = opts[:scope]
        last_request_at = (warden.request.session["warden.#{scope}.#{scope}.session"]['last_request_at'] rescue Time.zone.now)

        # As per Devise
        if last_request_at.is_a? Integer
          last_request_at = Time.at(last_request_at).utc
        elsif last_request_at.is_a? String
          last_request_at = Time.parse(last_request_at)
        end

        if user.timedout?(last_request_at) && !warden.request.env['devise.skip_timeout']
          ::EffectiveLogger.sign_out('Sign out', user: user, associated: user, timedout: true)
        else
          ::EffectiveLogger.sign_out('Sign out', user: user, associated: user)
        end
      else # User does not respond to timedout
        ::EffectiveLogger.sign_out('Sign out', user: user, associated: user)
      end
    end
  end

end