class LDAPDataHandler(object): """ Collector handler to provide user data from LDAP """ def __init__(self, *args, **kw): # load pykolab conf self.pykolab_conf = pykolab.getConf() if not hasattr(self.pykolab_conf, 'defaults'): self.pykolab_conf.finalize_conf(fatal=False) self.ldap = Auth() self.ldap.connect() def register(self, callback): interests = { 'GETUSERDATA': { 'callback': self.get_user_data } } callback(interests) def get_user_data(self, notification): notification = json.loads(notification) log.debug("GETUSERDATA for %r" % (notification), level=9) if notification.has_key('user'): try: user_dn = self.ldap.find_user_dn(notification['user'], True) log.debug("User DN for %s: %r" % (notification['user'], user_dn), level=8) except Exception, e: log.error("LDAP connection error: %r", e) user_dn = None if user_dn: unique_attr = self.pykolab_conf.get('ldap', 'unique_attribute', 'nsuniqueid') user_rec = self.ldap.get_entry_attributes(None, user_dn, [unique_attr, 'cn']) log.debug("User attributes: %r" % (user_rec), level=8) if user_rec and user_rec.has_key(unique_attr): user_rec['dn'] = user_dn user_rec['id'] = user_rec[unique_attr] del user_rec[unique_attr] else: user_rec = None notification['user_data'] = user_rec return json.dumps(notification)
def user_dn_from_email_address(email_address): """ Resolves the given email address to a Kolab user entity """ global auth if not auth: auth = Auth() auth.connect() # return cached value if user_dn_from_email_address.cache.has_key(email_address): return user_dn_from_email_address.cache[email_address] local_domains = auth.list_domains() if not local_domains == None: local_domains = list(set(local_domains.keys())) if not email_address.split('@')[1] in local_domains: user_dn_from_email_address.cache[email_address] = None return None log.debug(_("Checking if email address %r belongs to a local user") % (email_address), level=8) user_dn = auth.find_user_dn(email_address, True) if isinstance(user_dn, basestring): log.debug(_("User DN: %r") % (user_dn), level=8) else: log.debug(_("No user record(s) found for %r") % (email_address), level=9) # remember this lookup user_dn_from_email_address.cache[email_address] = user_dn return user_dn