Class: Caboose::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
app/models/caboose/authenticator.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(username, password, site = nil) ⇒ Object



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
# File 'app/models/caboose/authenticator.rb', line 4

def authenticate(username, password, site = nil)
  resp = Caboose::StdClass.new(
    'error' => nil,
    'user' => nil 
  )
  pass = Digest::SHA1.hexdigest(Caboose::salt + password)
  
  user = Caboose::User.where(:username => username, :site_id => site.id).first
  user = Caboose::User.where(:email    => username, :site_id => site.id).first if user.nil?
          
  valid_credentials = false
  if user && user.password == pass 
    valid_credentials = true
  elsif site      
    mp = Caboose::Setting.where(:site_id => site.id, :name => 'master_password').first
    mp = mp ? mp.value : nil
    if mp && mp.strip.length > 0 && mp == pass
      valid_credentials = true
    end
  end
  
  if valid_credentials
    resp.user = user
  else
    resp.error = "Invalid credentials"
  end
  
  #resp.user = Caboose::User.where(:username => username, :password => pass).first    
  #if (resp.user.nil?)
  #  resp.user = Caboose::User.where(:email => username, :password => pass).first
  #end                
  #resp.error = "Invalid credentials" if resp.user.nil?
      
  return resp
end