Class: Usman::AuthenticationService

Inherits:
Object
  • Object
show all
Defined in:
app/services/usman/authentication_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ AuthenticationService

Returns a new instance of AuthenticationService.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/usman/authentication_service.rb', line 6

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

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



4
5
6
# File 'app/services/usman/authentication_service.rb', line 4

def error
  @error
end

#login_handleObject (readonly)

Returns the value of attribute login_handle.



4
5
6
# File 'app/services/usman/authentication_service.rb', line 4

def 
  @login_handle
end

#passwordObject (readonly)

Returns the value of attribute password.



4
5
6
# File 'app/services/usman/authentication_service.rb', line 4

def password
  @password
end

#remote_ipObject (readonly)

Returns the value of attribute remote_ip.



4
5
6
# File 'app/services/usman/authentication_service.rb', line 4

def remote_ip
  @remote_ip
end

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'app/services/usman/authentication_service.rb', line 4

def user
  @user
end

Instance Method Details

#authenticateObject



38
39
40
# File 'app/services/usman/authentication_service.rb', line 38

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

#check_if_user_existsObject



29
30
31
32
# File 'app/services/usman/authentication_service.rb', line 29

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

#check_if_user_is_approvedObject



34
35
36
# File 'app/services/usman/authentication_service.rb', line 34

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

#invalid_login_errorObject



21
22
23
# File 'app/services/usman/authentication_service.rb', line 21

def 
  "authentication.invalid_login"
end

#set_error(id) ⇒ Object



42
43
44
# File 'app/services/usman/authentication_service.rb', line 42

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

#user_status_errorObject



25
26
27
# File 'app/services/usman/authentication_service.rb', line 25

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