auth.rb 1.2 KB

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