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.util.CmsUUID;
033import org.opencms.widgets.CmsDisplayWidget;
034import org.opencms.workplace.CmsWidgetDialog;
035import org.opencms.workplace.CmsWidgetDialogParameter;
036
037import javax.servlet.http.HttpServletRequest;
038import javax.servlet.http.HttpServletResponse;
039import javax.servlet.jsp.PageContext;
040
041/**
042 * Dialog to edit the roles of a user.<p>
043 *
044 * @since 6.5.6
045 */
046public class CmsUserRoleDialog extends CmsWidgetDialog {
047
048    /** localized messages Keys prefix. */
049    public static final String KEY_PREFIX = "user";
050
051    /** Defines which pages are valid for this dialog. */
052    public static final String[] PAGES = {"page1"};
053
054    /** The user object that is edited on this dialog. */
055    protected CmsUser m_user;
056
057    /** Stores the value of the request parameter for the user id. */
058    private String m_paramUserid;
059
060    /**
061     * Public constructor with JSP action element.<p>
062     *
063     * @param jsp an initialized JSP action element
064     */
065    public CmsUserRoleDialog(CmsJspActionElement jsp) {
066
067        super(jsp);
068    }
069
070    /**
071     * Public constructor with JSP variables.<p>
072     *
073     * @param context the JSP page context
074     * @param req the JSP request
075     * @param res the JSP response
076     */
077    public CmsUserRoleDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
078
079        this(new CmsJspActionElement(context, req, res));
080    }
081
082    /**
083     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
084     */
085    @Override
086    public void actionCommit() {
087
088        // noop
089    }
090
091    /**
092     * Returns the simple name of the user object.<p>
093     *
094     * @return the simple name of the user object
095     */
096    public String getName() {
097
098        return m_user.getSimpleName();
099    }
100
101    /**
102     * Returns the user id parameter value.<p>
103     *
104     * @return the user id parameter value
105     */
106    public String getParamUserid() {
107
108        return m_paramUserid;
109    }
110
111    /**
112     * This method is needed only for displaying reasons.<p>
113     *
114     * @param name nothing to do with this parameter
115     */
116    public void setName(String name) {
117
118        // nothing will be done here, just to avoid warnings
119        name.length();
120    }
121
122    /**
123     * Sets the user id parameter value.<p>
124     *
125     * @param userId the user id parameter value
126     */
127    public void setParamUserid(String userId) {
128
129        m_paramUserid = userId;
130    }
131
132    /**
133     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
134     *
135     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
136     *
137     * @param dialog the dialog (page) to get the HTML for
138     * @return the dialog HTML for all defined widgets of the named dialog (page)
139     */
140    @Override
141    protected String createDialogHtml(String dialog) {
142
143        StringBuffer result = new StringBuffer(1024);
144
145        result.append(createWidgetTableStart());
146        // show error header once if there were validation errors
147        result.append(createWidgetErrorHeader());
148        if (dialog.equals(PAGES[0])) {
149            // create the widgets for the first dialog page
150            result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_IDENTIFICATION_BLOCK_0)));
151            result.append(createWidgetTableStart());
152            result.append(createDialogRowsHtml(0, 2));
153            result.append(createWidgetTableEnd());
154            result.append(dialogBlockEnd());
155        }
156
157        result.append(createWidgetTableEnd());
158        return result.toString();
159    }
160
161    /**
162     * @see org.opencms.workplace.CmsWidgetDialog#defaultActionHtmlEnd()
163     */
164    @Override
165    protected String defaultActionHtmlEnd() {
166
167        return "";
168    }
169
170    /**
171     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
172     */
173    @Override
174    protected void defineWidgets() {
175
176        // initialize the user object to use for the dialog
177        initUserObject();
178
179        setKeyPrefix(KEY_PREFIX);
180
181        // widgets to display
182        addWidget(new CmsWidgetDialogParameter(this, "name", PAGES[0], new CmsDisplayWidget()));
183        addWidget(new CmsWidgetDialogParameter(m_user, "lastname", PAGES[0], new CmsDisplayWidget()));
184        addWidget(new CmsWidgetDialogParameter(m_user, "firstname", PAGES[0], new CmsDisplayWidget()));
185    }
186
187    /**
188     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
189     */
190    @Override
191    protected String[] getPageArray() {
192
193        return PAGES;
194    }
195
196    /**
197     * Initializes the user object to work with depending on the dialog state and request parameters.<p>
198     */
199    protected void initUserObject() {
200
201        try {
202            m_user = getCms().readUser(new CmsUUID(getParamUserid()));
203            return;
204        } catch (Exception e) {
205            // noop
206        }
207    }
208
209    /**
210     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
211     */
212    @Override
213    protected void validateParamaters() throws Exception {
214
215        // test the needed parameters
216        getCms().readUser(new CmsUUID(getParamUserid())).getName();
217    }
218}