Search in sources :

Example 66 with User

use of com.liferay.portal.model.User 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)

Example 67 with User

use of com.liferay.portal.model.User in project liferay-ide by liferay.

the class UserLocalServiceImpl method updatePassword.

/**
 * Updates the user's password, optionally with tracking and validation of
 * the change.
 *
 * @param  userId the primary key of the user
 * @param  password1 the user's new password
 * @param  password2 the user's new password confirmation
 * @param  passwordReset whether the user should be asked to reset their
 *         password the next time they login
 * @param  silentUpdate whether the password should be updated without being
 *         tracked, or validated. Primarily used for password imports.
 * @return the user
 * @throws PortalException if a user with the primary key could not be found
 * @throws SystemException if a system exception occurred
 */
@Override
public User updatePassword(long userId, String password1, String password2, boolean passwordReset, boolean silentUpdate) throws PortalException, SystemException {
    User user = userPersistence.findByPrimaryKey(userId);
    if (!silentUpdate) {
        validatePassword(user.getCompanyId(), userId, password1, password2);
    }
    String oldEncPwd = user.getPassword();
    if (!user.isPasswordEncrypted()) {
        oldEncPwd = PasswordEncryptorUtil.encrypt(user.getPassword());
    }
    String newEncPwd = PasswordEncryptorUtil.encrypt(password1);
    if (user.hasCompanyMx()) {
        mailService.updatePassword(user.getCompanyId(), userId, password1);
    }
    user.setPassword(newEncPwd);
    user.setPasswordUnencrypted(password1);
    user.setPasswordEncrypted(true);
    user.setPasswordReset(passwordReset);
    user.setPasswordModifiedDate(new Date());
    user.setDigest(StringPool.BLANK);
    user.setGraceLoginCount(0);
    if (!silentUpdate) {
        user.setPasswordModified(true);
    }
    try {
        userPersistence.update(user);
    } catch (ModelListenerException mle) {
        String msg = GetterUtil.getString(mle.getCause().getMessage());
        if (LDAPSettingsUtil.isPasswordPolicyEnabled(user.getCompanyId())) {
            String passwordHistory = PrefsPropsUtil.getString(user.getCompanyId(), PropsKeys.LDAP_ERROR_PASSWORD_HISTORY);
            if (msg.contains(passwordHistory)) {
                throw new UserPasswordException(UserPasswordException.PASSWORD_ALREADY_USED);
            }
        }
        throw new UserPasswordException(UserPasswordException.PASSWORD_INVALID);
    }
    if (!silentUpdate) {
        user.setPasswordModified(false);
        passwordTrackerLocalService.trackPassword(userId, oldEncPwd);
    }
    return user;
}
Also used : ModelListenerException(com.liferay.portal.ModelListenerException) User(com.liferay.portal.model.User) UserPasswordException(com.liferay.portal.UserPasswordException) Date(java.util.Date)

Example 68 with User

use of com.liferay.portal.model.User in project liferay-ide by liferay.

the class UserLocalServiceImpl method updateModifiedDate.

/**
 * Updates the user's modified date.
 *
 * @param  userId the primary key of the user
 * @param  modifiedDate the new modified date
 * @return the user
 * @throws PortalException if a user with the primary key could not be found
 * @throws SystemException if a system exception occurred
 */
@Override
public User updateModifiedDate(long userId, Date modifiedDate) throws PortalException, SystemException {
    User user = userPersistence.findByPrimaryKey(userId);
    user.setModifiedDate(modifiedDate);
    userPersistence.update(user);
    return user;
}
Also used : User(com.liferay.portal.model.User)

Example 69 with User

use of com.liferay.portal.model.User in project liferay-ide by liferay.

the class UserLocalServiceImpl method checkLoginFailureById.

/**
 * Adds a failed login attempt to the user and updates the user's last
 * failed login date.
 *
 * @param  userId the primary key of the user
 * @throws PortalException if a user with the primary key could not be found
 * @throws SystemException if a system exception occurred
 */
@Override
public void checkLoginFailureById(long userId) throws PortalException, SystemException {
    User user = userPersistence.findByPrimaryKey(userId);
    checkLoginFailure(user);
}
Also used : User(com.liferay.portal.model.User)

Example 70 with User

use of com.liferay.portal.model.User in project liferay-ide by liferay.

the class UserLocalServiceImpl method checkLoginFailureByEmailAddress.

/**
 * Adds a failed login attempt to the user with the email address and
 * updates the user's last failed login date.
 *
 * @param  companyId the primary key of the user's company
 * @param  emailAddress the user's email address
 * @throws PortalException if a user with the email address could not be
 *         found
 * @throws SystemException if a system exception occurred
 */
@Override
public void checkLoginFailureByEmailAddress(long companyId, String emailAddress) throws PortalException, SystemException {
    User user = getUserByEmailAddress(companyId, emailAddress);
    checkLoginFailure(user);
}
Also used : User(com.liferay.portal.model.User)

Aggregations

User (com.liferay.portal.model.User)97 Date (java.util.Date)22 SystemException (com.liferay.portal.kernel.exception.SystemException)17 Group (com.liferay.portal.model.Group)15 PortalException (com.liferay.portal.kernel.exception.PortalException)13 Company (com.liferay.portal.model.Company)11 ServiceContext (com.liferay.portal.service.ServiceContext)11 Indexable (com.liferay.portal.kernel.search.Indexable)10 UserGroup (com.liferay.portal.model.UserGroup)10 IOException (java.io.IOException)9 DuplicateUserEmailAddressException (com.liferay.portal.DuplicateUserEmailAddressException)8 EncryptorException (com.liferay.util.EncryptorException)7 Song (org.liferay.jukebox.model.Song)7 Contact (com.liferay.portal.model.Contact)6 PrincipalException (com.liferay.portal.security.auth.PrincipalException)6 Album (org.liferay.jukebox.model.Album)6 DuplicateUserScreenNameException (com.liferay.portal.DuplicateUserScreenNameException)5 GroupFriendlyURLException (com.liferay.portal.GroupFriendlyURLException)5 NoSuchImageException (com.liferay.portal.NoSuchImageException)5 NoSuchOrganizationException (com.liferay.portal.NoSuchOrganizationException)5