server/ldaputils.py
changeset 8387 b59af20a868d
parent 8384 98782f17dd84
child 8430 5bee87a14bb1
equal deleted inserted replaced
8386:249b21722e5e 8387:b59af20a868d
    35 import ldap
    35 import ldap
    36 from ldap.ldapobject import ReconnectLDAPObject
    36 from ldap.ldapobject import ReconnectLDAPObject
    37 from ldap.filter import filter_format
    37 from ldap.filter import filter_format
    38 from ldapurl import LDAPUrl
    38 from ldapurl import LDAPUrl
    39 
    39 
    40 from cubicweb import ValidationError, AuthenticationError
    40 from cubicweb import ValidationError, AuthenticationError, Binary
    41 from cubicweb.server.sources import ConnectionWrapper
    41 from cubicweb.server.sources import ConnectionWrapper
    42 
    42 
    43 _ = unicode
    43 _ = unicode
    44 
    44 
    45 # search scopes
    45 # search scopes
   123 You can set multiple groups by separating them by a comma.',
   123 You can set multiple groups by separating them by a comma.',
   124           'group': 'ldap-source', 'level': 1,
   124           'group': 'ldap-source', 'level': 1,
   125           }),
   125           }),
   126         ('user-attrs-map',
   126         ('user-attrs-map',
   127          {'type' : 'named',
   127          {'type' : 'named',
   128           'default': {'uid': 'login', 'gecos': 'email'},
   128           'default': {'uid': 'login', 'gecos': 'email', 'userPassword': 'upassword'},
   129           'help': 'map from ldap user attributes to cubicweb attributes (with Active Directory, you want to use sAMAccountName:login,mail:email,givenName:firstname,sn:surname)',
   129           'help': 'map from ldap user attributes to cubicweb attributes (with Active Directory, you want to use sAMAccountName:login,mail:email,givenName:firstname,sn:surname)',
   130           'group': 'ldap-source', 'level': 1,
   130           'group': 'ldap-source', 'level': 1,
   131           }),
   131           }),
   132 
   132 
   133     )
   133     )
   342 
   342 
   343     def _process_ldap_item(self, dn, iterator):
   343     def _process_ldap_item(self, dn, iterator):
   344         """Turn an ldap received item into a proper dict."""
   344         """Turn an ldap received item into a proper dict."""
   345         itemdict = {'dn': dn}
   345         itemdict = {'dn': dn}
   346         for key, value in iterator:
   346         for key, value in iterator:
   347             if not isinstance(value, str):
   347             if self.user_attrs.get(key) == 'upassword': # XXx better password detection
   348                 try:
   348                 itemdict[key] = Binary(value[0].encode('utf-8'))
   349                     for i in range(len(value)):
   349             else:
   350                         value[i] = unicode(value[i], 'utf8')
   350                 for i, val in enumerate(value):
   351                 except Exception:
   351                     value[i] = unicode(val, 'utf-8', 'replace')
   352                     pass
   352                 if isinstance(value, list) and len(value) == 1:
   353             if isinstance(value, list) and len(value) == 1:
   353                     itemdict[key] = value = value[0]
   354                 itemdict[key] = value = value[0]
       
   355         return itemdict
   354         return itemdict
   356 
   355 
   357     def _process_no_such_object(self, session, dn):
   356     def _process_no_such_object(self, session, dn):
   358         """Some search return NO_SUCH_OBJECT error, handle this (usually because
   357         """Some search return NO_SUCH_OBJECT error, handle this (usually because
   359         an object whose dn is no more existent in ldap as been encountered).
   358         an object whose dn is no more existent in ldap as been encountered).