protected int authenticate(
      long companyId, String emailAddress, String screenName, long userId, String password)
      throws Exception {

    LDAPAuthConfiguration ldapAuthConfiguration =
        _ldapAuthConfigurationProvider.getConfiguration(companyId);

    if (!ldapAuthConfiguration.enabled()) {
      if (_log.isDebugEnabled()) {
        _log.debug("Authenticator is not enabled");
      }

      return SUCCESS;
    }

    if (_log.isDebugEnabled()) {
      _log.debug("Authenticator is enabled");
    }

    int preferredLDAPServerResult =
        authenticateAgainstPreferredLDAPServer(
            companyId, emailAddress, screenName, userId, password);

    LDAPImportConfiguration ldapImportConfiguration =
        _ldapImportConfigurationProvider.getConfiguration(companyId);

    if (preferredLDAPServerResult == SUCCESS) {
      if (ldapImportConfiguration.importUserPasswordEnabled()) {
        return preferredLDAPServerResult;
      }

      return Authenticator.SKIP_LIFERAY_CHECK;
    }

    List<LDAPServerConfiguration> ldapServerConfigurations =
        _ldapServerConfigurationProvider.getConfigurations(companyId);

    for (LDAPServerConfiguration ldapServerConfiguration : ldapServerConfigurations) {

      int result =
          authenticate(
              ldapServerConfiguration.ldapServerId(),
              companyId,
              emailAddress,
              screenName,
              userId,
              password);

      if (result == SUCCESS) {
        if (ldapImportConfiguration.importUserPasswordEnabled()) {
          return result;
        }

        return Authenticator.SKIP_LIFERAY_CHECK;
      }
    }

    return authenticateRequired(companyId, userId, emailAddress, screenName, true, FAILURE);
  }