Search in sources :

Example 1 with SubscriptionSender

use of com.liferay.portal.util.SubscriptionSender in project liferay-ide by liferay.

the class UserLocalServiceImpl method sendEmailAddressVerification.

/**
 * Sends an email address verification to the user.
 *
 * @param  user the verification email recipient
 * @param  emailAddress the recipient's email address
 * @param  serviceContext the service context to be applied. Must set the
 *         portal URL, main path, primary key of the layout, remote address,
 *         remote host, and agent for the user.
 * @throws PortalException if a portal exception occurred
 * @throws SystemException if a system exception occurred
 */
@Override
public void sendEmailAddressVerification(User user, String emailAddress, ServiceContext serviceContext) throws PortalException, SystemException {
    if (user.isEmailAddressVerified() && StringUtil.equalsIgnoreCase(emailAddress, user.getEmailAddress())) {
        return;
    }
    Ticket ticket = ticketLocalService.addTicket(user.getCompanyId(), User.class.getName(), user.getUserId(), TicketConstants.TYPE_EMAIL_ADDRESS, emailAddress, null, serviceContext);
    String verifyEmailAddressURL = serviceContext.getPortalURL() + serviceContext.getPathMain() + "/portal/verify_email_address?ticketKey=" + ticket.getKey();
    long plid = serviceContext.getPlid();
    if (plid > 0) {
        Layout layout = layoutLocalService.fetchLayout(plid);
        if (layout != null) {
            Group group = layout.getGroup();
            if (!layout.isPrivateLayout() && !group.isUser()) {
                verifyEmailAddressURL += "&p_l_id=" + serviceContext.getPlid();
            }
        }
    }
    String fromName = PrefsPropsUtil.getString(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_NAME);
    String fromAddress = PrefsPropsUtil.getString(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
    String toName = user.getFullName();
    String toAddress = emailAddress;
    String subject = PrefsPropsUtil.getContent(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_VERIFICATION_SUBJECT);
    String body = PrefsPropsUtil.getContent(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_VERIFICATION_BODY);
    SubscriptionSender subscriptionSender = new SubscriptionSender();
    subscriptionSender.setBody(body);
    subscriptionSender.setCompanyId(user.getCompanyId());
    subscriptionSender.setContextAttributes("[$EMAIL_VERIFICATION_CODE$]", ticket.getKey(), "[$EMAIL_VERIFICATION_URL$]", verifyEmailAddressURL, "[$REMOTE_ADDRESS$]", serviceContext.getRemoteAddr(), "[$REMOTE_HOST$]", serviceContext.getRemoteHost(), "[$USER_ID$]", user.getUserId(), "[$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) Group(com.liferay.portal.model.Group) UserGroup(com.liferay.portal.model.UserGroup) User(com.liferay.portal.model.User) Layout(com.liferay.portal.model.Layout) SubscriptionSender(com.liferay.portal.util.SubscriptionSender)

Example 2 with SubscriptionSender

use of com.liferay.portal.util.SubscriptionSender in project liferay-ide by liferay.

the class UserLocalServiceImpl method sendEmail.

protected void sendEmail(User user, String password, ServiceContext serviceContext) throws SystemException {
    if (!PrefsPropsUtil.getBoolean(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_ENABLED)) {
        return;
    }
    String fromName = PrefsPropsUtil.getString(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_NAME);
    String fromAddress = PrefsPropsUtil.getString(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
    String toName = user.getFullName();
    String toAddress = user.getEmailAddress();
    String subject = PrefsPropsUtil.getContent(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_SUBJECT);
    String body = null;
    if (Validator.isNotNull(password)) {
        body = PrefsPropsUtil.getContent(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY);
    } else {
        body = PrefsPropsUtil.getContent(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_NO_PASSWORD_BODY);
    }
    SubscriptionSender subscriptionSender = new SubscriptionSender();
    subscriptionSender.setBody(body);
    subscriptionSender.setCompanyId(user.getCompanyId());
    subscriptionSender.setContextAttributes("[$USER_ID$]", user.getUserId(), "[$USER_PASSWORD$]", password, "[$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 : SubscriptionSender(com.liferay.portal.util.SubscriptionSender)

Example 3 with SubscriptionSender

use of com.liferay.portal.util.SubscriptionSender in project liferay-ide by liferay.

the class KBArticleLocalServiceImpl method notifySubscribers.

protected void notifySubscribers(KBArticle kbArticle, ServiceContext serviceContext) throws PortalException, SystemException {
    if (Validator.isNull(serviceContext.getLayoutFullURL())) {
        return;
    }
    PortletPreferences preferences = portletPreferencesLocalService.getPreferences(kbArticle.getCompanyId(), kbArticle.getGroupId(), PortletKeys.PREFS_OWNER_TYPE_GROUP, PortletKeys.PREFS_PLID_SHARED, PortletKeys.KNOWLEDGE_BASE_ADMIN, null);
    if (serviceContext.isCommandAdd() && !AdminUtil.getEmailKBArticleAddedEnabled(preferences)) {
        return;
    }
    if (serviceContext.isCommandUpdate() && !AdminUtil.getEmailKBArticleUpdatedEnabled(preferences)) {
        return;
    }
    String fromName = AdminUtil.getEmailFromName(preferences, kbArticle.getCompanyId());
    String fromAddress = AdminUtil.getEmailFromAddress(preferences, kbArticle.getCompanyId());
    String kbArticleContent = StringUtil.replace(kbArticle.getContent(), new String[] { "href=\"/", "src=\"/" }, new String[] { "href=\"" + serviceContext.getPortalURL() + "/", "src=\"" + serviceContext.getPortalURL() + "/" });
    Map<String, String> kbArticleDiffs = getEmailKBArticleDiffs(kbArticle);
    for (String key : kbArticleDiffs.keySet()) {
        String value = StringUtil.replace(kbArticleDiffs.get(key), new String[] { "href=\"/", "src=\"/" }, new String[] { "href=\"" + serviceContext.getPortalURL() + "/", "src=\"" + serviceContext.getPortalURL() + "/" });
        kbArticleDiffs.put(key, value);
    }
    String subject = null;
    String body = null;
    if (serviceContext.isCommandAdd()) {
        subject = AdminUtil.getEmailKBArticleAddedSubject(preferences);
        body = AdminUtil.getEmailKBArticleAddedBody(preferences);
    } else {
        subject = AdminUtil.getEmailKBArticleUpdatedSubject(preferences);
        body = AdminUtil.getEmailKBArticleUpdatedBody(preferences);
    }
    SubscriptionSender subscriptionSender = new AdminSubscriptionSender(kbArticle, serviceContext);
    subscriptionSender.setBody(body);
    subscriptionSender.setCompanyId(kbArticle.getCompanyId());
    subscriptionSender.setContextAttribute("[$ARTICLE_CONTENT$]", kbArticleContent, false);
    subscriptionSender.setContextAttribute("[$ARTICLE_CONTENT_DIFF$]", kbArticleDiffs.get("content"), false);
    subscriptionSender.setContextAttribute("[$ARTICLE_TITLE$]", kbArticle.getTitle(), false);
    subscriptionSender.setContextAttribute("[$ARTICLE_TITLE_DIFF$]", kbArticleDiffs.get("title"), false);
    subscriptionSender.setContextUserPrefix("ARTICLE");
    subscriptionSender.setFrom(fromAddress, fromName);
    subscriptionSender.setHtmlFormat(true);
    subscriptionSender.setMailId("kb_article", kbArticle.getKbArticleId());
    subscriptionSender.setPortletId(serviceContext.getPortletId());
    subscriptionSender.setReplyToAddress(fromAddress);
    subscriptionSender.setScopeGroupId(kbArticle.getGroupId());
    subscriptionSender.setSubject(subject);
    subscriptionSender.setUserId(kbArticle.getUserId());
    subscriptionSender.addPersistedSubscribers(KBArticle.class.getName(), kbArticle.getGroupId());
    subscriptionSender.addPersistedSubscribers(KBArticle.class.getName(), kbArticle.getResourcePrimKey());
    while (!kbArticle.isRoot() && (kbArticle.getClassNameId() == kbArticle.getParentResourceClassNameId())) {
        kbArticle = getLatestKBArticle(kbArticle.getParentResourcePrimKey(), WorkflowConstants.STATUS_APPROVED);
        subscriptionSender.addPersistedSubscribers(KBArticle.class.getName(), kbArticle.getResourcePrimKey());
    }
    subscriptionSender.flushNotificationsAsync();
}
Also used : KBArticle(com.liferay.knowledgebase.model.KBArticle) AdminSubscriptionSender(com.liferay.knowledgebase.admin.util.AdminSubscriptionSender) PortletPreferences(javax.portlet.PortletPreferences) AdminSubscriptionSender(com.liferay.knowledgebase.admin.util.AdminSubscriptionSender) SubscriptionSender(com.liferay.portal.util.SubscriptionSender)

Example 4 with SubscriptionSender

use of com.liferay.portal.util.SubscriptionSender in project liferay-ide by liferay.

the class KBCommentLocalServiceImpl method notifySubscribers.

protected void notifySubscribers(KBComment kbComment, ServiceContext serviceContext) throws PortalException, SystemException {
    PortletPreferences preferences = portletPreferencesLocalService.getPreferences(kbComment.getCompanyId(), kbComment.getGroupId(), PortletKeys.PREFS_OWNER_TYPE_GROUP, PortletKeys.PREFS_PLID_SHARED, PortletKeys.KNOWLEDGE_BASE_ADMIN, null);
    if (!AdminUtil.isSuggestionStatusChangeNotificationEnabled(kbComment.getStatus(), preferences)) {
        return;
    }
    String fromName = AdminUtil.getEmailFromName(preferences, serviceContext.getCompanyId());
    String fromAddress = AdminUtil.getEmailFromAddress(preferences, kbComment.getCompanyId());
    String subject = AdminUtil.getEmailKBArticleSuggestionNotificationSubject(kbComment.getStatus(), preferences);
    String body = AdminUtil.getEmailKBArticleSuggestionNotificationBody(kbComment.getStatus(), preferences);
    KBArticle kbArticle = kbArticleLocalService.getLatestKBArticle(kbComment.getClassPK(), WorkflowConstants.STATUS_APPROVED);
    String kbArticleContent = StringUtil.replace(kbArticle.getContent(), new String[] { "href=\"/", "src=\"/" }, new String[] { "href=\"" + serviceContext.getPortalURL() + "/", "src=\"" + serviceContext.getPortalURL() + "/" });
    SubscriptionSender subscriptionSender = new AdminSubscriptionSender(kbArticle, serviceContext);
    subscriptionSender.setBody(body);
    subscriptionSender.setCompanyId(kbArticle.getCompanyId());
    subscriptionSender.setContextAttribute("[$ARTICLE_CONTENT$]", kbArticleContent, false);
    subscriptionSender.setContextAttribute("[$COMMENT_CONTENT$]", kbComment.getContent(), false);
    subscriptionSender.setContextAttribute("[$COMMENT_CREATE_DATE$]", getFormattedKBCommentCreateDate(kbComment, serviceContext), false);
    subscriptionSender.setContextUserPrefix("ARTICLE");
    subscriptionSender.setFrom(fromAddress, fromName);
    subscriptionSender.setHtmlFormat(true);
    subscriptionSender.setMailId("kb_article", kbArticle.getKbArticleId());
    subscriptionSender.setPortletId(serviceContext.getPortletId());
    subscriptionSender.setReplyToAddress(fromAddress);
    subscriptionSender.setScopeGroupId(kbArticle.getGroupId());
    subscriptionSender.setSubject(subject);
    subscriptionSender.setUserId(kbArticle.getUserId());
    User user = userLocalService.getUser(kbComment.getUserId());
    subscriptionSender.addRuntimeSubscribers(user.getEmailAddress(), user.getFullName());
    subscriptionSender.flushNotificationsAsync();
}
Also used : User(com.liferay.portal.model.User) KBArticle(com.liferay.knowledgebase.model.KBArticle) AdminSubscriptionSender(com.liferay.knowledgebase.admin.util.AdminSubscriptionSender) PortletPreferences(javax.portlet.PortletPreferences) AdminSubscriptionSender(com.liferay.knowledgebase.admin.util.AdminSubscriptionSender) SubscriptionSender(com.liferay.portal.util.SubscriptionSender)

Example 5 with SubscriptionSender

use of com.liferay.portal.util.SubscriptionSender 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

SubscriptionSender (com.liferay.portal.util.SubscriptionSender)5 User (com.liferay.portal.model.User)3 AdminSubscriptionSender (com.liferay.knowledgebase.admin.util.AdminSubscriptionSender)2 KBArticle (com.liferay.knowledgebase.model.KBArticle)2 Ticket (com.liferay.portal.model.Ticket)2 PortletPreferences (javax.portlet.PortletPreferences)2 DuplicateUserEmailAddressException (com.liferay.portal.DuplicateUserEmailAddressException)1 ReservedUserEmailAddressException (com.liferay.portal.ReservedUserEmailAddressException)1 UserEmailAddressException (com.liferay.portal.UserEmailAddressException)1 StringBundler (com.liferay.portal.kernel.util.StringBundler)1 Company (com.liferay.portal.model.Company)1 Group (com.liferay.portal.model.Group)1 Layout (com.liferay.portal.model.Layout)1 PasswordPolicy (com.liferay.portal.model.PasswordPolicy)1 UserGroup (com.liferay.portal.model.UserGroup)1 RegExpToolkit (com.liferay.portal.security.pwd.RegExpToolkit)1 Date (java.util.Date)1