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.db.CmsUserSettings;
031import org.opencms.file.CmsGroup;
032import org.opencms.file.CmsObject;
033import org.opencms.file.CmsProject;
034import org.opencms.file.CmsResource;
035import org.opencms.file.CmsUser;
036import org.opencms.i18n.CmsLocaleManager;
037import org.opencms.jsp.CmsJspActionElement;
038import org.opencms.main.CmsException;
039import org.opencms.main.CmsIllegalArgumentException;
040import org.opencms.main.CmsLog;
041import org.opencms.main.OpenCms;
042import org.opencms.security.CmsOrganizationalUnit;
043import org.opencms.security.CmsPasswordInfo;
044import org.opencms.security.CmsRole;
045import org.opencms.security.CmsUserLog;
046import org.opencms.site.CmsSite;
047import org.opencms.util.CmsStringUtil;
048import org.opencms.util.CmsUUID;
049import org.opencms.widgets.CmsCheckboxWidget;
050import org.opencms.widgets.CmsDisplayWidget;
051import org.opencms.widgets.CmsGroupWidget;
052import org.opencms.widgets.CmsInputWidget;
053import org.opencms.widgets.CmsPasswordWidget;
054import org.opencms.widgets.CmsSelectWidget;
055import org.opencms.widgets.CmsSelectWidgetOption;
056import org.opencms.widgets.CmsTextareaWidget;
057import org.opencms.widgets.CmsVfsFileWidget;
058import org.opencms.workplace.CmsDialog;
059import org.opencms.workplace.CmsWidgetDialog;
060import org.opencms.workplace.CmsWidgetDialogParameter;
061import org.opencms.workplace.CmsWorkplaceSettings;
062import org.opencms.workplace.CmsWorkplaceView;
063import org.opencms.workplace.tools.CmsToolManager;
064
065import java.util.ArrayList;
066import java.util.HashMap;
067import java.util.Iterator;
068import java.util.List;
069import java.util.Locale;
070import java.util.Map;
071
072import javax.servlet.http.HttpServletRequest;
073
074import org.apache.commons.logging.Log;
075
076/**
077 * Dialog to edit new or existing user in the administration view.<p>
078 *
079 * @since 6.0.0
080 */
081public abstract class A_CmsEditUserDialog extends CmsWidgetDialog {
082
083    /** localized messages Keys prefix. */
084    public static final String KEY_PREFIX = "user";
085
086    /** Defines which pages are valid for this dialog. */
087    public static final String[] PAGES = {"page1"};
088
089    /** Request parameter name for the user id. */
090    public static final String PARAM_USERID = "userid";
091
092    /** The log object for this class. */
093    private static final Log LOG = CmsLog.getLog(A_CmsEditUserDialog.class);
094
095    /** Session parameter name for the pwd info object. */
096    private static final String PWD_OBJECT = "PWD_INFO";
097
098    /** Session parameter name for the user object. */
099    private static final String USER_OBJECT = "USER";
100
101    /** The no role value. */
102    private static final String NO_ROLE = "NO_ROLE";
103
104    /** The user object that is edited on this dialog. */
105    protected CmsUser m_user;
106
107    /** The starting group for the new user. */
108    private String m_group;
109
110    /** The default language for the new user. */
111    private String m_language;
112
113    /** Stores the value of the request parameter for the organizational unit fqn. */
114    private String m_paramOufqn;
115
116    /** Stores the value of the request parameter for the user id. */
117    private String m_paramUserid;
118
119    /** The password information object. */
120    private CmsPasswordInfo m_pwdInfo;
121
122    /** The starting site for the new user. */
123    private String m_site;
124
125    /** The start folder. */
126    private String m_startFolder;
127
128    /** The starting project for the new user. */
129    private String m_startProject;
130
131    /** The start view. */
132    private String m_startView;
133
134    /** The user role. */
135    private String m_role;
136
137    /**
138     * Public constructor with JSP action element.<p>
139     *
140     * @param jsp an initialized JSP action element
141     */
142    public A_CmsEditUserDialog(CmsJspActionElement jsp) {
143
144        super(jsp);
145    }
146
147    /**
148     * Commits the edited user to the db.<p>
149     */
150    @Override
151    public void actionCommit() {
152
153        List<Throwable> errors = new ArrayList<Throwable>();
154
155        try {
156            // if new create it first
157            if (isNewUser()) {
158                // test the group name
159                CmsGroup group = null;
160                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getGroup())) {
161                    group = getCms().readGroup(getGroup());
162                }
163
164                if (group != null) {
165                    if (group.getSimpleName().equals(OpenCms.getDefaultUsers().getGroupAdministrators())) {
166                        // in case the administrators group is selected, the administrator role must be selected also
167                        if (getRole().equals(NO_ROLE)) {
168                            throw new CmsIllegalArgumentException(
169                                Messages.get().container(Messages.ERR_ADD_TO_ADMINISTRATORS_GROUP_0));
170                        }
171                        CmsRole role = CmsRole.valueOfRoleName(getRole());
172                        if (!CmsRole.ROOT_ADMIN.getRoleName().equals(role.getRoleName())
173                            && !CmsRole.ADMINISTRATOR.getRoleName().equals(role.getRoleName())) {
174                            throw new CmsIllegalArgumentException(
175                                Messages.get().container(Messages.ERR_ADD_TO_ADMINISTRATORS_GROUP_0));
176                        }
177                    } else if (group.getSimpleName().equals(OpenCms.getDefaultUsers().getGroupUsers())) {
178                        // in case the users group is selected, the user should have at least one role
179                        if (getRole().equals(NO_ROLE)) {
180                            throw new CmsIllegalArgumentException(
181                                Messages.get().container(Messages.ERR_ADD_TO_USERS_GROUP_0));
182                        }
183                    } else if (group.getSimpleName().equals(OpenCms.getDefaultUsers().getGroupGuests())) {
184                        // in case the users group is selected, the user should have at least one role
185                        if (!getRole().equals(NO_ROLE)) {
186                            throw new CmsIllegalArgumentException(
187                                Messages.get().container(Messages.ERR_ADD_TO_GUESTS_GROUP_0));
188                        }
189                    }
190                }
191                m_pwdInfo.validate();
192                CmsUser newUser = createUser(
193                    m_paramOufqn + m_user.getSimpleName(),
194                    m_pwdInfo.getNewPwd(),
195                    m_user.getDescription(),
196                    m_user.getAdditionalInfo());
197                newUser.setFirstname(m_user.getFirstname());
198                newUser.setLastname(m_user.getLastname());
199                newUser.setEmail(m_user.getEmail());
200                newUser.setAddress(m_user.getAddress());
201                newUser.setManaged(m_user.isManaged());
202                newUser.setEnabled(m_user.isEnabled());
203
204                m_user = newUser;
205            } else if (CmsStringUtil.isNotEmpty(m_pwdInfo.getNewPwd())) {
206                m_pwdInfo.validate();
207                getCms().setPassword(m_user.getName(), m_pwdInfo.getNewPwd());
208                CmsUserLog.logPasswordChange(getCms(), m_user.getName());
209            }
210
211            // write the edited user
212            writeUser(m_user);
213            // set starting membership
214            if (isNewUser()) {
215                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getGroup())) {
216                    getCms().addUserToGroup(m_user.getName(), getGroup());
217                }
218                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getRole()) && !getRole().equals(NO_ROLE)) {
219                    OpenCms.getRoleManager().addUserToRole(
220                        getCms(),
221                        CmsRole.valueOfRoleName(getRole()).forOrgUnit(m_paramOufqn),
222                        m_user.getName());
223                }
224            }
225            // set starting settings
226            CmsUserSettings settings = new CmsUserSettings(m_user);
227            settings.setLocale(CmsLocaleManager.getLocale(getLanguage()));
228            settings.setStartSite(getSite());
229            // set starting project
230            if (isNewUser()) {
231                settings.setStartProject(getParamOufqn() + getStartProject());
232            } else {
233                settings.setStartProject(getStartProject());
234            }
235
236            boolean webuserOu = false;
237            try {
238                webuserOu = OpenCms.getOrgUnitManager().readOrganizationalUnit(
239                    getCms(),
240                    getParamOufqn()).hasFlagWebuser();
241            } catch (CmsException e) {
242                // ignore
243            }
244            // only set the start folder for non web users
245            if (!webuserOu) {
246                CmsObject tmp = OpenCms.initCmsObject(getCms());
247                tmp.getRequestContext().setSiteRoot(getSite());
248                String folder = tmp.getRequestContext().removeSiteRoot(getStartFolder());
249                try {
250                    CmsResource res = tmp.readResource(folder);
251                    String siteRoot = OpenCms.getSiteManager().getSiteRoot(res.getRootPath());
252                    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(siteRoot)
253                        && CmsStringUtil.isNotEmptyOrWhitespaceOnly(tmp.getRequestContext().getSiteRoot())
254                        && !tmp.getRequestContext().getSiteRoot().equals("/")) {
255                        folder = res.getRootPath().substring(siteRoot.length());
256                    }
257                } catch (CmsException e) {
258                    throw new CmsIllegalArgumentException(
259                        Messages.get().container(Messages.ERR_SELECTED_FOLDER_NOT_IN_SITE_0));
260                }
261                settings.setStartFolder(folder);
262            }
263            settings.setStartView(getStartView());
264            settings.save(getCms());
265
266            // refresh the list
267            Map<?, ?> objects = (Map<?, ?>)getSettings().getListObject();
268            if (objects != null) {
269                objects.remove(getListClass());
270            }
271        } catch (Throwable t) {
272            errors.add(t);
273        }
274
275        if (errors.isEmpty() && isNewUser()) {
276            if ((getParamCloseLink() != null) && (getParamCloseLink().indexOf("path=" + getListRootPath()) > -1)) {
277                // set closelink
278                Map<String, String[]> argMap = new HashMap<String, String[]>();
279                argMap.put(PARAM_USERID, new String[] {m_user.getId().toString()});
280                argMap.put("oufqn", new String[] {m_paramOufqn});
281                setParamCloseLink(CmsToolManager.linkForToolPath(getJsp(), getListRootPath() + "/edit", argMap));
282            }
283        }
284        // set the list of errors to display when saving failed
285        setCommitErrors(errors);
286    }
287
288    /**
289     * Returns the description of the parent ou.<p>
290     *
291     * @return the description of the parent ou
292     */
293    public String getAssignedOu() {
294
295        try {
296            CmsOrganizationalUnit ou = OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), getParamOufqn());
297            return ou.getDisplayName(getLocale());
298        } catch (CmsException e) {
299            return null;
300        }
301    }
302
303    /**
304     * Returns the localized description of the user if the description is a key.<p>
305     *
306     * @return the localized description of the user if the description is a key
307     */
308    public String getDescription() {
309
310        return m_user.getDescription(getLocale());
311    }
312
313    /**
314     * Returns the group.<p>
315     *
316     * @return the group
317     */
318    public String getGroup() {
319
320        return m_group;
321    }
322
323    /**
324     * Returns the language.<p>
325     *
326     * @return the language
327     */
328    public String getLanguage() {
329
330        return m_language;
331    }
332
333    /**
334     * Returns the simple name of the user object.<p>
335     *
336     * @return the simple name of the user object
337     */
338    public String getName() {
339
340        if (m_user.getSimpleName().endsWith(CmsOrganizationalUnit.SEPARATOR)) {
341            return "";
342        }
343        return m_user.getSimpleName();
344    }
345
346    /**
347     * Returns the organizational unit fqn parameter value.<p>
348     *
349     * @return the organizational unit fqn parameter value
350     */
351    public String getParamOufqn() {
352
353        return m_paramOufqn;
354    }
355
356    /**
357     * Returns the user id parameter value.<p>
358     *
359     * @return the user id parameter value
360     */
361    public String getParamUserid() {
362
363        return m_paramUserid;
364    }
365
366    /**
367     * Returns the password information.<p>
368     *
369     * @return the password information
370     */
371    public CmsPasswordInfo getPwdInfo() {
372
373        return m_pwdInfo;
374    }
375
376    /**
377     * Returns the user role.<p>
378     *
379     * @return the user role
380     */
381    public String getRole() {
382
383        return m_role;
384    }
385
386    /**
387     * Returns the site.<p>
388     *
389     * @return the site
390     */
391    public String getSite() {
392
393        return m_site;
394    }
395
396    /**
397     * Returns the startFolder.<p>
398     *
399     * @return the startFolder
400     */
401    public String getStartFolder() {
402
403        return m_startFolder;
404    }
405
406    /**
407     * Returns the project.<p>
408     *
409     * @return the project
410     */
411    public String getStartProject() {
412
413        return m_startProject;
414    }
415
416    /**
417     * Returns the startView.<p>
418     *
419     * @return the startView
420     */
421    public String getStartView() {
422
423        return m_startView;
424    }
425
426    /**
427     * Return if user is enabled.<p>
428     *
429     * @return enabled status
430     */
431    public boolean isEnabled() {
432
433        return m_user.isEnabled();
434    }
435
436    /**
437     * Returns the selfManagement.<p>
438     *
439     * @return the selfManagement
440     */
441    public boolean isSelfManagement() {
442
443        return !m_user.isManaged();
444    }
445
446    /**
447     * This method is only needed for displaying reasons.<p>
448     *
449     * @param assignedOu nothing to do with this parameter
450     */
451    public void setAssignedOu(String assignedOu) {
452
453        // nothing will be done here, just to avoid warnings
454        assignedOu.length();
455    }
456
457    /**
458     * Sets the description for the user.<p>
459     *
460     * @param description the description for the user
461     */
462    public void setDescription(String description) {
463
464        m_user.setDescription(description);
465    }
466
467    /**
468     * Sets if user is enabled.<p>
469     *
470     * @param enabled is the user enabled
471     */
472    public void setEnabled(boolean enabled) {
473
474        m_user.setEnabled(enabled);
475    }
476
477    /**
478     * Sets the group.<p>
479     *
480     * @param group the group to set
481     */
482    public void setGroup(String group) {
483
484        m_group = group;
485    }
486
487    /**
488     * Sets the language.<p>
489     *
490     * @param language the language to set
491     */
492    public void setLanguage(String language) {
493
494        m_language = language;
495    }
496
497    /**
498     * Sets the name of the user object.<p>
499     *
500     * @param name the name of the user object
501     */
502    public void setName(String name) {
503
504        m_user.setName(getParamOufqn() + name);
505    }
506
507    /**
508     * Sets the organizational unit fqn parameter value.<p>
509     *
510     * @param ouFqn the organizational unit fqn parameter value
511     */
512    public void setParamOufqn(String ouFqn) {
513
514        if (ouFqn == null) {
515            ouFqn = "";
516        }
517        m_paramOufqn = ouFqn;
518    }
519
520    /**
521     * Sets the user id parameter value.<p>
522     *
523     * @param userId the user id parameter value
524     */
525    public void setParamUserid(String userId) {
526
527        m_paramUserid = userId;
528    }
529
530    /**
531     * Sets the user role.<p>
532     *
533     * @param role the user role
534     */
535    public void setRole(String role) {
536
537        m_role = role;
538    }
539
540    /**
541     * Sets the selfManagement.<p>
542     *
543     * @param selfManagement the selfManagement to set
544     */
545    public void setSelfManagement(boolean selfManagement) {
546
547        m_user.setManaged(!selfManagement);
548    }
549
550    /**
551     * Sets the site.<p>
552     *
553     * @param site the site to set
554     */
555    public void setSite(String site) {
556
557        m_site = site;
558    }
559
560    /**
561     * Sets the startFolder.<p>
562     *
563     * @param startFolder the startFolder to set
564     */
565    public void setStartFolder(String startFolder) {
566
567        m_startFolder = startFolder;
568    }
569
570    /**
571     * Sets the start project.<p>
572     *
573     * @param startProject the start project to set
574     */
575    public void setStartProject(String startProject) {
576
577        m_startProject = startProject;
578    }
579
580    /**
581     * Sets the startView.<p>
582     *
583     * @param startView the startView to set
584     */
585    public void setStartView(String startView) {
586
587        m_startView = startView;
588    }
589
590    /**
591     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
592     *
593     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
594     *
595     * @param dialog the dialog (page) to get the HTML for
596     * @return the dialog HTML for all defined widgets of the named dialog (page)
597     */
598    @Override
599    protected String createDialogHtml(String dialog) {
600
601        StringBuffer result = new StringBuffer(1024);
602
603        result.append(createWidgetTableStart());
604        // show error header once if there were validation errors
605        result.append(createWidgetErrorHeader());
606
607        if (dialog.equals(PAGES[0])) {
608            boolean webuserOu = false;
609            try {
610                webuserOu = OpenCms.getOrgUnitManager().readOrganizationalUnit(
611                    getCms(),
612                    getParamOufqn()).hasFlagWebuser();
613            } catch (CmsException e) {
614                // ignore
615            }
616            // create the widgets for the first dialog page
617            result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_IDENTIFICATION_BLOCK_0)));
618            result.append(createWidgetTableStart());
619            result.append(createDialogRowsHtml(0, 5));
620            result.append(createWidgetTableEnd());
621            result.append(dialogBlockEnd());
622            result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_ADDRESS_BLOCK_0)));
623            result.append(createWidgetTableStart());
624            result.append(createDialogRowsHtml(6, 10));
625            result.append(createWidgetTableEnd());
626            result.append(dialogBlockEnd());
627            int row = isNewUser() ? 17 : 15;
628            if (!webuserOu) {
629                if (getSites().isEmpty()) {
630                    row -= 1;
631                }
632                result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_SETTINGS_BLOCK_0)));
633                result.append(createWidgetTableStart());
634                result.append(createDialogRowsHtml(11, row));
635                result.append(createWidgetTableEnd());
636                result.append(dialogBlockEnd());
637            } else {
638                row = 10;
639            }
640            row++;
641            result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_AUTHENTIFICATION_BLOCK_0)));
642            result.append(createWidgetTableStart());
643            if (isPwdChangeAllowed(m_user)) {
644                result.append(createDialogRowsHtml(row, row + 3));
645            } else {
646                result.append(createDialogRowsHtml(row, row + 1));
647            }
648            result.append(createWidgetTableEnd());
649            result.append(dialogBlockEnd());
650        }
651
652        result.append(createWidgetTableEnd());
653        return result.toString();
654    }
655
656    /**
657     * Creates a new user.<p>
658     *
659     * @param name the name
660     * @param pwd the password
661     * @param desc the description
662     * @param info the additional information map
663     *
664     * @return the new user
665     *
666     * @throws CmsException if something goes wrong
667     */
668    protected abstract CmsUser createUser(String name, String pwd, String desc, Map<String, Object> info)
669    throws CmsException;
670
671    /**
672     * Creates the list of widgets for this dialog.<p>
673     */
674    @Override
675    protected void defineWidgets() {
676
677        // initialize the user object to use for the dialog
678        initUserObject();
679        boolean webuserOu = false;
680        try {
681            webuserOu = OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), getParamOufqn()).hasFlagWebuser();
682        } catch (CmsException e) {
683            webuserOu = m_user.isWebuser();
684        }
685        setKeyPrefix(KEY_PREFIX);
686
687        // widgets to display
688        if (isNewUser() && isEditable(m_user)) {
689            addWidget(new CmsWidgetDialogParameter(this, "name", PAGES[0], new CmsInputWidget()));
690        } else {
691            addWidget(new CmsWidgetDialogParameter(this, "name", PAGES[0], new CmsDisplayWidget()));
692        }
693        if (isEditable(m_user)) {
694            addWidget(new CmsWidgetDialogParameter(this, "description", "", PAGES[0], new CmsTextareaWidget(), 0, 1));
695            addWidget(new CmsWidgetDialogParameter(m_user, "lastname", PAGES[0], new CmsInputWidget()));
696            addWidget(new CmsWidgetDialogParameter(m_user, "firstname", PAGES[0], new CmsInputWidget()));
697            addWidget(new CmsWidgetDialogParameter(m_user, "email", PAGES[0], new CmsInputWidget()));
698            addWidget(new CmsWidgetDialogParameter(this, "assignedOu", PAGES[0], new CmsDisplayWidget()));
699            addWidget(new CmsWidgetDialogParameter(m_user, "institution", "", PAGES[0], new CmsInputWidget(), 0, 1));
700            addWidget(new CmsWidgetDialogParameter(m_user, "address", "", PAGES[0], new CmsInputWidget(), 0, 1));
701            addWidget(new CmsWidgetDialogParameter(m_user, "zipcode", "", PAGES[0], new CmsInputWidget(), 0, 1));
702            addWidget(new CmsWidgetDialogParameter(m_user, "city", "", PAGES[0], new CmsInputWidget(), 0, 1));
703            addWidget(new CmsWidgetDialogParameter(m_user, "country", "", PAGES[0], new CmsInputWidget(), 0, 1));
704            if (!webuserOu) {
705                addWidget(
706                    new CmsWidgetDialogParameter(this, "language", PAGES[0], new CmsSelectWidget(getLanguages())));
707                if (!getSites().isEmpty()) {
708                    addWidget(new CmsWidgetDialogParameter(this, "site", PAGES[0], new CmsSelectWidget(getSites())));
709                }
710                addWidget(
711                    new CmsWidgetDialogParameter(this, "startProject", PAGES[0], new CmsSelectWidget(getProjects())));
712                addWidget(
713                    new CmsWidgetDialogParameter(
714                        this,
715                        "startFolder",
716                        PAGES[0],
717                        new CmsVfsFileWidget(true, null, false)));
718                addWidget(new CmsWidgetDialogParameter(this, "startView", PAGES[0], new CmsSelectWidget(getViews())));
719                if (isNewUser()) {
720                    addWidget(
721                        new CmsWidgetDialogParameter(
722                            this,
723                            "group",
724                            PAGES[0],
725                            new CmsGroupWidget(null, null, getParamOufqn())));
726                    addWidget(new CmsWidgetDialogParameter(this, "role", PAGES[0], new CmsSelectWidget(getRoles())));
727                }
728            }
729        } else {
730            addWidget(new CmsWidgetDialogParameter(this, "description", PAGES[0], new CmsDisplayWidget()));
731            addWidget(new CmsWidgetDialogParameter(m_user, "lastname", PAGES[0], new CmsDisplayWidget()));
732            addWidget(new CmsWidgetDialogParameter(m_user, "firstname", PAGES[0], new CmsDisplayWidget()));
733            addWidget(new CmsWidgetDialogParameter(m_user, "email", PAGES[0], new CmsDisplayWidget()));
734            addWidget(new CmsWidgetDialogParameter(this, "assignedOu", PAGES[0], new CmsDisplayWidget()));
735            addWidget(new CmsWidgetDialogParameter(m_user, "institution", PAGES[0], new CmsDisplayWidget()));
736            addWidget(new CmsWidgetDialogParameter(m_user, "address", PAGES[0], new CmsDisplayWidget()));
737            addWidget(new CmsWidgetDialogParameter(m_user, "zipcode", PAGES[0], new CmsDisplayWidget()));
738            addWidget(new CmsWidgetDialogParameter(m_user, "city", PAGES[0], new CmsDisplayWidget()));
739            addWidget(new CmsWidgetDialogParameter(m_user, "country", PAGES[0], new CmsDisplayWidget()));
740            if (!webuserOu) {
741                addWidget(new CmsWidgetDialogParameter(this, "language", PAGES[0], new CmsDisplayWidget()));
742                if (!getSites().isEmpty()) {
743                    addWidget(new CmsWidgetDialogParameter(this, "site", PAGES[0], new CmsDisplayWidget()));
744                }
745                addWidget(new CmsWidgetDialogParameter(this, "startProject", PAGES[0], new CmsDisplayWidget()));
746                addWidget(new CmsWidgetDialogParameter(this, "startFolder", PAGES[0], new CmsDisplayWidget()));
747                addWidget(new CmsWidgetDialogParameter(this, "startView", PAGES[0], new CmsDisplayWidget()));
748            }
749        }
750        addWidget(new CmsWidgetDialogParameter(m_user, "enabled", PAGES[0], new CmsCheckboxWidget()));
751        addWidget(new CmsWidgetDialogParameter(this, "selfManagement", PAGES[0], new CmsCheckboxWidget()));
752        if (isPwdChangeAllowed(m_user)) {
753            addWidget(new CmsWidgetDialogParameter(m_pwdInfo, "newPwd", PAGES[0], new CmsPasswordWidget()));
754            addWidget(new CmsWidgetDialogParameter(m_pwdInfo, "confirmation", PAGES[0], new CmsPasswordWidget()));
755        }
756    }
757
758    /**
759     * Returns the dialog class name of the list to refresh.<p>
760     *
761     * @return the list dialog class name
762     */
763    protected abstract String getListClass();
764
765    /**
766     * Returns the root path for the list tool.<p>
767     *
768     * @return the root path
769     */
770    protected abstract String getListRootPath();
771
772    /**
773     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
774     */
775    @Override
776    protected String[] getPageArray() {
777
778        return PAGES;
779    }
780
781    /**
782     * @see org.opencms.workplace.CmsWorkplace#initMessages()
783     */
784    @Override
785    protected void initMessages() {
786
787        // add specific dialog resource bundle
788        addMessages(Messages.get().getBundleName());
789        // add default resource bundles
790        super.initMessages();
791    }
792
793    /**
794     * Initializes the user object to work with depending on the dialog state and request parameters.<p>
795     *
796     * Two initializations of the user object on first dialog call are possible:
797     * <ul>
798     * <li>edit an existing user</li>
799     * <li>create a new user</li>
800     * </ul>
801     */
802    protected void initUserObject() {
803
804        Object o = null;
805        try {
806            if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
807                // edit an existing user, get the user object from db
808                m_user = getCms().readUser(new CmsUUID(getParamUserid()));
809                m_pwdInfo = new CmsPasswordInfo();
810                CmsUserSettings settings = new CmsUserSettings(m_user);
811                m_language = settings.getLocale().toString();
812                m_site = CmsStringUtil.joinPaths(settings.getStartSite(), "/");
813                m_startProject = settings.getStartProject();
814                m_startFolder = settings.getStartFolder();
815                m_startView = settings.getStartView();
816                return;
817            } else {
818                // this is not the initial call, get the user object from session
819                o = getDialogObject();
820                Map<?, ?> dialogObject = (Map<?, ?>)o;
821                m_user = (CmsUser)dialogObject.get(USER_OBJECT);
822                m_pwdInfo = (CmsPasswordInfo)dialogObject.get(PWD_OBJECT);
823                CmsUserSettings settings = new CmsUserSettings(m_user);
824                m_language = settings.getLocale().toString();
825                m_site = CmsStringUtil.joinPaths(settings.getStartSite(), "/");
826                m_startProject = settings.getStartProject();
827                m_startFolder = settings.getStartFolder();
828                m_startView = settings.getStartView();
829                return;
830            }
831        } catch (Exception e) {
832            // noop
833        }
834        // create a new user object
835        m_user = new CmsUser();
836        m_pwdInfo = new CmsPasswordInfo();
837        m_group = "";
838        try {
839            m_group = getCms().readGroup(getParamOufqn() + OpenCms.getDefaultUsers().getGroupUsers()).getName();
840        } catch (CmsException e) {
841            // ignore
842        }
843        m_language = CmsLocaleManager.getDefaultLocale().toString();
844        m_site = CmsStringUtil.joinPaths(OpenCms.getSiteManager().getDefaultSite().getSiteRoot(), "/");
845        m_startProject = OpenCms.getWorkplaceManager().getDefaultUserSettings().getStartProject();
846        m_startFolder = OpenCms.getWorkplaceManager().getDefaultUserSettings().getStartFolder();
847        m_startView = OpenCms.getWorkplaceManager().getDefaultUserSettings().getStartView();
848    }
849
850    /**
851     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
852     */
853    @Override
854    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
855
856        // initialize parameters and dialog actions in super implementation
857        super.initWorkplaceRequestValues(settings, request);
858
859        // save the current state of the user and pwd (may be changed because of the widget values)
860        Map<String, Object> dialogObject = new HashMap<String, Object>();
861        dialogObject.put(USER_OBJECT, m_user);
862        dialogObject.put(PWD_OBJECT, m_pwdInfo);
863        setDialogObject(dialogObject);
864    }
865
866    /**
867     * Tests if the given user is editable or not.<p>
868     *
869     * Not editable means that the user can only be activated and deactivated.<p>
870     *
871     * @param user the user to test
872     *
873     * @return the editable flag
874     */
875    protected abstract boolean isEditable(CmsUser user);
876
877    /**
878     * Checks if the new user dialog has to be displayed.<p>
879     *
880     * @return <code>true</code> if the new user dialog has to be displayed
881     */
882    protected boolean isNewUser() {
883
884        return getCurrentToolPath().equals(getListRootPath() + "/new");
885    }
886
887    /**
888     * Indicates if the pwd can be edited or not.<p>
889     *
890     * @param user the edited cms user
891     *
892     * @return <code>true</code> if the pwd can be edited
893     */
894    protected boolean isPwdChangeAllowed(CmsUser user) {
895
896        return user.isUser(); // always true, just to avoid warning
897    }
898
899    /**
900     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
901     */
902    @Override
903    protected void validateParamaters() throws Exception {
904
905        OpenCms.getRoleManager().checkRole(getCms(), CmsRole.ACCOUNT_MANAGER.forOrgUnit(getParamOufqn()));
906        if (!isNewUser()) {
907            // test the needed parameters
908            getCms().readUser(new CmsUUID(getParamUserid())).getName();
909        }
910    }
911
912    /**
913     * Writes a user to the db.<p>
914     *
915     * @param user the user to write
916     *
917     * @throws CmsException if something goes wrong
918     */
919    protected abstract void writeUser(CmsUser user) throws CmsException;
920
921    /**
922     * Returns a list of options for the locale selector.<p>
923     *
924     * @return a list of options for the locale selector
925     */
926    private List<CmsSelectWidgetOption> getLanguages() {
927
928        List<CmsSelectWidgetOption> locales = new ArrayList<CmsSelectWidgetOption>();
929
930        Locale defLocale = null;
931        if ((m_user != null) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_user.getName())) {
932            defLocale = new CmsUserSettings(m_user).getLocale();
933        }
934        if (defLocale == null) {
935            defLocale = getCms().getRequestContext().getLocale();
936        }
937
938        Iterator<Locale> itLocales = OpenCms.getLocaleManager().getAvailableLocales().iterator();
939        while (itLocales.hasNext()) {
940            Locale locale = itLocales.next();
941            boolean selected = locale.equals(defLocale);
942            locales.add(
943                new CmsSelectWidgetOption(locale.toString(), selected, locale.getDisplayName(getLocale()), null));
944        }
945        return locales;
946    }
947
948    /**
949     * Returns a list of options for the project selector.<p>
950     *
951     * @return a list of options for the project selector
952     */
953    private List<CmsSelectWidgetOption> getProjects() {
954
955        List<CmsSelectWidgetOption> projects = new ArrayList<CmsSelectWidgetOption>();
956
957        try {
958            String defProject = null;
959            if ((m_user != null) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_user.getName())) {
960                defProject = new CmsUserSettings(m_user).getStartProject();
961            }
962            if (CmsStringUtil.isEmptyOrWhitespaceOnly(defProject)) {
963                defProject = getParamOufqn() + OpenCms.getWorkplaceManager().getDefaultUserSettings().getStartProject();
964            }
965            if (CmsStringUtil.isEmptyOrWhitespaceOnly(defProject)) {
966                defProject = getCms().getRequestContext().getCurrentProject().getName();
967            }
968
969            List<CmsProject> projectsList;
970            projectsList = OpenCms.getOrgUnitManager().getAllAccessibleProjects(getCms(), getParamOufqn(), false);
971
972            Iterator<CmsProject> itProjects = projectsList.iterator();
973
974            while (itProjects.hasNext()) {
975                boolean selected = false;
976                CmsProject project = itProjects.next();
977
978                String projectName = project.getName();
979                if (projectName.equals(defProject)) {
980                    selected = true;
981                }
982                if (isNewUser()) {
983                    projects.add(
984                        new CmsSelectWidgetOption(project.getSimpleName(), selected, project.getSimpleName(), null));
985                } else {
986                    projects.add(new CmsSelectWidgetOption(project.getName(), selected, project.getSimpleName(), null));
987                }
988            }
989
990        } catch (CmsException e) {
991            if (LOG.isErrorEnabled()) {
992                LOG.error(e.getLocalizedMessage(), e);
993            }
994        }
995
996        return projects;
997    }
998
999    /**
1000     * Returns the role select options.<p>
1001     *
1002     * @return the role select options
1003     */
1004    private List<CmsSelectWidgetOption> getRoles() {
1005
1006        List<CmsSelectWidgetOption> roleOptions = new ArrayList<CmsSelectWidgetOption>();
1007        roleOptions.add(
1008            new CmsSelectWidgetOption(
1009                NO_ROLE,
1010                false,
1011                Messages.get().getBundle(getLocale()).key(Messages.GUI_USER_EDITOR_NO_ROLE_0)));
1012        try {
1013            List<CmsRole> roles = new ArrayList<CmsRole>(
1014                OpenCms.getRoleManager().getRoles(getCms(), m_paramOufqn, false));
1015            // ensure the role sorting matches the system roles order
1016            CmsRole.applySystemRoleOrder(roles);
1017            for (CmsRole role : roles) {
1018                roleOptions.add(
1019                    new CmsSelectWidgetOption(
1020                        role.getRoleName(),
1021                        role.getRoleName().equals(CmsRole.ELEMENT_AUTHOR.getRoleName()),
1022                        role.getDisplayName(getCms(), getLocale())));
1023            }
1024        } catch (CmsException e) {
1025            LOG.error(e.getLocalizedMessage(), e);
1026        }
1027        return roleOptions;
1028    }
1029
1030    /**
1031     * Returns a list of options for the site selector.<p>
1032     *
1033     * @return a list of options for the site selector
1034     */
1035    private List<CmsSelectWidgetOption> getSites() {
1036
1037        List<CmsSelectWidgetOption> sites = new ArrayList<CmsSelectWidgetOption>();
1038        List<CmsSite> sitesList = OpenCms.getSiteManager().getAvailableSites(getCms(), true, false, getParamOufqn());
1039
1040        String defSite = null;
1041        if ((m_user != null) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_user.getName())) {
1042            defSite = new CmsUserSettings(m_user).getStartSite();
1043        }
1044        if (defSite == null) {
1045            defSite = getCms().getRequestContext().getSiteRoot();
1046        }
1047        if (!defSite.endsWith("/")) {
1048            defSite += "/";
1049        }
1050
1051        Iterator<CmsSite> itSites = sitesList.iterator();
1052        while (itSites.hasNext()) {
1053            CmsSite site = itSites.next();
1054            String siteRoot = site.getSiteRoot();
1055            if (CmsStringUtil.isEmptyOrWhitespaceOnly(siteRoot)) {
1056                if (sitesList.size() > 1) {
1057                    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_user.getName())) {
1058                        if (!OpenCms.getRoleManager().hasRole(getCms(), m_user.getName(), CmsRole.VFS_MANAGER)) {
1059                            // skip the root site if not accessible
1060                            continue;
1061                        }
1062                    }
1063                }
1064            }
1065            if (!siteRoot.endsWith("/")) {
1066                siteRoot += "/";
1067            }
1068            boolean selected = defSite.equals(siteRoot);
1069            sites.add(new CmsSelectWidgetOption(siteRoot, selected, substituteSiteTitle(site.getTitle()), null));
1070        }
1071        return sites;
1072    }
1073
1074    /**
1075     * Returns a list of options for the view selector.<p>
1076     *
1077     * @return a list of options for the view selector
1078     */
1079    private List<CmsSelectWidgetOption> getViews() {
1080
1081        List<CmsSelectWidgetOption> views = new ArrayList<CmsSelectWidgetOption>();
1082
1083        // determine the default view
1084        String defaultView = null;
1085        if ((m_user != null) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_user.getName())) {
1086            defaultView = new CmsUserSettings(m_user).getStartView();
1087        }
1088        if (CmsStringUtil.isEmptyOrWhitespaceOnly(defaultView)) {
1089            defaultView = VIEW_WORKPLACE;
1090        }
1091
1092        // get the workplace views
1093        List<CmsWorkplaceView> list = new ArrayList<CmsWorkplaceView>(OpenCms.getWorkplaceManager().getViews());
1094
1095        // add the direct edit view to the list
1096        String displayName = getMessages().key(org.opencms.workplace.commons.Messages.GUI_LABEL_DIRECT_EDIT_VIEW_0);
1097        list.add(new CmsWorkplaceView(displayName, VIEW_DIRECT_EDIT, Float.valueOf(100)));
1098
1099        // loop over the views and create a CmsSelectWidgetOption for each readable view
1100        for (CmsWorkplaceView view : list) {
1101            boolean visible = true;
1102            try {
1103                getCms().readResource(view.getUri());
1104            } catch (CmsException e) {
1105                // should usually never happen
1106                if (LOG.isInfoEnabled()) {
1107                    LOG.info(e.getLocalizedMessage());
1108                }
1109                visible = false;
1110            }
1111            if (visible) {
1112                boolean selected = view.getUri().equals(defaultView);
1113                views.add(new CmsSelectWidgetOption(view.getUri(), selected, resolveMacros(view.getKey()), null));
1114            }
1115        }
1116        return views;
1117    }
1118
1119}