Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

auth_source_ldap.rb 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # redMine - project management software
  2. # Copyright (C) 2006 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require 'net/ldap'
  18. require 'iconv'
  19. class AuthSourceLdap < AuthSource
  20. validates_presence_of :host, :port, :attr_login
  21. def after_initialize
  22. self.port = 389 if self.port == 0
  23. end
  24. def authenticate(login, password)
  25. attrs = []
  26. # get user's DN
  27. ldap_con = initialize_ldap_con(self.account, self.account_password)
  28. login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
  29. object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
  30. dn = String.new
  31. ldap_con.search( :base => self.base_dn,
  32. :filter => object_filter & login_filter,
  33. :attributes=> ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail]) do |entry|
  34. dn = entry.dn
  35. attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
  36. :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
  37. :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
  38. :auth_source_id => self.id ]
  39. end
  40. return nil if dn.empty?
  41. logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
  42. # authenticate user
  43. ldap_con = initialize_ldap_con(dn, password)
  44. return nil unless ldap_con.bind
  45. # return user's attributes
  46. logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
  47. attrs
  48. rescue Net::LDAP::LdapError => text
  49. raise "LdapError: " + text
  50. end
  51. # test the connection to the LDAP
  52. def test_connection
  53. ldap_con = initialize_ldap_con(self.account, self.account_password)
  54. ldap_con.open { }
  55. rescue Net::LDAP::LdapError => text
  56. raise "LdapError: " + text
  57. end
  58. def auth_method_name
  59. "LDAP"
  60. end
  61. private
  62. def initialize_ldap_con(ldap_user, ldap_password)
  63. Net::LDAP.new( {:host => self.host,
  64. :port => self.port,
  65. :auth => { :method => :simple, :username => Iconv.new('iso-8859-15', 'utf-8').iconv(ldap_user), :password => Iconv.new('iso-8859-15', 'utf-8').iconv(ldap_password) }}
  66. )
  67. end
  68. def self.get_attr(entry, attr_name)
  69. entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
  70. end
  71. end