def user_relevant(u): if BMO_IRC_NICK_RE.search(u['real_name']): return True if u['email'] == query: return True # This might allow too many users through. Let's not get too # attached to this. if u['real_name'] == query: return True return False
def get_or_create_bugzilla_users(user_data): # All users will have a stored password of "!", which Django uses to # indicate an invalid password. users = [] for user in user_data["users"]: bz_user_id = user["id"] email = user["email"] real_name = user["real_name"] can_login = user["can_login"] ircnick_match = BMO_IRC_NICK_RE.search(real_name) if ircnick_match: username = ircnick_match.group(1) else: username = placeholder_username(email, bz_user_id) try: bugzilla_user_map = BugzillaUserMap.objects.get(bugzilla_user_id=bz_user_id) except BugzillaUserMap.DoesNotExist: logger.info("bugzilla user %s/%s not present; creating %s" % (bz_user_id, email, username)) user = User(username=username, password="******", first_name=real_name, email=email, is_active=can_login) try: # Django kind of "corrupts" the DB transaction when an # integrity error occurs. So, we need to wrap in a # sub-transaction to prevent the "corruption" from # tainting the outer transaction. with transaction.atomic(): user.save() except: # Blanket exceptions are terrible, but there appears to # be no way to catch a generic IntegrityError since SQLite # and MySQL apparently share different exception class # hierarchies?! user.username = placeholder_username(email, bz_user_id) logger.info("could not create user %s; trying %s" % (username, user.username)) user.save() bugzilla_user_map = BugzillaUserMap(user=user, bugzilla_user_id=bz_user_id) bugzilla_user_map.save() profile = Profile.objects.get_or_create(user=user)[0] if not profile.is_private: profile.is_private = True profile.save() logger.info( "created user %s:%s from bugzilla user %s/%s/%s" % (user.id, user.username, bz_user_id, email, real_name) ) else: modified = False user = bugzilla_user_map.user old_username = user.username if user.username != username: logger.info("updating username of %s from %s to %s" % (user.id, user.username, username)) user.username = username try: with transaction.atomic(): user.save() except: # Blanket exceptions are terrible. new_username = placeholder_username(email, bz_user_id) if new_username != old_username: logger.info( "could not set preferred username to %s; " "updating username of %s from %s to %s" % (username, user.id, old_username, new_username) ) user.username = new_username user.save() else: logger.info("could not update username of %s; keeping " "as %s" % (user.id, old_username)) user.username = old_username if user.email != email: logger.info("updating email of %s:%s from %s to %s" % (user.id, user.username, user.email, email)) user.email = email modified = True if user.first_name != real_name: logger.info( "updating first name of %s:%s from %s to %s" % (user.id, user.username, user.first_name, real_name) ) user.first_name = real_name modified = True # Note that users *must* be disabled in Bugzilla and *cannot* # be disabled in Review Board, since, if user.is_active is False, # we can't tell if this was a result of can_login going False # at some previous time or the action of a Review Board admin. if user.is_active != can_login: logger.info("updating active of %s:%s to %s" % (user.id, user.username, can_login)) user.is_active = can_login modified = True if modified: user.save() users.append(user) return users
def get_or_create_bugzilla_users(user_data): # All users will have a stored password of "!", which Django uses to # indicate an invalid password. users = [] for user in user_data['users']: bz_user_id = user['id'] email = user['email'] real_name = user['real_name'] can_login = user['can_login'] ircnick_match = BMO_IRC_NICK_RE.search(real_name) if ircnick_match: username = ircnick_match.group(1) else: username = placeholder_username(email, bz_user_id) try: bugzilla_user_map = BugzillaUserMap.objects.get( bugzilla_user_id=bz_user_id) except BugzillaUserMap.DoesNotExist: logger.info('bugzilla user %s/%s not present; creating %s' % (bz_user_id, email, username)) user = User(username=username, password='******', first_name=real_name, email=email, is_active=can_login) try: # Django kind of "corrupts" the DB transaction when an # integrity error occurs. So, we need to wrap in a # sub-transaction to prevent the "corruption" from # tainting the outer transaction. with transaction.atomic(): user.save() except: # Blanket exceptions are terrible, but there appears to # be no way to catch a generic IntegrityError since SQLite # and MySQL apparently share different exception class # hierarchies?! user.username = placeholder_username(email, bz_user_id) logger.info('could not create user %s; trying %s' % (username, user.username)) user.save() bugzilla_user_map = BugzillaUserMap(user=user, bugzilla_user_id=bz_user_id) bugzilla_user_map.save() profile = Profile.objects.get_or_create(user=user)[0] if not profile.is_private: profile.is_private = True profile.save() logger.info('created user %s:%s from bugzilla user %s/%s/%s' % (user.id, user.username, bz_user_id, email, real_name)) else: modified = False user = bugzilla_user_map.user old_username = user.username if user.username != username: logger.info('updating username of %s from %s to %s' % (user.id, user.username, username)) user.username = username try: with transaction.atomic(): user.save() except: # Blanket exceptions are terrible. new_username = placeholder_username(email, bz_user_id) if new_username != old_username: logger.info( 'could not set preferred username to %s; ' 'updating username of %s from %s to %s' % (username, user.id, old_username, new_username)) user.username = new_username user.save() else: logger.info('could not update username of %s; keeping ' 'as %s' % (user.id, old_username)) user.username = old_username if user.email != email: logger.info('updating email of %s:%s from %s to %s' % (user.id, user.username, user.email, email)) user.email = email modified = True if user.first_name != real_name: logger.info( 'updating first name of %s:%s from %s to %s' % (user.id, user.username, user.first_name, real_name)) user.first_name = real_name modified = True # Note that users *must* be disabled in Bugzilla and *cannot* # be disabled in Review Board, since, if user.is_active is False, # we can't tell if this was a result of can_login going False # at some previous time or the action of a Review Board admin. if user.is_active != can_login: logger.info('updating active of %s:%s to %s' % (user.id, user.username, can_login)) user.is_active = can_login modified = True if modified: user.save() users.append(user) return users