ldap.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. require_dependency 'ldap'
  3. require_dependency 'ldap/user'
  4. class Auth
  5. class Ldap < Auth::Base
  6. def valid?(user, password)
  7. return false if !Setting.get('ldap_integration')
  8. ldap_user = ::Ldap::User.new()
  9. # get from config or fallback to login
  10. # for a list of user attributes which should
  11. # be used for logging in
  12. login_attributes = @config[:login_attributes] || %w[login]
  13. authed = login_attributes.any? do |attribute|
  14. ldap_user.valid?(user[attribute], password)
  15. end
  16. log_auth_result(user, authed)
  17. authed
  18. rescue => e
  19. message = "Can't connect to ldap backend #{e}"
  20. Rails.logger.info message
  21. Rails.logger.info e
  22. log(
  23. user: user,
  24. status: 'failed',
  25. response: message,
  26. )
  27. false
  28. end
  29. private
  30. def log_auth_result(user, authed)
  31. result = authed ? 'success' : 'failed'
  32. log(
  33. user: user,
  34. status: result,
  35. )
  36. end
  37. def log(user:, status:, response: nil)
  38. HttpLog.create(
  39. direction: 'out',
  40. facility: 'ldap',
  41. url: "bind -> #{user.login}",
  42. status: status,
  43. ip: nil,
  44. request: { content: user.login },
  45. response: { content: response || status },
  46. method: 'tcp',
  47. created_by_id: 1,
  48. updated_by_id: 1,
  49. )
  50. end
  51. end
  52. end