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