Module: AuthTrail

Defined in:
lib/authtrail.rb,
lib/auth_trail/engine.rb,
lib/auth_trail/manager.rb,
lib/auth_trail/version.rb,
app/jobs/auth_trail/geocode_job.rb

Defined Under Namespace

Modules: Manager Classes: Engine, GeocodeJob

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.exclude_methodObject

Returns the value of attribute exclude_method.



12
13
14
# File 'lib/authtrail.rb', line 12

def exclude_method
  @exclude_method
end

.geocodeObject

Returns the value of attribute geocode.



12
13
14
# File 'lib/authtrail.rb', line 12

def geocode
  @geocode
end

.identity_methodObject

Returns the value of attribute identity_method.



12
13
14
# File 'lib/authtrail.rb', line 12

def identity_method
  @identity_method
end

.track_methodObject

Returns the value of attribute track_method.



12
13
14
# File 'lib/authtrail.rb', line 12

def track_method
  @track_method
end

Class Method Details

.safely(default: nil) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/authtrail.rb', line 54

def self.safely(default: nil)
  begin
    yield
  rescue => e
    warn "[authtrail] #{e.class.name}: #{e.message}"
    default
  end
end

.track(strategy:, scope:, identity:, success:, request:, user: nil, failure_reason: nil) ⇒ Object



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
# File 'lib/authtrail.rb', line 24

def self.track(strategy:, scope:, identity:, success:, request:, user: nil, failure_reason: nil)
  info = {
    strategy: strategy,
    scope: scope,
    identity: identity,
    success: success,
    failure_reason: failure_reason,
    user: user,
    ip: request.remote_ip,
    user_agent: request.user_agent,
    referrer: request.referrer
  }

  if request.params[:controller]
    info[:context] = "#{request.params[:controller]}##{request.params[:action]}"
  end

  # if exclude_method throws an exception, default to not excluding
  exclude = AuthTrail.exclude_method && AuthTrail.safely(default: false) { AuthTrail.exclude_method.call(info) }

  unless exclude
    if AuthTrail.track_method
      AuthTrail.track_method.call(info)
    else
       = LoginActivity.create!(info)
      AuthTrail::GeocodeJob.perform_later() if AuthTrail.geocode
    end
  end
end