Method: JunglePath::API::Helpers::AuthLocalUser#get_user

Defined in:
lib/jungle_path/api/helpers/auth_local_user.rb

#get_user(user_name, password, no_cache = false) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/jungle_path/api/helpers/auth_local_user.rb', line 110

def get_user user_name, password, no_cache=false
  # is this username/password valid?
  cache_key = "#{user_name}.#{password}"
  user = cache[cache_key]
  if user == nil or no_cache
    #assumed_user = nil
    ds = nil
    if user_name_is_key? user_name
      ds = db.base['select id, user_name, name, first_name, last_name, phone, email, hash, key, active from "user" where key = ?', user_name.downcase]
    else
      lowercase_user_name = nil
      lowercase_user_name = user_name.downcase if user_name
      ds = db.base['select id, user_name, name, first_name, last_name, phone, email, hash, key, active from "user" where user_name = ?', lowercase_user_name]
    end
    hash = ds.first
    #puts "get_user: hash: #{hash}."
    user = Schema::User.new(hash, false) if hash
    halt 401, "Unauthorized" unless user
    halt 401, "Unauthorized: user #{user.user_name} is not marked as active." unless user.active
    if user_name_is_key? user_name
      user.is_valid = true
    else
      user.is_valid = valid_user?(user, password)
    end
    cache[cache_key] = user if user
  end
  user.password = password
  user
end