001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.user;
029
030import org.opencms.db.CmsUserSettings;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsUser;
033import org.opencms.main.CmsException;
034import org.opencms.main.OpenCms;
035import org.opencms.notification.A_CmsNotification;
036import org.opencms.notification.CmsNotificationMacroResolver;
037import org.opencms.ui.login.CmsLoginHelper;
038import org.opencms.util.CmsMacroResolver;
039import org.opencms.workplace.CmsWorkplaceLoginHandler;
040
041/**
042 * Class to send email to user in case of password reset or creating new user.<p>
043 */
044public class CmsSendPasswordNotification extends A_CmsNotification {
045
046    /** Field name. */
047    private static final String FIELD_CHANGE_PASSWORD = "TextChangePassword";
048
049    /** Field name. */
050    private static final String FIELD_KEEP_PASSWORD = "TextKeepPassword";
051
052    /**Is user new? */
053    private boolean m_new;
054
055    /**Is password temporal? */
056    private boolean m_tempPassword;
057
058    /**
059     * Public constructor.<p>
060     *
061     * @param cms CmsObject
062     * @param password password
063     * @param receiver User
064     * @param ou the user OU
065     * @param adminUser User
066     * @param link to login
067     * @param newUser boolean
068     * @param tempPassword <code>true</code> to use a temporary password
069     */
070    public CmsSendPasswordNotification(
071        CmsObject cms,
072        String password,
073        CmsUser receiver,
074        String ou,
075        CmsUser adminUser,
076        String link,
077        boolean newUser,
078        boolean tempPassword) {
079
080        super(cms, receiver);
081        m_new = newUser;
082        m_tempPassword = tempPassword;
083        addMacro("password", password);
084        addMacro(
085            CmsNotificationMacroResolver.WORKPLACE_LOGIN_LINK,
086            "<a href =\""
087                + OpenCms.getLinkManager().getWorkplaceLink(cms, CmsWorkplaceLoginHandler.LOGIN_HANDLER, false)
088                + "?"
089                + CmsLoginHelper.PARAM_USERNAME
090                + "="
091                + receiver.getSimpleName()
092                + "&"
093                + CmsLoginHelper.PARAM_OUFQN
094                + "="
095                + receiver.getOuFqn()
096                + "\">"
097                + OpenCms.getLinkManager().getWorkplaceLink(cms, CmsWorkplaceLoginHandler.LOGIN_HANDLER, false)
098                + "</a>");
099        addMacro(CmsNotificationMacroResolver.RECEIVER_OU_FQN, ou);
100        try {
101            addMacro(
102                CmsNotificationMacroResolver.RECEIVER_OU,
103                OpenCms.getOrgUnitManager().readOrganizationalUnit(cms, ou).getDisplayName(
104                    new CmsUserSettings(receiver).getLocale()));
105
106        } catch (CmsException e) {
107            addMacro(CmsNotificationMacroResolver.RECEIVER_OU, receiver.getOuFqn());
108        }
109
110    }
111
112    /**
113     * @see org.opencms.notification.A_CmsNotification#appendXMLContent(java.lang.StringBuffer)
114     */
115    @Override
116    protected void appendXMLContent(StringBuffer msg) {
117
118        String xmlName = m_tempPassword ? FIELD_CHANGE_PASSWORD : FIELD_KEEP_PASSWORD;
119
120        // append header from xmlcontent
121        msg.append(
122            CmsMacroResolver.resolveMacros(m_mailContent.getStringValue(m_cms, xmlName, m_locale), m_macroResolver));
123
124        msg.append("\n<br/><br/>\n");
125
126        // append footer from xmlcontent
127        msg.append(
128            CmsMacroResolver.resolveMacros(m_mailContent.getStringValue(m_cms, "Footer", m_locale), m_macroResolver));
129
130    }
131
132    /**
133     * @see org.opencms.notification.A_CmsNotification#generateHtmlMsg()
134     */
135    @Override
136    protected String generateHtmlMsg() {
137
138        return "";
139    }
140
141    /**
142     * @see org.opencms.notification.A_CmsNotification#getNotificationContent()
143     */
144    @Override
145    protected String getNotificationContent() {
146
147        if (m_new) {
148            return OpenCms.getSystemInfo().getConfigFilePath(m_cms, "notification/password-new-user-notification");
149        }
150        return OpenCms.getSystemInfo().getConfigFilePath(
151            m_cms,
152            "notification/password-new-password-from-admin-notification");
153    }
154
155}