code: private void sync(final DirContext ctx, final AlpineQueryManager qm, final LdapConnectionWrapper ldap,
                        LdapUser user) throws NamingException {
          LOGGER.debug("Syncing: " + user.getUsername());
          final SearchResult result = ldap.searchForSingleUsername(ctx, user.getUsername());
          if (result != null) {
              user.setDN(result.getNameInNamespace());
              user.setEmail(ldap.getAttribute(result, LdapConnectionWrapper.ATTRIBUTE_MAIL));
              user = qm.updateLdapUser(user);
              // Dynamically assign team membership (if enabled)
              if (LdapConnectionWrapper.TEAM_SYNCHRONIZATION) {
                  final List<String> groupDNs = ldap.getGroups(ctx, user);
                  qm.synchronizeTeamMembership(user, groupDNs);
              }
          } else {
              // This is an invalid user - a username that exists in the database that does not exist in LDAP
              user.setDN("INVALID");
              user.setEmail(null);
              user = qm.updateLdapUser(user);
              if (LdapConnectionWrapper.TEAM_SYNCHRONIZATION) {
                  qm.synchronizeTeamMembership(user, new ArrayList<>());
              }
          }
      }
docs: Performs the actual sync of the specified user.
  @param ctx a DirContext
  @param qm the AlpineQueryManager to use
  @param ldap the LdapConnectionWrapper to use
  @param user the LdapUser instance to sync
  @throws NamingException when a problem with the connection with the directory