Sha256: c01df0a77a74187f48753e80b07946109edef8d33841ccd54df727a2efe44129

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

module Incline

  ##
  # Defines an auth engine.
  class AuthEngineBase

    ##
    # The auth engine initializer should take a hash of parameters.
    def initialize(options = {})
      @options = options || {}
    end

    ##
    # The authenticate method takes an email and password to authenticate a user and the client IP for logging purposes.
    def authenticate(email, password, client_ip)
      nil
    end

    protected

    ##
    # Logs a failure message for a user.  The user can either be an Incline::User model or the email address used
    # in the attempt.
    def add_failure_to(user, message, client_ip) # :doc:
      Incline::Log::info "LOGIN(#{user}) FAILURE FROM #{client_ip}: #{message}"
      history_length = 2
      unless user.is_a?(::Incline::User)
        message = "[email: #{user}] #{message}"
        user = User.anonymous
        history_length = 6
      end
      purge_old_history_for user, history_length
      user.login_histories.create(ip_address: client_ip, successful: false, message: message)
    end

    ##
    # Logs a success message for a user.
    def add_success_to(user, message, client_ip)  # :doc:
      Incline::Log::info "LOGIN(#{user}) SUCCESS FROM #{client_ip}: #{message}"
      purge_old_history_for user
      user.login_histories.create(ip_address: client_ip, successful: true, message: message)
    end

    private

    def purge_old_history_for(user, max_months = 2)
      user.login_histories.where('"incline_user_login_histories"."created_at" <= ?', Time.now - max_months.months).delete_all
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
incline-0.2.3 lib/incline/auth_engine_base.rb
incline-0.1.9 lib/incline/auth_engine_base.rb
incline-0.1.8 lib/incline/auth_engine_base.rb
incline-0.1.7 lib/incline/auth_engine_base.rb
incline-0.1.5 lib/incline/auth_engine_base.rb