auth.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Auth
  3. include ApplicationLib
  4. =begin
  5. authenticate user via username and password
  6. result = Auth.check(username, password, user)
  7. returns
  8. result = user_model # if authentication was successfully
  9. =end
  10. def self.check(username, password, user)
  11. # use std. auth backends
  12. config = [
  13. {
  14. adapter: 'Auth::Internal',
  15. },
  16. {
  17. adapter: 'Auth::Developer',
  18. },
  19. ]
  20. # added configured backends
  21. Setting.where(area: 'Security::Authentication').each { |setting|
  22. if setting.state_current[:value]
  23. config.push setting.state_current[:value]
  24. end
  25. }
  26. # try to login against configure auth backends
  27. user_auth = nil
  28. config.each { |config_item|
  29. next if !config_item[:adapter]
  30. # load backend
  31. backend = load_adapter(config_item[:adapter])
  32. next if !backend
  33. user_auth = backend.check(username, password, config_item, user)
  34. # auth not ok
  35. next if !user_auth
  36. Rails.logger.info "Authentication against #{config_item[:adapter]} for user #{user_auth.login} ok."
  37. # remember last login date
  38. user_auth.update_last_login
  39. return user_auth
  40. }
  41. nil
  42. end
  43. end