Project

General

Profile

Patch #4643 » member-onthefly-somewhat-more-elaborate-SMILE.patch

Marcel Waldvogel, 2010-01-25 00:54

View differences:

app/models/auth_source_ldap.rb (Arbeitskopie)
60 60
  rescue  Net::LDAP::LdapError => text
61 61
    raise "LdapError: " + text
62 62
  end
63
  
64
  def import(login)
65
    logger.debug("Trying to import #{login}")
66
    return nil if login.blank?
67
    logger.debug("Continuing to import #{login}")
68
    attrs = []
69
    # get user's DN
70
    ldap_con = initialize_ldap_con(self.account, self.account_password)
71
    logger.debug("Opening ldap_con to #{ldap_con.to_s}")
72
    login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) 
73
    object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) 
74
    dn = String.new
75
    ldap_con.search( :base => self.base_dn, 
76
                     :filter => object_filter & login_filter, 
77
                     # only ask for the DN if on-the-fly registration is disabled
78
                     :attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry|
79
      dn = entry.dn
80
      attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
81
               :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
82
               :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
83
               :auth_source_id => self.id ]
84
      logger.info("LDAP found DN #{dn} for login #{login} with attrs #{attrs.inspect}")
85
    end
86
    return nil if dn.empty?
87
    logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
88
    # authenticate user
89
    # return user's attributes
90
    attrs    
91
  rescue  Net::LDAP::LdapError => text
92
    raise "LdapError: " + text
93
  end
63 94

  
64 95
  # test the connection to the LDAP
65 96
  def test_connection
app/models/auth_source.rb (Arbeitskopie)
46 46
    end
47 47
    return nil
48 48
  end
49

  
50
  # Try to import a user not yet registered against available sources
51
  def self.get_data(login)
52
    AuthSource.find(:all, :conditions => ["onthefly_register=?", true]).each do |source|
53
      begin
54
        logger.debug "Importing '#{login}' from '#{source.name}'" if logger && logger.debug?
55
	logger.debug "Using class #{source.class.to_s}" if logger && logger.debug?
56
        attrs = source.import(login)
57
      rescue => e
58
        logger.error "Error during import: #{e.message}"
59
        attrs = nil
60
      end
61
      return attrs if attrs
62
    end
63
    return nil
64
  end
65

  
66
  def self.import(login)
67
    auth = get_data(login)
68
    logger.debug("auth is #{auth.class.to_s}")
69
    if auth && auth.size == 1
70
      a = auth[0]
71
      a.each { |key, value| logger.debug("#{key} => #{value}") }
72
      user = User.new(a)
73
      user.login = login
74
      user.language = Setting.default_language
75
      user.admin = false # Just to be sure
76
      if user.save
77
        logger.debug("successful created")
78
	return user
79
      else
80
        logger.debug("failed to create")
81
	return nil
82
      end
83
    else
84
      logger.debug("User not found among those sources available for on-the-fly creation")
85
      return nil
86
    end
87
  end
88

  
49 89
end
app/controllers/members_controller.rb (Arbeitskopie)
24 24
    members = []
25 25
    if params[:member] && request.post?
26 26
      attrs = params[:member].dup
27
      # When no user is selected but the name does match a user
28
      # in LDAP, which has not yet been imported, then go and import the
29
      # user from LDAP and add it to the project. Multiple names may be
30
      # separated by whitespace.
31
      if (! attrs.has_key?(:user_ids) && ! params[:principal_search].empty?)
32
        attrs[:user_ids] = []
33
        newUser = nil
34
        params[:principal_search].split.each do |login|
35
	  newUser = AuthSource.import(login)
36
          if newUser
37
	    logger.info("Imported AuthSource as #{newUser}")
38
          else
39
            newUser = User.first(:conditions => ["login = ?", login])
40
          end
41
	  attrs[:user_ids] << newUser.id if newUser
42
          logger.debug("Would join entries #{attrs[:user_ids].inspect}")
43
        end
44
      end
27 45
      if (user_ids = attrs.delete(:user_ids))
28 46
        user_ids.each do |user_id|
29 47
          members << Member.new(attrs.merge(:user_id => user_id))
(2-2/2)