--- plugins.py.orig 2010-01-15 00:45:43.000000000 +0100 +++ plugins.py 2010-01-15 00:57:19.000000000 +0100 @@ -129,7 +129,8 @@ implements(IMetadataProvider) def __init__(self, ldap_connection, attributes=None, - filterstr='(objectClass=*)'): + filterstr='(objectClass=*)', + flatten_attributes=()): """ Fetch LDAP attributes of the authenticated user. @@ -146,7 +147,11 @@ @raise ValueError: If L{make_ldap_connection} could not create a connection from C{ldap_connection}, or if C{attributes} is not an iterable. - + @param flatten_attributes: LDAP attributes that you want to + fetch as a string (instead of a list) when there is only + one value. Of course, if multiple values are returned, you + will get all of them as a list. + @type flatten_attributes: C{iterable} or C{str} """ if hasattr(attributes, 'split'): attributes = attributes.split(',') @@ -155,9 +160,17 @@ attributes = list(attributes) elif attributes is not None: raise ValueError('The needed LDAP attributes are not valid') + if hasattr(flatten_attributes, 'split'): + flatten_attributes = flatten_attributes.split(',') + elif hasattr(flatten_attributes, '__iter__'): + # Converted to list, just in case... + flatten_attributes = list(flatten_attributes) + elif attributes != (): + raise ValueError('flatten_attributes is not valid') self.ldap_connection = make_ldap_connection(ldap_connection) self.attributes = attributes self.filterstr = filterstr + self.flatten_attributes = flatten_attributes # IMetadataProvider def add_metadata(self, environ, identity): @@ -179,6 +192,10 @@ ) try: for (dn, attributes) in self.ldap_connection.search_s(*args): + for attr, value in attributes.items(): + if attr in self.flatten_attributes and \ + len(value) == 1: + attributes[attr] = value[0] identity.update(attributes) except ldap.LDAPError, msg: environ['repoze.who.logger'].warn('Cannot add metadata: %s' % \