def import_users(self): """Imports users from all the configured LDAP plugins into OGDS. """ session = create_session() # Set all SQL users inactive first - the ones still contained in the LDAP # will be set active again below (in the same transaction). for user in session.query(User): user.active = 0 for plugin in self._ldap_plugins(): ldap_userfolder = plugin._getLDAPUserFolder() uid_attr = self._get_uid_attr(ldap_userfolder) ldap_util = ILDAPSearch(ldap_userfolder) ldap_users = ldap_util.get_users() for ldap_user in ldap_users: dn, info = ldap_user # Ignore users without an UID in LDAP if not uid_attr in info: continue userid = info[uid_attr] # Skip users with uid longer than SQL 'userid' column # FIXME: Increase size of SQL column to 64 if len(userid) > 30: continue if not self.user_exists(userid): # Create the new user user = User(userid) session.add(user) else: # Get the existing user user = session.query(User).filter_by(userid=userid).first() # Iterate over all SQL columns and update their values columns = User.__table__.columns for col in columns: if col.name == 'userid': # We already set the userid when creating the user # object, and it may not be called the same in LDAP as # in our SQL model continue value = info.get(col.name) # We can't store sequences in SQL columns. So if we do get a multi-valued field # to be stored directly in OGDS, we treat it as a multi-line string and join it. if isinstance(value, list) or isinstance(value, tuple): value = ' '.join([str(v) for v in value]) setattr(user, col.name, value) # Set the user active user.active = 1 logger.info("Imported user '%s'..." % userid) session.flush()
def import_users(self): """Imports users from all the configured LDAP plugins into OGDS. """ session = create_session() # Set all SQL users inactive first - the ones still contained in the LDAP # will be set active again below (in the same transaction). for user in session.query(User): user.active = 0 for plugin in self._ldap_plugins(): ldap_userfolder = plugin._getLDAPUserFolder() uid_attr = self._get_uid_attr(ldap_userfolder) ldap_util = ILDAPSearch(ldap_userfolder) ldap_users = ldap_util.get_users() for ldap_user in ldap_users: dn, info = ldap_user # Ignore users without an UID in LDAP if not uid_attr in info: continue userid = info[uid_attr] # Skip users with uid longer than SQL 'userid' column # FIXME: Increase size of SQL column to 64 if len(userid) > 30: continue if not self.user_exists(userid): # Create the new user user = User(userid) session.add(user) else: # Get the existing user user = session.query(User).filter_by(userid=userid).first() # Iterate over all SQL columns and update their values columns = User.__table__.columns for col in columns: if col.name == 'userid': # We already set the userid when creating the user # object, and it may not be called the same in LDAP as # in our SQL model continue value = info.get(col.name) # We can't store sequences in SQL columns. So if we do get a multi-valued field # to be stored directly in OGDS, we treat it as a multi-line string and join it. if isinstance(value, list) or isinstance(value, tuple): value = ' '.join([str(v) for v in value]) setattr(user, col.name, value) # Set the user active user.active = 1 logger.info("Imported user '%s'..." % userid) session.flush()
def import_users(self): """Imports users from all the configured LDAP plugins into OGDS. """ session = create_session() # Set all SQL users inactive first - the ones still contained in the # LDAP will be set active again below (in the same transaction). for user in session.query(User): user.active = False for plugin in self._ldap_plugins(): ldap_userfolder = plugin._getLDAPUserFolder() uid_attr = self._get_uid_attr(ldap_userfolder) ldap_util = ILDAPSearch(ldap_userfolder) logger.info(u'Users base: %s' % ldap_userfolder.users_base) logger.info(u'User filter: %s' % ldap_util.get_user_filter()) ldap_users = ldap_util.get_users() for ldap_user in ldap_users: dn, info = ldap_user # Ignore users without an UID in LDAP if uid_attr not in info: continue userid = info[uid_attr] userid = userid.decode('utf-8') # Skip users with uid longer than SQL 'userid' column if len(userid) > USER_ID_LENGTH: logger.warn(u"Skipping user '{}' - " u"userid too long!".format(userid)) continue if not self.user_exists(userid): # Create the new user user = User(userid) session.add(user) else: # Get the existing user try: user = self.get_sql_user(userid) except MultipleResultsFound: # Duplicate user with slightly different spelling # (casing, whitespace, ...) that may not be considered # different by the SQL backend's unique constraint. # We therefore enforce uniqueness ourselves. logger.warn( u"Skipping duplicate user '{}'!".format(userid)) continue # Iterate over all SQL columns and update their values columns = User.__table__.columns for col in columns: if col.name == 'userid': # We already set the userid when creating the user # object, and it may not be called the same in LDAP as # in our SQL model continue value = info.get(col.name) # We can't store sequences in SQL columns. So if we do get # a multi-valued field to be stored directly in OGDS, we # treat it as a multi-line string and join it. if isinstance(value, list) or isinstance(value, tuple): value = ' '.join([str(v) for v in value]) if isinstance(value, str): value = value.decode('utf-8') # Truncate purely descriptive user fields if necessary if isinstance(col.type, String): if value and len(value) > col.type.length: logger.warn(u"Truncating value %r for column %r " u"(user: %r)" % (value, col.name, userid)) value = value[:col.type.length] setattr(user, col.name, value) # Set the user active user.active = True logger.info(u"Imported user '{}'".format(userid)) session.flush()
def import_users(self): """Imports users from all the configured LDAP plugins into OGDS. """ session = create_session() # Set all SQL users inactive first - the ones still contained in the # LDAP will be set active again below (in the same transaction). for user in session.query(User): user.active = False for plugin in self._ldap_plugins(): ldap_userfolder = plugin._getLDAPUserFolder() uid_attr = self._get_uid_attr(ldap_userfolder) ldap_util = ILDAPSearch(ldap_userfolder) logger.info(u"Users base: %s" % ldap_userfolder.users_base) logger.info(u"User filter: %s" % ldap_util.get_user_filter()) ldap_users = ldap_util.get_users() for ldap_user in ldap_users: dn, info = ldap_user # Ignore users without an UID in LDAP if uid_attr not in info: continue userid = info[uid_attr] userid = userid.decode("utf-8") # Skip users with uid longer than SQL 'userid' column if len(userid) > USER_ID_LENGTH: logger.warn(u"Skipping user '{}' - " u"userid too long!".format(userid)) continue if not self.user_exists(userid): # Create the new user user = User(userid) session.add(user) else: # Get the existing user try: user = self.get_sql_user(userid) except MultipleResultsFound: # Duplicate user with slightly different spelling # (casing, whitespace, ...) that may not be considered # different by the SQL backend's unique constraint. # We therefore enforce uniqueness ourselves. logger.warn(u"Skipping duplicate user '{}'!".format(userid)) continue # Iterate over all SQL columns and update their values columns = User.__table__.columns for col in columns: if col.name == "userid": # We already set the userid when creating the user # object, and it may not be called the same in LDAP as # in our SQL model continue value = info.get(col.name) # We can't store sequences in SQL columns. So if we do get # a multi-valued field to be stored directly in OGDS, we # treat it as a multi-line string and join it. if isinstance(value, list) or isinstance(value, tuple): value = " ".join([str(v) for v in value]) if isinstance(value, str): value = value.decode("utf-8") setattr(user, col.name, value) # Set the user active user.active = True logger.info(u"Imported user '{}'".format(userid)) session.flush()