Example usage for com.liferay.portal.security.auth ScreenNameGeneratorFactory getInstance

List of usage examples for com.liferay.portal.security.auth ScreenNameGeneratorFactory getInstance

Introduction

In this page you can find the example usage for com.liferay.portal.security.auth ScreenNameGeneratorFactory getInstance.

Prototype

public static ScreenNameGenerator getInstance() 

Source Link

Usage

From source file:com.custom.portal.security.ldap.CustomPortalLDAPImporterImpl.java

License:Open Source License

protected User updateUser(long companyId, CustomLDAPUser ldapUser, User user, Properties userMappings,
        Properties contactMappings, String password, String modifiedDate, boolean isNew) throws Exception {

    Date ldapUserModifiedDate = null;

    boolean passwordReset = ldapUser.isPasswordReset();

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

        passwordReset = user.isPasswordReset();
    }//w  ww  .  j av a  2 s .c  o  m

    try {
        if (Validator.isNotNull(modifiedDate)) {
            ldapUserModifiedDate = LDAPUtil.parseDate(modifiedDate);

            if (ldapUserModifiedDate.equals(user.getModifiedDate())) {
                if (ldapUser.isAutoPassword()) {
                    if (_log.isDebugEnabled()) {
                        _log.debug("Skipping user " + user.getEmailAddress()
                                + " because he is already synchronized");
                    }

                    return user;
                }

                UserLocalServiceUtil.updatePassword(user.getUserId(), password, password, passwordReset, true);

                if (_log.isDebugEnabled()) {
                    _log.debug("User " + user.getEmailAddress() + " is already synchronized, but updated "
                            + "password to avoid a blank value");
                }

                return user;
            }
        } else if (!isNew) {
            if (_log.isInfoEnabled()) {
                _log.info("Skipping user " + user.getEmailAddress()
                        + " because the LDAP entry was never modified");
            }

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

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

        if (StringUtil.equalsIgnoreCase(password, _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();

    Contact ldapContact = ldapUser.getContact();

    birthdayCal.setTime(ldapContact.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);
    }

    updateLDAPUser(ldapUser.getUser(), ldapContact, user, userMappings, contactMappings);

    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());

    boolean hasBusinessPhone = false;

    for (Phone phone : user.getPhones()) {
        if (phone.getTypeId() == 11006) {
            hasBusinessPhone = true;
            break;
        }
    }

    if (!hasBusinessPhone && !ldapUser.getPhone().isEmpty()) {
        if (_log.isDebugEnabled()) {
            _log.debug("Adding Business phone: " + ldapUser.getPhone());
        }
        PhoneLocalServiceUtil.addPhone(user.getUserId(), Contact.class.getName(), user.getContactId(),
                ldapUser.getPhone(), "", 11006, true, ldapUser.getServiceContext());
    }

    boolean hasBusinessAddress = false;

    for (Address address : user.getAddresses()) {
        if (address.getTypeId() == 11000) {
            hasBusinessAddress = true;
            break;
        }
    }

    if (!hasBusinessAddress && !ldapUser.getStreet().isEmpty() && !ldapUser.getCity().isEmpty()) {
        if (_log.isDebugEnabled()) {
            _log.debug("Adding Business Address: " + ldapUser.getStreet() + " " + ldapUser.getCity() + " "
                    + ldapUser.getZip());

        }
        AddressLocalServiceUtil.addAddress(user.getUserId(), Contact.class.getName(), user.getContactId(),
                ldapUser.getStreet(), "", "", ldapUser.getCity(), ldapUser.getZip(), 0, 0, 11000, true, true,
                ldapUser.getServiceContext());
    }

    user = UserLocalServiceUtil.updateStatus(user.getUserId(), ldapUser.getStatus());

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

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

        if (ArrayUtil.isNotEmpty(portraitBytes)) {
            UserLocalServiceUtil.updatePortrait(user.getUserId(), portraitBytes);
        } else {
            UserLocalServiceUtil.deletePortrait(user.getUserId());
        }
    }

    return user;
}