Search in sources :

Example 1 with RegExpToolkit

use of com.liferay.portal.security.pwd.RegExpToolkit in project liferay-ide by liferay.

the class UserLocalServiceImpl method completeUserRegistration.

/**
 * Completes the user's registration by generating a password and sending
 * the confirmation email.
 *
 * @param  user the user
 * @param  serviceContext the service context to be applied. You can specify
 *         an unencrypted custom password for the user via attribute
 *         <code>passwordUnencrypted</code>. You automatically generate a
 *         password for the user by setting attribute
 *         <code>autoPassword</code> to <code>true</code>. You can send a
 *         confirmation email to the user by setting attribute
 *         <code>sendEmail</code> to <code>true</code>.
 * @throws PortalException if a portal exception occurred
 * @throws SystemException if a system exception occurred
 */
@Override
public void completeUserRegistration(User user, ServiceContext serviceContext) throws PortalException, SystemException {
    boolean autoPassword = ParamUtil.getBoolean(serviceContext, "autoPassword");
    String password = (String) serviceContext.getAttribute("passwordUnencrypted");
    if (autoPassword) {
        if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
            if (_log.isWarnEnabled()) {
                StringBundler sb = new StringBundler(4);
                sb.append("When LDAP password policy is enabled, it is ");
                sb.append("possible that portal generated passwords will ");
                sb.append("not match the LDAP policy. Using ");
                sb.append("RegExpToolkit to generate new password.");
                _log.warn(sb.toString());
            }
            RegExpToolkit regExpToolkit = new RegExpToolkit();
            password = regExpToolkit.generate(null);
        } else {
            PasswordPolicy passwordPolicy = passwordPolicyLocalService.getPasswordPolicy(user.getCompanyId(), user.getOrganizationIds());
            password = PwdToolkitUtil.generate(passwordPolicy);
        }
        serviceContext.setAttribute("passwordUnencrypted", password);
        user.setPassword(PasswordEncryptorUtil.encrypt(password));
        user.setPasswordUnencrypted(password);
        user.setPasswordEncrypted(true);
        user.setPasswordModified(true);
        user.setPasswordModifiedDate(new Date());
        userPersistence.update(user);
        user.setPasswordModified(false);
    }
    if (user.hasCompanyMx()) {
        mailService.addUser(user.getCompanyId(), user.getUserId(), password, user.getFirstName(), user.getMiddleName(), user.getLastName(), user.getEmailAddress());
    }
    boolean sendEmail = ParamUtil.getBoolean(serviceContext, "sendEmail");
    if (sendEmail) {
        sendEmail(user, password, serviceContext);
    }
    Company company = companyPersistence.findByPrimaryKey(user.getCompanyId());
    if (company.isStrangersVerify()) {
        sendEmailAddressVerification(user, user.getEmailAddress(), serviceContext);
    }
}
Also used : Company(com.liferay.portal.model.Company) PasswordPolicy(com.liferay.portal.model.PasswordPolicy) RegExpToolkit(com.liferay.portal.security.pwd.RegExpToolkit) StringBundler(com.liferay.portal.kernel.util.StringBundler) Date(java.util.Date)

Example 2 with RegExpToolkit

use of com.liferay.portal.security.pwd.RegExpToolkit in project liferay-ide by liferay.

the class UserLocalServiceImpl method sendPassword.

/**
 * Sends the password email to the user with the email address. The content
 * of this email can be specified in <code>portal.properties</code> with the
 * <code>admin.email.password</code> keys.
 *
 * @param  companyId the primary key of the user's company
 * @param  emailAddress the user's email address
 * @param  fromName the name of the individual that the email should be from
 * @param  fromAddress the address of the individual that the email should
 *         be from
 * @param  subject the email subject. If <code>null</code>, the subject
 *         specified in <code>portal.properties</code> will be used.
 * @param  body the email body. If <code>null</code>, the body specified in
 *         <code>portal.properties</code> will be used.
 * @param  serviceContext the service context to be applied
 * @throws PortalException if a user with the email address could not be
 *         found
 * @throws SystemException if a system exception occurred
 */
