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.ui.A_CmsUI;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.Messages;
038import org.opencms.ui.components.OpenCmsTheme;
039import org.opencms.workplace.CmsWorkplaceLoginHandler;
040
041import java.util.Locale;
042
043import org.apache.commons.logging.Log;
044
045import com.vaadin.server.UserError;
046
047/**
048 * Dialog used to change the password.<p>
049 */
050public class CmsSetPasswordDialog extends CmsChangePasswordDialog {
051
052    /** Logger instance for this class. */
053    private static final Log LOG = CmsLog.getLog(CmsSetPasswordDialog.class);
054
055    /** Serial version id. */
056    private static final long serialVersionUID = 1L;
057
058    /**
059     * Creates a new instance.<p>
060     *
061     * @param cms the CMS context
062     * @param user the user
063     * @param locale the locale
064     */
065    public CmsSetPasswordDialog(final CmsObject cms, CmsUser user, Locale locale) {
066        super(cms, user, locale);
067        // hide the old password field, it's not required
068        m_form.hideOldPassword();
069    }
070
071    /**
072     * Submits the password.<p>
073     */
074    @Override
075    void submit() {
076
077        if ((m_user == null) || m_user.isManaged()) {
078            return;
079        }
080        String password1 = m_form.getPassword1();
081        String password2 = m_form.getPassword2();
082        String error = null;
083        if (validatePasswords(password1, password2)) {
084            try {
085                m_cms.setPassword(m_user.getName(), password1);
086                CmsTokenValidator.clearToken(CmsLoginUI.m_adminCms, m_user);
087            } catch (CmsException e) {
088                error = e.getLocalizedMessage(m_locale);
089                LOG.debug(e.getLocalizedMessage(), e);
090            } catch (Exception e) {
091                error = e.getLocalizedMessage();
092                LOG.error(e.getLocalizedMessage(), e);
093            }
094
095            if (error != null) {
096                m_form.setErrorPassword1(new UserError(error), OpenCmsTheme.SECURITY_INVALID);
097            } else {
098                CmsVaadinUtils.showAlert(
099                    Messages.get().getBundle(A_CmsUI.get().getLocale()).key(Messages.GUI_PWCHANGE_SUCCESS_HEADER_0),
100                    Messages.get().getBundle(A_CmsUI.get().getLocale()).key(
101                        Messages.GUI_PWCHANGE_GUI_PWCHANGE_SUCCESS_CONTENT_0),
102                    new Runnable() {
103
104                        public void run() {
105
106                            A_CmsUI.get().getPage().setLocation(
107                                OpenCms.getLinkManager().substituteLinkForUnknownTarget(
108                                    CmsLoginUI.m_adminCms,
109                                    CmsWorkplaceLoginHandler.LOGIN_HANDLER,
110                                    false));
111                        }
112                    });
113            }
114        }
115    }
116}