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.login;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsUser;
032import org.opencms.gwt.CmsVfsService;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.security.CmsOrganizationalUnit;
037import org.opencms.security.CmsUserLog;
038import org.opencms.ui.A_CmsUI;
039import org.opencms.ui.CmsVaadinUtils;
040import org.opencms.ui.I_CmsHasButtons;
041import org.opencms.ui.Messages;
042import org.opencms.util.CmsStringUtil;
043import org.opencms.workplace.CmsWorkplaceLoginHandler;
044
045import java.util.Arrays;
046import java.util.List;
047import java.util.Locale;
048
049import org.apache.commons.logging.Log;
050import org.apache.commons.mail.EmailException;
051
052import com.vaadin.ui.Button;
053import com.vaadin.ui.Button.ClickEvent;
054import com.vaadin.ui.Notification;
055import com.vaadin.ui.Notification.Type;
056import com.vaadin.v7.data.Validator.InvalidValueException;
057import com.vaadin.v7.data.validator.EmailValidator;
058import com.vaadin.v7.ui.AbstractField;
059import com.vaadin.v7.ui.TextField;
060import com.vaadin.v7.ui.VerticalLayout;
061
062/**
063 * Dialog to request a password reset link if you forgot your passsword.<p>
064 */
065public class CmsForgotPasswordDialog extends VerticalLayout implements I_CmsHasButtons {
066
067    /** The logger instance for this class. */
068    private static final Log LOG = CmsLog.getLog(CmsForgotPasswordDialog.class);
069
070    /** Serial version id. */
071    private static final long serialVersionUID = 1L;
072
073    /** Field for the email address. */
074    protected TextField m_emailField;
075
076    /** The user field. */
077    protected TextField m_userField;
078
079    /** The OU selector. */
080    protected CmsLoginOuSelector m_ouSelect;
081
082    /** Button to request the mail with the password reset link .*/
083    protected Button m_mailButton;
084
085    /** Button to cancel. */
086    protected Button m_cancelButton;
087
088    /**
089     * Creates a new instance.<p>
090     *
091     * @param startOu the organizational unit that should be preselected
092     */
093    public CmsForgotPasswordDialog(String startOu) {
094
095        Locale locale = A_CmsUI.get().getLocale();
096        CmsVaadinUtils.readAndLocalizeDesign(this, OpenCms.getWorkplaceManager().getMessages(locale), null);
097        List<CmsOrganizationalUnit> ouList = CmsLoginHelper.getOrgUnitsForLoginDialog(A_CmsUI.getCmsObject(), null);
098        m_ouSelect.initOrgUnits(ouList, false);
099        if ((startOu != null) && m_ouSelect.hasOrgUnit(startOu)) {
100            m_ouSelect.setValue(startOu);
101        }
102        String notEmptyMessage = CmsVaadinUtils.getMessageText(Messages.GUI_VALIDATION_FIELD_EMPTY_0);
103        m_userField.setRequired(true);
104        m_userField.setRequiredError(notEmptyMessage);
105        m_emailField.setRequired(true);
106        m_emailField.setRequiredError(notEmptyMessage);
107        m_emailField.addValidator(
108            new EmailValidator(
109                Messages.get().getBundle(A_CmsUI.get().getLocale()).key(Messages.GUI_PWCHANGE_INVALID_EMAIL_0)));
110        m_cancelButton.addClickListener(new Button.ClickListener() {
111
112            /**Serial version id. */
113            private static final long serialVersionUID = 1L;
114
115            public void buttonClick(ClickEvent event) {
116
117                cancel();
118            }
119        });
120
121        m_mailButton.addClickListener(new Button.ClickListener() {
122
123            /** Serial version id. */
124            private static final long serialVersionUID = 1L;
125
126            public void buttonClick(ClickEvent event) {
127
128                boolean valid = true;
129                for (AbstractField<?> field : Arrays.asList(m_userField, m_emailField)) {
130                    try {
131                        field.validate();
132                    } catch (InvalidValueException e) {
133                        valid = false;
134                    }
135                }
136                if (!valid) {
137                    return;
138                }
139                String selectedOu = getSelectedOrgUnit();
140                String fullName = CmsStringUtil.joinPaths(selectedOu, m_userField.getValue());
141                if (sendPasswordResetLink(CmsLoginUI.m_adminCms, fullName, m_emailField.getValue())) {
142                    CmsUserLog.logPasswordResetRequest(A_CmsUI.getCmsObject(), fullName);
143                    // Since we need to actually go to a different page here, we can't use a Vaadin notification,
144                    // because we don't get notified on the server when the user clicks it.
145                    CmsVaadinUtils.showAlert(
146                        Messages.get().getBundle(A_CmsUI.get().getLocale()).key(
147                            Messages.GUI_PWCHANGE_MAILSENT_HEADER_0),
148                        Messages.get().getBundle(A_CmsUI.get().getLocale()).key(
149                            Messages.GUI_PWCHANGE_MAILSENT_MESSAGE_0),
150                        new Runnable() {
151
152                            public void run() {
153
154                                A_CmsUI.get().getPage().setLocation(
155                                    OpenCms.getLinkManager().substituteLinkForUnknownTarget(
156                                        CmsLoginUI.m_adminCms,
157                                        CmsWorkplaceLoginHandler.LOGIN_HANDLER,
158                                        false));
159
160                            }
161
162                        });
163                }
164            }
165
166        });
167
168    }
169
170    /**
171     * Tries to find a user with the given email address, and if one is found, sends a mail with the password reset link to them.<p>
172     *
173     * @param cms the CMS Context
174     * @param fullUserName the full user name including OU
175     * @param email the email address entered by the user
176     * @return true if the mail could be sent
177     */
178    public static boolean sendPasswordResetLink(CmsObject cms, String fullUserName, String email) {
179
180        LOG.info("Trying to find user for email " + email);
181        email = email.trim();
182
183        try {
184            CmsUser foundUser = null;
185            try {
186                foundUser = cms.readUser(fullUserName);
187            } catch (CmsException e) {
188                LOG.error(e.getLocalizedMessage(), e);
189            }
190            if ((foundUser == null)
191                || CmsStringUtil.isEmptyOrWhitespaceOnly(email)
192                || !email.equalsIgnoreCase(foundUser.getEmail())
193                || foundUser.isManaged()
194                || foundUser.isWebuser()) {
195                Notification.show(
196                    CmsVaadinUtils.getMessageText(Messages.GUI_PWCHANGE_EMAIL_MISMATCH_0),
197                    Type.ERROR_MESSAGE);
198                return false;
199            }
200            long now = System.currentTimeMillis();
201            long expiration = OpenCms.getLoginManager().getTokenLifetime() + now;
202            String expirationStr = CmsVfsService.formatDateTime(cms, expiration);
203            String token = CmsTokenValidator.createToken(cms, foundUser, now);
204            String link = OpenCms.getLinkManager().getWorkplaceLink(cms, CmsWorkplaceLoginHandler.LOGIN_HANDLER, false)
205                + "?at="
206                + token;
207            LOG.info("Sending password reset link to user " + foundUser.getName() + ": " + link);
208            CmsPasswordChangeNotification notification = new CmsPasswordChangeNotification(
209                cms,
210                foundUser,
211                link,
212                expirationStr);
213            try {
214                notification.send();
215            } catch (EmailException e) {
216                Notification.show(
217                    Messages.get().getBundle(A_CmsUI.get().getLocale()).key(Messages.GUI_PWCHANGE_MAIL_SEND_ERROR_0),
218                    Type.ERROR_MESSAGE);
219                LOG.error(e.getLocalizedMessage(), e);
220                return false;
221            }
222
223        } catch (Exception e) {
224            LOG.error(e.getLocalizedMessage(), e);
225            Notification.show(e.getLocalizedMessage(), Type.ERROR_MESSAGE);
226            return false;
227        }
228        return true;
229    }
230
231    /**
232     * Cancels the dialog.<p>
233     */
234    public void cancel() {
235
236        CmsObject cms = A_CmsUI.getCmsObject();
237        String link = OpenCms.getLinkManager().substituteLinkForUnknownTarget(
238            cms,
239            CmsWorkplaceLoginHandler.LOGIN_HANDLER,
240            false);
241        A_CmsUI.get().getPage().setLocation(link);
242    }
243
244    /**
245     * @see org.opencms.ui.I_CmsHasButtons#getButtons()
246     */
247    public List<Button> getButtons() {
248
249        return Arrays.asList(m_mailButton, m_cancelButton);
250    }
251
252    /**
253     * Gets the selected organizational unit.
254     *
255     * @return the selected organizational unit
256     */
257    private String getSelectedOrgUnit() {
258
259        String selectedOu = m_ouSelect.getValue();
260        selectedOu = (selectedOu != null) ? selectedOu : "";
261        return selectedOu;
262    }
263
264}