Project

General

Profile

Defect #4283 » lookup_LDAP_attributes_as_user.patch

Felix Schäfer, 2009-11-30 23:01

View differences:

app/models/auth_source_ldap.rb
37 37
    # get user's DN
38 38
    ldap_con = initialize_ldap_con(self.account, self.account_password)
39 39
    login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) 
40
    object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) 
41 40
    dn = String.new
42 41
    ldap_con.search( :base => self.base_dn, 
43
                     :filter => object_filter & login_filter, 
44
                     # only ask for the DN if on-the-fly registration is disabled
45
                     :attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry|
42
                     :filter => login_filter, 
43
                     :attributes=> 'dn') do |entry|
46 44
      dn = entry.dn
47
      attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
48
               :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
49
               :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
50
               :auth_source_id => self.id ] if onthefly_register?
51 45
    end
52 46
    return nil if dn.empty?
53 47
    logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
54 48
    # authenticate user
55 49
    ldap_con = initialize_ldap_con(dn, password)
56 50
    return nil unless ldap_con.bind
57
    # return user's attributes
58 51
    logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
52
    # get attributes when on-the-fly registration is enabled
53
    if onthefly_register?
54
      ldap_con.search( :base => self.base_dn, 
55
                       :scope => Net::LDAP::SearchScope_BaseObject,
56
                       :attributes=> [self.attr_firstname, self.attr_lastname, self.attr_mail]) do |entry|
57
        attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
58
                 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
59
                 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
60
                 :auth_source_id => self.id ]
61
      end
62
    end
63
    # return user's attributes
59 64
    attrs    
60 65
  rescue  Net::LDAP::LdapError => text
61 66
    raise "LdapError: " + text
    (1-1/1)