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.db.CmsLoginMessage;
031import org.opencms.i18n.CmsMessages;
032import org.opencms.main.OpenCms;
033import org.opencms.security.CmsOrganizationalUnit;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.Messages;
036import org.opencms.ui.components.CmsFakeWindow;
037
038import java.util.List;
039import java.util.Locale;
040import java.util.Map;
041
042import com.google.common.collect.Maps;
043import com.vaadin.annotations.DesignRoot;
044import com.vaadin.event.ShortcutAction.KeyCode;
045import com.vaadin.ui.Button;
046import com.vaadin.ui.Button.ClickEvent;
047import com.vaadin.ui.Button.ClickListener;
048import com.vaadin.v7.shared.ui.label.ContentMode;
049import com.vaadin.v7.ui.Label;
050import com.vaadin.v7.ui.OptionGroup;
051import com.vaadin.v7.ui.TextField;
052import com.vaadin.v7.ui.VerticalLayout;
053
054/**
055 * Login form.<p>
056 */
057@DesignRoot
058public class CmsLoginForm extends VerticalLayout {
059
060    /** The private PC type constant. */
061    public static final String PC_TYPE_PRIVATE = "private";
062
063    /** The public PC type constant. */
064    public static final String PC_TYPE_PUBLIC = "public";
065
066    /** Version id. */
067    private static final long serialVersionUID = 1L;
068
069    /** The login controller. */
070    protected CmsLoginController m_controller;
071
072    /** The label showing the copyright information. */
073    private Label m_copyright;
074
075    /** The error label. */
076    private Label m_error;
077
078    /** Button for opening the "forgot password" dialog. */
079    private Button m_forgotPasswordButton;
080
081    /**Fake window. */
082    private CmsFakeWindow m_fakeWindow;
083
084    /** Label showing an optional configurable message.*/
085    private Label m_additionalMessage;
086
087    /** Login button. */
088    private Button m_loginButton;
089
090    /** Button to show / hide advanced options. */
091    private Button m_optionsButton;
092
093    /** Boolean which indicated whether the advanced options are currently visible. */
094    private boolean m_optionsVisible;
095
096    /** Widget for OU selection. */
097    private CmsLoginOuSelector m_ouSelect;
098
099    /** Widget for entering the password. */
100    private TextField m_passwordField;
101
102    /** The security field, which allows the user to choose between a private or public PC. */
103    private OptionGroup m_securityField;
104    /** Widget for entering the user name.  */
105    private TextField m_userField;
106
107    /**
108     * Creates a new instance.<p>
109     *
110     * @param controller the login controller
111     * @param locale the locale to use
112     */
113    public CmsLoginForm(CmsLoginController controller, Locale locale) {
114
115        m_controller = controller;
116        final CmsMessages messages = OpenCms.getWorkplaceManager().getMessages(locale);
117        Map<String, String> macros = Maps.newHashMap();
118        macros.put("showSecure", "" + controller.isShowSecure());
119        String pctype = controller.getPcType();
120        CmsVaadinUtils.readAndLocalizeDesign(this, messages, macros);
121        m_securityField.addItem(PC_TYPE_PUBLIC);
122        m_securityField.addItem(PC_TYPE_PRIVATE);
123        m_securityField.setValue(pctype);
124        m_copyright.setContentMode(ContentMode.HTML);
125        m_copyright.setValue(CmsLoginHelper.getCopyrightHtml(locale));
126        CmsLoginMessage beforeLoginMessage = OpenCms.getLoginManager().getBeforeLoginMessage();
127        if ((beforeLoginMessage != null) && beforeLoginMessage.isEnabled()) {
128            m_additionalMessage.setVisible(true);
129            m_additionalMessage.setContentMode(ContentMode.HTML);
130            m_additionalMessage.setValue(beforeLoginMessage.getMessage());
131        }
132        m_securityField.setItemCaption(
133            PC_TYPE_PRIVATE,
134            messages.key(org.opencms.workplace.Messages.GUI_LOGIN_PCTYPE_PRIVATE_0));
135        m_securityField.setItemCaption(
136            PC_TYPE_PUBLIC,
137            messages.key(org.opencms.workplace.Messages.GUI_LOGIN_PCTYPE_PUBLIC_0));
138        setWidth("600px");
139        m_loginButton.setClickShortcut(KeyCode.ENTER);
140        m_loginButton.addClickListener(new ClickListener() {
141
142            private static final long serialVersionUID = 1L;
143
144            public void buttonClick(ClickEvent event) {
145
146                m_controller.onClickLogin();
147            }
148        });
149        addAttachListener(new AttachListener() {
150
151            private static final long serialVersionUID = 1L;
152
153            @SuppressWarnings("synthetic-access")
154            public void attach(AttachEvent event) {
155
156                m_userField.focus();
157            }
158        });
159
160        ClickListener forgotPasswordListener = new ClickListener() {
161
162            private static final long serialVersionUID = 1L;
163
164            public void buttonClick(ClickEvent event) {
165
166                m_controller.onClickForgotPassword();
167            }
168        };
169
170        m_forgotPasswordButton.addClickListener(forgotPasswordListener);
171
172        m_optionsButton.addClickListener(
173
174            new ClickListener() {
175
176                private static final long serialVersionUID = 1L;
177
178                public void buttonClick(ClickEvent event) {
179
180                    toggleOptionsVisible();
181                }
182
183            });
184        setOptionsVisible(false);
185        m_error.setContentMode(ContentMode.HTML);
186    }
187
188    /**
189     * Gets the OU.<p>
190     *
191     * @return the OU
192     */
193    public String getOrgUnit() {
194
195        return m_ouSelect.getValue();
196    }
197
198    /**
199     * Gets the password.<p>
200     *
201     * @return the password
202     */
203    public String getPassword() {
204
205        return m_passwordField.getValue();
206    }
207
208    /**
209     * Gets the PC type.<p>
210     *
211     * @return the PC type
212     */
213    public String getPcType() {
214
215        return "" + m_securityField.getValue();
216    }
217
218    /**
219     * Gets the user.<p>
220     *
221     * @return the user
222     */
223    public String getUser() {
224
225        return m_userField.getValue();
226    }
227
228    /**
229     * Resets the password field.<p>
230     */
231    public void resetPassword() {
232
233        m_passwordField.clear();
234
235    }
236
237    /**
238     * Selects a specific org unit.<p>
239     *
240     * @param preselectedOu the OU to select
241     */
242    public void selectOrgUnit(String preselectedOu) {
243
244        m_ouSelect.setValue(preselectedOu);
245
246    }
247
248    /**
249     * Sets visibility of 'advanced' options.<p>
250     *
251     * @param optionsVisible true if the options should be shown, false if not
252     */
253    public void setOptionsVisible(boolean optionsVisible) {
254
255        m_optionsVisible = optionsVisible;
256
257        boolean ousVisible = optionsVisible && !m_ouSelect.isAlwaysHidden();
258        m_ouSelect.setVisible(ousVisible);
259        m_forgotPasswordButton.setVisible(optionsVisible);
260        String optionsMessage = CmsVaadinUtils.getMessageText(
261            optionsVisible ? Messages.GUI_LOGIN_OPTIONS_HIDE_0 : Messages.GUI_LOGIN_OPTIONS_SHOW_0);
262        m_optionsButton.setCaption(optionsMessage);
263    }
264
265    /**
266     * Sets the org units available for selection.<p>
267     *
268     * @param ous the ous
269     */
270    public void setSelectableOrgUnits(List<CmsOrganizationalUnit> ous) {
271
272        m_ouSelect.initOrgUnits(ous);
273    }
274
275    /**
276     * Toggles visibility of 'advanced' options.<p>
277     */
278    public void toggleOptionsVisible() {
279
280        setOptionsVisible(!m_optionsVisible);
281    }
282
283    /**
284     * Displays the given login error.<p>
285     *
286     * @param messageHTML the error message
287     */
288    void displayError(String messageHTML) {
289
290        // m_fakeWindow.addStyleName("waggler");
291        m_error.setValue(messageHTML);
292        m_error.setVisible(true);
293        CmsVaadinUtils.waggleMeOnce(m_fakeWindow);
294        //
295        //Add JavaScript code, which adds the wiggle class and removes it after a short time.
296        //        JavaScript.getCurrent().execute(
297        //            "wiggleElement=document.querySelectorAll(\".waggler\")[0];\n"
298        //                + "wiggleElement.className=wiggleElement.className + \" waggle\";\n"
299        //                + "setTimeout(function () {\n"
300        //                + "element=document.querySelectorAll(\".waggle\")[0];\n"
301        //                + "element.className=element.className.replace(/\\bwaggle\\b/g, \"\");"
302        //                + "    }, 1500);");
303    }
304
305}