code: private LdapUser autoProvision(final AlpineQueryManager qm) throws AlpineAuthenticationException {
          LOGGER.debug("Provisioning: " + username);
          LdapUser user = null;
          final LdapConnectionWrapper ldap = new LdapConnectionWrapper();
          DirContext dirContext = null;
          try {
              dirContext = ldap.createDirContext();
              final SearchResult result = ldap.searchForSingleUsername(dirContext, username);
              if (result != null) {
                  user = new LdapUser();
                  user.setUsername(username);
                  user.setDN(result.getNameInNamespace());
                  user.setEmail(ldap.getAttribute(result, LdapConnectionWrapper.ATTRIBUTE_MAIL));
                  user = qm.persist(user);
                  // Dynamically assign team membership (if enabled)
                  if (LdapConnectionWrapper.TEAM_SYNCHRONIZATION) {
                      final List<String> groupDNs = ldap.getGroups(dirContext, user);
                      user = qm.synchronizeTeamMembership(user, groupDNs);
                  }
              } else {
                  LOGGER.warn("Could not find '" + username + "' in the directory while provisioning the user. Ensure '" + Config.AlpineKey.LDAP_ATTRIBUTE_NAME.getPropertyName() + "' is defined correctly");
                  throw new AlpineAuthenticationException(AlpineAuthenticationException.CauseType.UNMAPPED_ACCOUNT);
              }
          } catch (NamingException e) {
              LOGGER.error("An error occurred while auto-provisioning an authenticated user", e);
              throw new AlpineAuthenticationException(AlpineAuthenticationException.CauseType.OTHER);
          } finally {
              ldap.closeQuietly(dirContext);
          }
          return user;
      }
docs: Automatically creates an LdapUser, sets the value of various LDAP attributes, and
  persists the user to the database.
  @param qm the query manager to use
  @return the persisted LdapUser object
  @throws AlpineAuthenticationException if an exception occurs
  @since 1.4.0