@Override
public void sendPassword(long companyId, String emailAddress, String fromName, String fromAddress, String subject, String body, ServiceContext serviceContext) throws PortalException, SystemException {
    Company company = companyPersistence.findByPrimaryKey(companyId);
    if (!company.isSendPassword() && !company.isSendPasswordResetLink()) {
        return;
    }
    emailAddress = StringUtil.toLowerCase(emailAddress.trim());
    if (Validator.isNull(emailAddress)) {
        throw new UserEmailAddressException();
    }
    User user = userPersistence.findByC_EA(companyId, emailAddress);
    PasswordPolicy passwordPolicy = user.getPasswordPolicy();
    String newPassword = StringPool.BLANK;
    String passwordResetURL = StringPool.BLANK;
    if (company.isSendPasswordResetLink()) {
        Date expirationDate = null;
        if ((passwordPolicy != null) && (passwordPolicy.getResetTicketMaxAge() > 0)) {
            expirationDate = new Date(System.currentTimeMillis() + (passwordPolicy.getResetTicketMaxAge() * 1000));
        }
        Ticket ticket = ticketLocalService.addTicket(companyId, User.class.getName(), user.getUserId(), TicketConstants.TYPE_PASSWORD, null, expirationDate, serviceContext);
        passwordResetURL = serviceContext.getPortalURL() + serviceContext.getPathMain() + "/portal/update_password?p_l_id=" + serviceContext.getPlid() + "&ticketKey=" + ticket.getKey();
    } else {
        if (!PasswordEncryptorUtil.PASSWORDS_ENCRYPTION_ALGORITHM.equals(PasswordEncryptorUtil.TYPE_NONE)) {
            if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
                if (_log.isWarnEnabled()) {
                    StringBundler sb = new StringBundler(5);
                    sb.append("When LDAP password policy is enabled, ");
                    sb.append("it is possible that portal generated ");
                    sb.append("passwords will not match the LDAP policy.");
                    sb.append("Using RegExpToolkit to generate new ");
                    sb.append("password.");
                    _log.warn(sb.toString());
                }
                RegExpToolkit regExpToolkit = new RegExpToolkit();
                newPassword = regExpToolkit.generate(null);
            } else {
                newPassword = PwdToolkitUtil.generate(passwordPolicy);
            }
            boolean passwordReset = false;
            if (passwordPolicy.getChangeable() && passwordPolicy.getChangeRequired()) {
                passwordReset = true;
            }
            user.setPassword(PasswordEncryptorUtil.encrypt(newPassword));
            user.setPasswordUnencrypted(newPassword);
            user.setPasswordEncrypted(true);
            user.setPasswordReset(passwordReset);
            user.setPasswordModified(true);
            user.setPasswordModifiedDate(new Date());
            userPersistence.update(user);
            user.setPasswordModified(false);
        } else {
            newPassword = user.getPassword();
        }
    }
    if (Validator.isNull(fromName)) {
        fromName = PrefsPropsUtil.getString(companyId, PropsKeys.ADMIN_EMAIL_FROM_NAME);
    }
    if (Validator.isNull(fromAddress)) {
        fromAddress = PrefsPropsUtil.getString(companyId, PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
    }
    String toName = user.getFullName();
    String toAddress = user.getEmailAddress();
    if (Validator.isNull(subject)) {
        if (company.isSendPasswordResetLink()) {
            subject = PrefsPropsUtil.getContent(companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_RESET_SUBJECT);
        } else {
            subject = PrefsPropsUtil.getContent(companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_SUBJECT);
        }
    }
    if (Validator.isNull(body)) {
        if (company.isSendPasswordResetLink()) {
            body = PrefsPropsUtil.getContent(companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_RESET_BODY);
        } else {
            body = PrefsPropsUtil.getContent(companyId, PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_BODY);
        }
    }
    SubscriptionSender subscriptionSender = new SubscriptionSender();
    subscriptionSender.setBody(body);
    subscriptionSender.setCompanyId(companyId);
    subscriptionSender.setContextAttributes("[$PASSWORD_RESET_URL$]", passwordResetURL, "[$REMOTE_ADDRESS$]", serviceContext.getRemoteAddr(), "[$REMOTE_HOST$]", serviceContext.getRemoteHost(), "[$USER_ID$]", user.getUserId(), "[$USER_PASSWORD$]", newPassword, "[$USER_SCREENNAME$]", user.getScreenName());
    subscriptionSender.setFrom(fromAddress, fromName);
    subscriptionSender.setHtmlFormat(true);
    subscriptionSender.setMailId("user", user.getUserId(), System.currentTimeMillis(), PwdGenerator.getPassword());
    subscriptionSender.setServiceContext(serviceContext);
    subscriptionSender.setSubject(subject);
    subscriptionSender.setUserId(user.getUserId());
    subscriptionSender.addRuntimeSubscribers(toAddress, toName);
    subscriptionSender.flushNotificationsAsync();
}
Also used : Ticket(com.liferay.portal.model.Ticket) Company(com.liferay.portal.model.Company) User(com.liferay.portal.model.User) UserEmailAddressException(com.liferay.portal.UserEmailAddressException) ReservedUserEmailAddressException(com.liferay.portal.ReservedUserEmailAddressException) DuplicateUserEmailAddressException(com.liferay.portal.DuplicateUserEmailAddressException) PasswordPolicy(com.liferay.portal.model.PasswordPolicy) RegExpToolkit(com.liferay.portal.security.pwd.RegExpToolkit) Date(java.util.Date) StringBundler(com.liferay.portal.kernel.util.StringBundler) SubscriptionSender(com.liferay.portal.util.SubscriptionSender)

Aggregations

StringBundler (com.liferay.portal.kernel.util.StringBundler)2 Company (com.liferay.portal.model.Company)2 PasswordPolicy (com.liferay.portal.model.PasswordPolicy)2 RegExpToolkit (com.liferay.portal.security.pwd.RegExpToolkit)2 Date (java.util.Date)2 DuplicateUserEmailAddressException (com.liferay.portal.DuplicateUserEmailAddressException)1 ReservedUserEmailAddressException (com.liferay.portal.ReservedUserEmailAddressException)1 UserEmailAddressException (com.liferay.portal.UserEmailAddressException)1 Ticket (com.liferay.portal.model.Ticket)1 User (com.liferay.portal.model.User)1 SubscriptionSender (com.liferay.portal.util.SubscriptionSender)1