protected User getUser(long companyId, LDAPUser ldapUser) throws Exception {
    User user = null;

    try {
      String authType =
          PrefsPropsUtil.getString(
              companyId,
              PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
              PropsValues.COMPANY_SECURITY_AUTH_TYPE);

      if (authType.equals(CompanyConstants.AUTH_TYPE_SN) && !ldapUser.isAutoScreenName()) {

        user = UserLocalServiceUtil.getUserByScreenName(companyId, ldapUser.getScreenName());
      } else {
        user = UserLocalServiceUtil.getUserByEmailAddress(companyId, ldapUser.getEmailAddress());
      }
    } catch (NoSuchUserException nsue) {
    }

    return user;
  }
  protected User addUser(long companyId, LDAPUser ldapUser, String password) throws Exception {

    if (_log.isDebugEnabled()) {
      _log.debug("Adding user " + ldapUser.getEmailAddress());
    }

    boolean autoPassword = ldapUser.isAutoPassword();

    if (!PropsValues.LDAP_IMPORT_USER_PASSWORD_ENABLED) {
      autoPassword = PropsValues.LDAP_IMPORT_USER_PASSWORD_AUTOGENERATED;

      if (!autoPassword) {
        String defaultPassword = PropsValues.LDAP_IMPORT_USER_PASSWORD_DEFAULT;

        if (defaultPassword.equalsIgnoreCase(_USER_PASSWORD_SCREEN_NAME)) {

          defaultPassword = ldapUser.getScreenName();
        }

        password = defaultPassword;
      }
    }

    Calendar birthdayCal = CalendarFactoryUtil.getCalendar();

    birthdayCal.setTime(ldapUser.getBirthday());

    int birthdayMonth = birthdayCal.get(Calendar.MONTH);
    int birthdayDay = birthdayCal.get(Calendar.DAY_OF_MONTH);
    int birthdayYear = birthdayCal.get(Calendar.YEAR);

    User user =
        UserLocalServiceUtil.addUser(
            ldapUser.getCreatorUserId(),
            companyId,
            autoPassword,
            password,
            password,
            ldapUser.isAutoScreenName(),
            ldapUser.getScreenName(),
            ldapUser.getEmailAddress(),
            0,
            StringPool.BLANK,
            ldapUser.getLocale(),
            ldapUser.getFirstName(),
            ldapUser.getMiddleName(),
            ldapUser.getLastName(),
            0,
            0,
            ldapUser.isMale(),
            birthdayMonth,
            birthdayDay,
            birthdayYear,
            StringPool.BLANK,
            ldapUser.getGroupIds(),
            ldapUser.getOrganizationIds(),
            ldapUser.getRoleIds(),
            ldapUser.getUserGroupIds(),
            ldapUser.isSendEmail(),
            ldapUser.getServiceContext());

    if (ldapUser.isUpdatePortrait()) {
      byte[] portraitBytes = ldapUser.getPortraitBytes();

      if ((portraitBytes != null) && (portraitBytes.length > 0)) {
        user = UserLocalServiceUtil.updatePortrait(user.getUserId(), portraitBytes);
      }
    }

    return user;
  }
  protected User updateUser(
      long companyId, LDAPUser ldapUser, User user, String password, String modifiedDate)
      throws Exception {

    Date ldapUserModifiedDate = null;

    try {
      if (Validator.isNull(modifiedDate)) {
        if (_log.isInfoEnabled()) {
          _log.info("LDAP entry never modified, skipping user " + user.getEmailAddress());
        }

        return user;
      } else {
        ldapUserModifiedDate = LDAPUtil.parseDate(modifiedDate);
      }

      if (ldapUserModifiedDate.equals(user.getModifiedDate()) && ldapUser.isAutoPassword()) {

        if (_log.isDebugEnabled()) {
          _log.debug("User is already synchronized, skipping user " + user.getEmailAddress());
        }

        return user;
      }
    } catch (ParseException pe) {
      if (_log.isDebugEnabled()) {
        _log.debug("Unable to parse LDAP modify timestamp " + modifiedDate, pe);
      }
    }

    boolean passwordReset = ldapUser.isPasswordReset();

    if (PrefsPropsUtil.getBoolean(
        companyId, PropsKeys.LDAP_EXPORT_ENABLED, PropsValues.LDAP_EXPORT_ENABLED)) {

      passwordReset = user.isPasswordReset();
    }

    if (!PropsValues.LDAP_IMPORT_USER_PASSWORD_ENABLED) {
      password = PropsValues.LDAP_IMPORT_USER_PASSWORD_DEFAULT;

      if (password.equalsIgnoreCase(_USER_PASSWORD_SCREEN_NAME)) {
        password = ldapUser.getScreenName();
      }
    }

    if (Validator.isNull(ldapUser.getScreenName())) {
      ldapUser.setAutoScreenName(true);
    }

    if (ldapUser.isAutoScreenName()) {
      ScreenNameGenerator screenNameGenerator = ScreenNameGeneratorFactory.getInstance();

      ldapUser.setScreenName(
          screenNameGenerator.generate(companyId, user.getUserId(), ldapUser.getEmailAddress()));
    }

    Calendar birthdayCal = CalendarFactoryUtil.getCalendar();

    birthdayCal.setTime(user.getContact().getBirthday());

    int birthdayMonth = birthdayCal.get(Calendar.MONTH);
    int birthdayDay = birthdayCal.get(Calendar.DAY_OF_MONTH);
    int birthdayYear = birthdayCal.get(Calendar.YEAR);

    if (ldapUser.isUpdatePassword()) {
      UserLocalServiceUtil.updatePassword(
          user.getUserId(), password, password, passwordReset, true);
    }

    Contact contact = user.getContact();

    Set<String> ldapIgnoreAttributes = SetUtil.fromArray(PropsValues.LDAP_USER_IGNORE_ATTRIBUTES);

    for (String attribute : ldapIgnoreAttributes) {
      Object value = BeanPropertiesUtil.getObjectSilent(user, attribute);

      if (value == null) {
        value = BeanPropertiesUtil.getObjectSilent(contact, attribute);
      }

      if (value != null) {
        BeanPropertiesUtil.setProperty(ldapUser, attribute, value);
      }
    }

    updateLDAPUser(ldapUser.getUser(), ldapUser.getContact(), user);

    user =
        UserLocalServiceUtil.updateUser(
            user.getUserId(),
            password,
            StringPool.BLANK,
            StringPool.BLANK,
            passwordReset,
            ldapUser.getReminderQueryQuestion(),
            ldapUser.getReminderQueryAnswer(),
            ldapUser.getScreenName(),
            ldapUser.getEmailAddress(),
            ldapUser.getFacebookId(),
            ldapUser.getOpenId(),
            ldapUser.getLanguageId(),
            ldapUser.getTimeZoneId(),
            ldapUser.getGreeting(),
            ldapUser.getComments(),
            ldapUser.getFirstName(),
            ldapUser.getMiddleName(),
            ldapUser.getLastName(),
            ldapUser.getPrefixId(),
            ldapUser.getSuffixId(),
            ldapUser.isMale(),
            birthdayMonth,
            birthdayDay,
            birthdayYear,
            ldapUser.getSmsSn(),
            ldapUser.getAimSn(),
            ldapUser.getFacebookSn(),
            ldapUser.getIcqSn(),
            ldapUser.getJabberSn(),
            ldapUser.getMsnSn(),
            ldapUser.getMySpaceSn(),
            ldapUser.getSkypeSn(),
            ldapUser.getTwitterSn(),
            ldapUser.getYmSn(),
            ldapUser.getJobTitle(),
            ldapUser.getGroupIds(),
            ldapUser.getOrganizationIds(),
            ldapUser.getRoleIds(),
            ldapUser.getUserGroupRoles(),
            ldapUser.getUserGroupIds(),
            ldapUser.getServiceContext());

    if (ldapUserModifiedDate != null) {
      user = UserLocalServiceUtil.updateModifiedDate(user.getUserId(), ldapUserModifiedDate);
    }

    if (ldapUser.isUpdatePortrait()) {
      byte[] portraitBytes = ldapUser.getPortraitBytes();

      if ((portraitBytes != null) && (portraitBytes.length > 0)) {
        UserLocalServiceUtil.updatePortrait(user.getUserId(), portraitBytes);
      } else {
        UserLocalServiceUtil.deletePortrait(user.getUserId());
      }
    }

    return user;
  }