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.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.security.CmsAuthentificationException;
036import org.opencms.security.CmsUserLog;
037import org.opencms.security.twofactor.CmsSecondFactorInfo;
038import org.opencms.security.twofactor.CmsTwoFactorAuthenticationHandler;
039import org.opencms.ui.A_CmsUI;
040import org.opencms.ui.CmsVaadinUtils;
041import org.opencms.ui.Messages;
042import org.opencms.ui.components.OpenCmsTheme;
043import org.opencms.workplace.CmsWorkplaceLoginHandler;
044
045import java.util.Locale;
046
047import org.apache.commons.logging.Log;
048
049import com.vaadin.server.UserError;
050
051/**
052 * Dialog used to change the password.<p>
053 */
054public class CmsSetPasswordDialog extends CmsChangePasswordDialog {
055
056    /** Logger instance for this class. */
057    private static final Log LOG = CmsLog.getLog(CmsSetPasswordDialog.class);
058
059    /** Serial version id. */
060    private static final long serialVersionUID = 1L;
061
062    /**
063     * Creates a new instance.<p>
064     *
065     * @param cms the CMS context
066     * @param user the user
067     * @param locale the locale
068     */
069    public CmsSetPasswordDialog(final CmsObject cms, CmsUser user, Locale locale) {
070
071        super(cms, user, locale);
072        // hide the old password field, it's not required
073        m_form.hideOldPassword();
074    }
075
076    /**
077     * Submits the password.<p>
078     */
079    @Override
080    void submit() {
081
082        if ((m_user == null) || m_user.isManaged()) {
083            return;
084        }
085        String password1 = m_form.getPassword1();
086        String password2 = m_form.getPassword2();
087        if (validatePasswords(password1, password2)) {
088            maybeCheckSecondFactor((CmsSecondFactorInfo secondFactorInfo) -> {
089                try {
090                    CmsTwoFactorAuthenticationHandler twoFactorHandler = OpenCms.getTwoFactorAuthenticationHandler();
091                    if (twoFactorHandler.needsTwoFactorAuthentication(m_user)
092                        && twoFactorHandler.hasSecondFactor(m_user)) {
093                        if (!twoFactorHandler.verifySecondFactor(m_user, secondFactorInfo)) {
094                            throw new CmsAuthentificationException(
095                                org.opencms.security.Messages.get().container(
096                                    org.opencms.security.Messages.ERR_VERIFICATION_FAILED_1,
097                                    m_user.getName()));
098                        }
099                    }
100                    m_cms.setPassword(m_user.getName(), password1);
101                    CmsUserLog.logPasswordChange(m_cms, m_user.getName());
102                    CmsTokenValidator.clearToken(CmsLoginUI.m_adminCms, m_user);
103                    showSetPasswordSuccess();
104                } catch (CmsException e) {
105                    String error = e.getLocalizedMessage(m_locale);
106                    m_form.setErrorPassword1(new UserError(error), OpenCmsTheme.SECURITY_INVALID);
107                    LOG.debug(e.getLocalizedMessage(), e);
108                } catch (Exception e) {
109                    String error = e.getLocalizedMessage();
110                    m_form.setErrorPassword1(new UserError(error), OpenCmsTheme.SECURITY_INVALID);
111                    LOG.error(e.getLocalizedMessage(), e);
112                }
113            });
114        }
115    }
116
117    /**
118     * Shows that setting the password was successful.
119     */
120    private void showSetPasswordSuccess() {
121
122        CmsVaadinUtils.showAlert(
123            Messages.get().getBundle(A_CmsUI.get().getLocale()).key(Messages.GUI_PWCHANGE_SUCCESS_HEADER_0),
124            Messages.get().getBundle(A_CmsUI.get().getLocale()).key(
125                Messages.GUI_PWCHANGE_GUI_PWCHANGE_SUCCESS_CONTENT_0),
126            new Runnable() {
127
128                public void run() {
129
130                    String target = OpenCms.getLinkManager().substituteLinkForUnknownTarget(
131                        CmsLoginUI.m_adminCms,
132                        CmsWorkplaceLoginHandler.LOGIN_HANDLER,
133                        false);
134                    target += "?" + CmsLoginHelper.PARAM_OUFQN + "=" + m_user.getOuFqn();
135                    A_CmsUI.get().getPage().setLocation(target);
136                }
137            });
138    }
139}