ldap.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  2. require 'net/ldap'
  3. module Auth::Ldap
  4. def self.check( username, password, config, user )
  5. scope = Net::LDAP::SearchScope_WholeSubtree
  6. # ldap connect
  7. ldap = Net::LDAP.new( :host => config[:host], :port => config[:port] )
  8. # set auth data if needed
  9. if config[:bind_dn] && config[:bind_pw]
  10. ldap.auth config[:bind_dn], config[:bind_pw]
  11. end
  12. # ldap bind
  13. begin
  14. if !ldap.bind
  15. puts "NOTICE: Can't bind to '#{config[:host]}', #{ldap.get_operation_result.code}, #{ldap.get_operation_result.message}"
  16. return
  17. end
  18. rescue Exception => e
  19. puts "NOTICE: Can't connect to '#{config[:host]}', #{e.to_s}"
  20. return
  21. end
  22. # search user
  23. filter = "(#{config[:uid]}=#{username})"
  24. if config[:always_filter] && !config[:always_filter].empty?
  25. filter = "(&#{filter}#{config[:always_filter]})"
  26. end
  27. user_dn = nil
  28. user_data = {}
  29. ldap.search( :base => config[:base], :filter => filter, :scope => scope ) do |entry|
  30. user_data = {}
  31. user_dn = entry.dn
  32. # remember attributes for :sync_params
  33. entry.each do |attribute, values|
  34. user_data[ attribute.downcase.to_sym ] = ''
  35. values.each do |value|
  36. user_data[ attribute.downcase.to_sym ] = value
  37. end
  38. end
  39. end
  40. if user_dn == nil
  41. puts "NOTICE: ldap entry found for user '#{username}' with filter #{filter} failed!"
  42. return nil
  43. end
  44. # try ldap bind with user credentals
  45. auth = ldap.authenticate user_dn, password
  46. if !ldap.bind( auth )
  47. puts "NOTICE: ldap bind with '#{user_dn}' failed!"
  48. return false
  49. end
  50. # create/update user
  51. if config[:sync_params]
  52. user_attributes = {
  53. :source => 'ldap',
  54. :updated_by_id => 1,
  55. }
  56. config[:sync_params].each {| local_data, ldap_data |
  57. if user_data[ ldap_data.downcase.to_sym ]
  58. user_attributes[ local_data.downcase.to_sym] = user_data[ ldap_data.downcase.to_sym ]
  59. end
  60. }
  61. if !user
  62. user_attributes[:created_by_id] = 1
  63. user = User.create( user_attributes )
  64. puts "NOTICE: user created '#{user.login}'"
  65. else
  66. user.update_attributes( user_attributes )
  67. puts "NOTICE: user updated '#{user.login}'"
  68. end
  69. end
  70. # return if it was not possible to create user
  71. return if !user
  72. # sync roles
  73. # FIXME
  74. # sync groups
  75. # FIXME
  76. # set always roles
  77. if config[:always_roles]
  78. role_ids = user.role_ids
  79. config[:always_roles].each {|role_name|
  80. role = Role.where( :name => role_name ).first
  81. next if !role
  82. if !role_ids.include?( role.id )
  83. role_ids.push role.id
  84. end
  85. }
  86. user.role_ids = role_ids
  87. user.save
  88. end
  89. # set always groups
  90. if config[:always_groups]
  91. group_ids = user.group_ids
  92. config[:always_groups].each {|group_name|
  93. group = Group.where( :name => group_name ).first
  94. next if !group
  95. if !group_ids.include?( group.id )
  96. group_ids.push group.id
  97. end
  98. }
  99. user.group_ids = group_ids
  100. user.save
  101. end
  102. # take session down
  103. # - not needed, done by Net::LDAP -
  104. return user
  105. end
  106. end