Sha256: c241350f30c11a9f46d9c8362474458a0c35c7ab74a74a6e1545dee87181af5e

Contents?: true

Size: 1.07 KB

Versions: 62

Compression:

Stored size: 1.07 KB

Contents

module Usman
  class AuthenticationService

    attr_reader :login_handle, :password, :error, :user, :remote_ip

    def initialize(params)
      @login_handle = params[:login_handle]
      @password = params[:password]
      @remote_ip = params[:remote_ip]
      @error = nil
      
      check_if_user_exists
      if @user
        authenticate
        check_if_user_is_approved
      end

      @user.start_session(@remote_ip) unless @error
    end

    def invalid_login_error
      "authentication.invalid_login"
    end

    def user_status_error
      "authentication.user_is_#{@user.status.downcase}"
    end

    def check_if_user_exists
      @user = User.where("LOWER(email) = LOWER('#{@login_handle}') OR LOWER(username) = LOWER('#{@login_handle}')").first
      set_error(invalid_login_error) unless @user
    end

    def check_if_user_is_approved
      set_error(user_status_error) unless @user.approved?
    end

    def authenticate
      set_error(invalid_login_error) unless @user.authenticate(@password)
    end

    def set_error(id)
      @error ||= id
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
usman-0.4.10.pre.materialize app/services/usman/authentication_service.rb
usman-0.4.9.pre.materialize app/services/usman/authentication_service.rb
usman-0.4.8.pre.materialize app/services/usman/authentication_service.rb
usman-0.4.7.pre.materialize app/services/usman/authentication_service.rb
usman-0.4.6.pre.materialize app/services/usman/authentication_service.rb
usman-0.4.5.pre.materialize app/services/usman/authentication_service.rb
usman-0.4.4.pre.materialize app/services/usman/authentication_service.rb
usman-0.4.3.pre.materialize app/services/usman/authentication_service.rb
usman-0.4.2.pre.materialize app/services/usman/authentication_service.rb
usman-0.4.1.pre.materialize app/services/usman/authentication_service.rb
usman-0.4.0.pre.materialize app/services/usman/authentication_service.rb
usman-0.3.38 app/services/usman/authentication_service.rb
usman-0.3.37 app/services/usman/authentication_service.rb
usman-0.3.36 app/services/usman/authentication_service.rb
usman-0.3.35 app/services/usman/authentication_service.rb
usman-0.3.34 app/services/usman/authentication_service.rb
usman-0.3.33 app/services/usman/authentication_service.rb
usman-0.3.32 app/services/usman/authentication_service.rb
usman-0.3.31 app/services/usman/authentication_service.rb
usman-0.3.30 app/services/usman/authentication_service.rb