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 GmbH & Co. KG, 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.workplace.tools.accounts;
029
030import org.opencms.file.CmsUser;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsSessionManager;
034import org.opencms.main.OpenCms;
035import org.opencms.util.CmsUUID;
036import org.opencms.widgets.CmsDisplayWidget;
037import org.opencms.workplace.CmsWidgetDialog;
038import org.opencms.workplace.CmsWidgetDialogParameter;
039import org.opencms.workplace.list.CmsListDateMacroFormatter;
040import org.opencms.workplace.list.I_CmsListFormatter;
041
042import java.util.ArrayList;
043import java.util.Date;
044
045import javax.servlet.http.HttpServletRequest;
046import javax.servlet.http.HttpServletResponse;
047import javax.servlet.jsp.PageContext;
048
049/**
050 * Dialog to edit new or existing user in the administration view.<p>
051 *
052 * @since 6.0.0
053 */
054public class CmsUserOverviewDialog extends CmsWidgetDialog {
055
056    /** localized messages Keys prefix. */
057    public static final String KEY_PREFIX = "user.ov";
058
059    /** Defines which pages are valid for this dialog. */
060    public static final String[] PAGES = {"page1"};
061
062    /** Request parameter name for the user id. */
063    public static final String PARAM_USERID = "userid";
064
065    /** Formatter for the last login property. */
066    private static final I_CmsListFormatter LAST_LOGIN_FORMATTER = CmsListDateMacroFormatter.getDefaultDateFormatter();
067
068    /** The user object that is edited on this dialog. */
069    protected CmsUser m_user;
070
071    /** Stores the value of the request parameter for the user id. */
072    private String m_paramUserid;
073
074    /**
075     * Public constructor with JSP action element.<p>
076     *
077     * @param jsp an initialized JSP action element
078     */
079    public CmsUserOverviewDialog(CmsJspActionElement jsp) {
080
081        super(jsp);
082    }
083
084    /**
085     * Public constructor with JSP variables.<p>
086     *
087     * @param context the JSP page context
088     * @param req the JSP request
089     * @param res the JSP response
090     */
091    public CmsUserOverviewDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
092
093        this(new CmsJspActionElement(context, req, res));
094    }
095
096    /**
097     * Commits the edited user to the db.<p>
098     */
099    @Override
100    public void actionCommit() {
101
102        // no saving is done
103        setCommitErrors(new ArrayList<Throwable>());
104    }
105
106    /**
107     * Calls the switch user method of the SessionManager.<p>
108     *
109     * @return the direct edit patch
110     *
111     * @throws CmsException if something goes wrong
112     */
113    public String actionSwitchUser() throws CmsException {
114
115        try {
116            CmsSessionManager sessionManager = OpenCms.getSessionManager();
117            CmsUser user = getCms().readUser(new CmsUUID(getJsp().getRequest().getParameter("userid")));
118            return sessionManager.switchUser(getCms(), getJsp().getRequest(), user);
119        } catch (CmsException e) {
120            String toolPath = getCurrentToolPath().substring(0, getCurrentToolPath().lastIndexOf("/"));
121            getToolManager().setCurrentToolPath(this, toolPath);
122            throw e;
123        }
124    }
125
126    /**
127     * Returns the description of the parent ou.<p>
128     *
129     * @return the description of the parent ou
130     */
131    public String getAssignedOu() {
132
133        try {
134            return OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), m_user.getOuFqn()).getDisplayName(
135                getLocale());
136        } catch (CmsException e) {
137            return null;
138        }
139    }
140
141    /**
142     * Returns the creation date.<p>
143     *
144     * Auxiliary Property for better representation.<p>
145     *
146     * @return the creation date
147     */
148    public String getCreated() {
149
150        return LAST_LOGIN_FORMATTER.format(new Date(m_user.getDateCreated()), getLocale());
151    }
152
153    /**
154     * Returns the localized description of the user.<p>
155     *
156     * @return the localized description of the user
157     */
158    public String getDescription() {
159
160        return m_user.getDescription(getLocale());
161    }
162
163    /**
164     * Returns the last login.<p>
165     *
166     * Auxiliary Property for better representation.<p>
167     *
168     * @return the last login
169     */
170    public String getLastlogin() {
171
172        return LAST_LOGIN_FORMATTER.format(new Date(m_user.getLastlogin()), getLocale());
173    }
174
175    /**
176     * Returns the simple name of the user object.<p>
177     *
178     * @return the simple name of the user object
179     */
180    public String getName() {
181
182        return m_user.getSimpleName();
183    }
184
185    /**
186     * Returns the user id parameter value.<p>
187     *
188     * @return the user id parameter value
189     */
190    public String getParamUserid() {
191
192        return m_paramUserid;
193    }
194
195    /**
196     * Returns the selfManagement.<p>
197     *
198     * @return the selfManagement
199     */
200    public boolean isSelfManagement() {
201
202        return !m_user.isManaged();
203    }
204
205    /**
206     * Setter for widget definition.<p>
207     *
208     * @param assignedOu the ou description
209     */
210    public void setAssignedOu(String assignedOu) {
211
212        assignedOu.length(); // prevent warning
213    }
214
215    /**
216     * Sets the creation date.<p>
217     *
218     * Auxiliary Property for better representation.<p>
219     *
220     * @param created the creation date to set
221     */
222    public void setCreated(String created) {
223
224        if (created == null) {
225            // just to avoid warnings
226        }
227    }
228
229    /**
230     * Sets the description of the user.<p>
231     *
232     * @param description the user description
233     */
234    public void setDescription(String description) {
235
236        m_user.setDescription(description);
237    }
238
239    /**
240     * Sets the last login.<p>
241     *
242     * Auxiliary Property for better representation.<p>
243     *
244     * @param lastlogin the last login to set
245     */
246    public void setLastlogin(String lastlogin) {
247
248        if (lastlogin == null) {
249            // just to avoid warnings
250        }
251    }
252
253    /**
254     * Sets the name of the user object.<p>
255     *
256     * @param name the name of the user object
257     */
258    public void setName(String name) {
259
260        name.length();
261    }
262
263    /**
264     * Sets the user id parameter value.<p>
265     *
266     * @param userId the user id parameter value
267     */
268    public void setParamUserid(String userId) {
269
270        m_paramUserid = userId;
271    }
272
273    /**
274     * Sets the selfManagement.<p>
275     *
276     * @param selfManagement the selfManagement to set
277     */
278    public void setSelfManagement(boolean selfManagement) {
279
280        m_user.setManaged(!selfManagement);
281    }
282
283    /**
284     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
285     *
286     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
287     *
288     * @param dialog the dialog (page) to get the HTML for
289     * @return the dialog HTML for all defined widgets of the named dialog (page)
290     */
291    @Override
292    protected String createDialogHtml(String dialog) {
293
294        StringBuffer result = new StringBuffer(1024);
295
296        result.append(createWidgetTableStart());
297        // show error header once if there were validation errors
298        result.append(createWidgetErrorHeader());
299
300        int n = (!isOverview() ? 3 : 5);
301        if (dialog.equals(PAGES[0])) {
302            // create the widgets for the first dialog page
303            result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_IDENTIFICATION_BLOCK_0)));
304            result.append(createWidgetTableStart());
305            result.append(createDialogRowsHtml(0, n));
306            result.append(createWidgetTableEnd());
307            result.append(dialogBlockEnd());
308            if (!isOverview()) {
309                result.append(createWidgetTableEnd());
310                return result.toString();
311            }
312            result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_ADDRESS_BLOCK_0)));
313            result.append(createWidgetTableStart());
314            result.append(createDialogRowsHtml(6, 10));
315            result.append(createWidgetTableEnd());
316            result.append(dialogBlockEnd());
317            result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_AUTHENTIFICATION_BLOCK_0)));
318            result.append(createWidgetTableStart());
319            result.append(createDialogRowsHtml(11, 14));
320            result.append(createWidgetTableEnd());
321            result.append(dialogBlockEnd());
322        }
323
324        result.append(createWidgetTableEnd());
325        return result.toString();
326    }
327
328    /**
329     * @see org.opencms.workplace.CmsWidgetDialog#defaultActionHtmlEnd()
330     */
331    @Override
332    protected String defaultActionHtmlEnd() {
333
334        return "";
335    }
336
337    /**
338     * Creates the list of widgets for this dialog.<p>
339     */
340    @Override
341    protected void defineWidgets() {
342
343        // initialize the user object to use for the dialog
344        initUserObject();
345
346        setKeyPrefix(KEY_PREFIX);
347
348        // widgets to display
349        if (isOverview()) {
350            addWidget(new CmsWidgetDialogParameter(this, "name", PAGES[0], new CmsDisplayWidget()));
351            addWidget(new CmsWidgetDialogParameter(this, "description", PAGES[0], new CmsDisplayWidget()));
352            addWidget(new CmsWidgetDialogParameter(m_user, "lastname", PAGES[0], new CmsDisplayWidget()));
353            addWidget(new CmsWidgetDialogParameter(m_user, "firstname", PAGES[0], new CmsDisplayWidget()));
354            addWidget(new CmsWidgetDialogParameter(m_user, "email", PAGES[0], new CmsDisplayWidget()));
355            addWidget(new CmsWidgetDialogParameter(this, "assignedOu", PAGES[0], new CmsDisplayWidget()));
356            addWidget(new CmsWidgetDialogParameter(m_user, "institution", PAGES[0], new CmsDisplayWidget()));
357            addWidget(new CmsWidgetDialogParameter(m_user, "address", PAGES[0], new CmsDisplayWidget()));
358            addWidget(new CmsWidgetDialogParameter(m_user, "zipcode", PAGES[0], new CmsDisplayWidget()));
359            addWidget(new CmsWidgetDialogParameter(m_user, "city", PAGES[0], new CmsDisplayWidget()));
360            addWidget(new CmsWidgetDialogParameter(m_user, "country", PAGES[0], new CmsDisplayWidget()));
361            addWidget(new CmsWidgetDialogParameter(m_user, "enabled", PAGES[0], new CmsDisplayWidget()));
362            addWidget(new CmsWidgetDialogParameter(this, "selfManagement", PAGES[0], new CmsDisplayWidget()));
363            addWidget(new CmsWidgetDialogParameter(this, "lastlogin", PAGES[0], new CmsDisplayWidget()));
364            addWidget(new CmsWidgetDialogParameter(this, "created", PAGES[0], new CmsDisplayWidget()));
365        } else {
366            addWidget(new CmsWidgetDialogParameter(this, "name", PAGES[0], new CmsDisplayWidget()));
367            addWidget(new CmsWidgetDialogParameter(m_user, "lastname", PAGES[0], new CmsDisplayWidget()));
368            addWidget(new CmsWidgetDialogParameter(m_user, "firstname", PAGES[0], new CmsDisplayWidget()));
369            addWidget(new CmsWidgetDialogParameter(this, "assignedOu", PAGES[0], new CmsDisplayWidget()));
370        }
371    }
372
373    /**
374     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
375     */
376    @Override
377    protected String[] getPageArray() {
378
379        return PAGES;
380    }
381
382    /**
383     * @see org.opencms.workplace.CmsWorkplace#initMessages()
384     */
385    @Override
386    protected void initMessages() {
387
388        // add specific dialog resource bundle
389        addMessages(Messages.get().getBundleName());
390        // add default resource bundles
391        super.initMessages();
392    }
393
394    /**
395     * Initializes the user object.<p>
396     */
397    protected void initUserObject() {
398
399        try {
400            // edit an existing user, get the user object from db
401            m_user = getCms().readUser(new CmsUUID(getParamUserid()));
402        } catch (CmsException e) {
403            // should never happen
404        }
405    }
406
407    /**
408     * Overridden to set a custom online help path. <p>
409     *
410     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceMembers(org.opencms.jsp.CmsJspActionElement)
411     */
412    @Override
413    protected void initWorkplaceMembers(CmsJspActionElement jsp) {
414
415        super.initWorkplaceMembers(jsp);
416        setOnlineHelpUriCustom("/accounts/users/overview/");
417    }
418
419    /**
420     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
421     */
422    @Override
423    protected void validateParamaters() throws Exception {
424
425        // test the needed parameters
426        getCms().readUser(new CmsUUID(getParamUserid())).getName();
427    }
428
429    /**
430     * Checks if the User overview has to be displayed.<p>
431     *
432     * @return <code>true</code> if the user overview has to be displayed
433     */
434    private boolean isOverview() {
435
436        return getCurrentToolPath().endsWith("/users/edit");
437    }
438}