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.i18n.CmsMessageContainer;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsRuntimeException;
035import org.opencms.main.OpenCms;
036import org.opencms.security.CmsRole;
037import org.opencms.workplace.CmsDialog;
038import org.opencms.workplace.list.CmsListDefaultAction;
039import org.opencms.workplace.list.CmsListItem;
040import org.opencms.workplace.list.CmsListItemDetails;
041import org.opencms.workplace.list.CmsListItemDetailsFormatter;
042import org.opencms.workplace.list.CmsListMetadata;
043
044import java.io.IOException;
045import java.util.ArrayList;
046import java.util.HashMap;
047import java.util.Iterator;
048import java.util.List;
049import java.util.Map;
050
051import javax.servlet.ServletException;
052import javax.servlet.http.HttpServletRequest;
053import javax.servlet.http.HttpServletResponse;
054import javax.servlet.jsp.PageContext;
055
056/**
057 * Roles overview view.<p>
058 *
059 * @since 6.5.6
060 */
061public class CmsRolesList extends A_CmsRolesList {
062
063    /** list action id constant. */
064    public static final String LIST_DEFACTION_OVERVIEW = "do";
065
066    /** list item detail id constant. */
067    public static final String LIST_DETAIL_USERS = "du";
068
069    /** list id constant. */
070    public static final String LIST_ID = "lsrs";
071
072    /** Request parameter name for the role name. */
073    public static final String PARAM_ROLE = "role";
074
075    /**
076     * Public constructor.<p>
077     *
078     * @param jsp an initialized JSP action element
079     */
080    public CmsRolesList(CmsJspActionElement jsp) {
081
082        this(jsp, LIST_ID);
083    }
084
085    /**
086     * Public constructor.<p>
087     *
088     * @param jsp an initialized JSP action element
089     * @param listId the id of the list
090     */
091    public CmsRolesList(CmsJspActionElement jsp, String listId) {
092
093        this(jsp, listId, Messages.get().container(Messages.GUI_ROLEEDIT_LIST_NAME_0));
094    }
095
096    /**
097     * Public constructor with JSP variables.<p>
098     *
099     * @param context the JSP page context
100     * @param req the JSP request
101     * @param res the JSP response
102     */
103    public CmsRolesList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
104
105        this(new CmsJspActionElement(context, req, res));
106    }
107
108    /**
109     * Public constructor.<p>
110     *
111     * @param jsp an initialized JSP action element
112     * @param listId the id of the list
113     * @param listName the name of the list
114     */
115    protected CmsRolesList(CmsJspActionElement jsp, String listId, CmsMessageContainer listName) {
116
117        super(jsp, listId, listName);
118    }
119
120    /**
121     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
122     */
123    @Override
124    public void executeListMultiActions() throws CmsRuntimeException {
125
126        throw new UnsupportedOperationException();
127    }
128
129    /**
130     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
131     */
132    @Override
133    public void executeListSingleActions() throws IOException, ServletException, CmsRuntimeException {
134
135        String roleName = getSelectedItem().get(LIST_COLUMN_GROUP_NAME).toString();
136        Map<String, String[]> params = new HashMap<String, String[]>();
137        params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, new String[] {getParamOufqn()});
138        params.put(PARAM_ROLE, new String[] {roleName});
139        params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
140        if (getParamListAction().equals(LIST_ACTION_ICON)) {
141            try {
142                if (OpenCms.getRoleManager().hasRole(getCms(), CmsRole.valueOf(getCms().readGroup(roleName)))) {
143                    // forward to the edit user screen
144                    getToolManager().jspForwardTool(this, getCurrentToolPath() + "/overview/edit", params);
145                } else {
146                    getToolManager().jspForwardTool(this, getCurrentToolPath() + "/overview", params);
147                }
148            } catch (CmsException e) {
149                // noop
150            }
151        } else if (getParamListAction().equals(LIST_DEFACTION_OVERVIEW)) {
152            // forward to the overview screen
153            getToolManager().jspForwardTool(this, getCurrentToolPath() + "/overview", params);
154        } else {
155            throwListUnsupportedActionException();
156        }
157        listSave();
158    }
159
160    /**
161     * Returns the path of the edit icon.<p>
162     *
163     * @return the path of the edit icon
164     */
165    public String getEditIcon() {
166
167        return PATH_BUTTONS + "user.png";
168    }
169
170    /**
171     * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#getIconPath(org.opencms.workplace.list.CmsListItem)
172     */
173    @Override
174    public String getIconPath(CmsListItem item) {
175
176        return PATH_BUTTONS + "role.png";
177    }
178
179    /**
180     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
181     */
182    @Override
183    protected void fillDetails(String detailId) {
184
185        if (!detailId.equals(LIST_DETAIL_USERS)) {
186            super.fillDetails(detailId);
187            return;
188        }
189        // get content
190        List<CmsListItem> roles = getList().getAllContent();
191        Iterator<CmsListItem> itRoles = roles.iterator();
192        while (itRoles.hasNext()) {
193            CmsListItem item = itRoles.next();
194            String roleName = item.get(LIST_COLUMN_GROUP_NAME).toString();
195            StringBuffer html = new StringBuffer(512);
196            try {
197                if (detailId.equals(LIST_DETAIL_USERS)) {
198                    CmsRole role = CmsRole.valueOf(getCms().readGroup(roleName));
199                    List<CmsUser> users = OpenCms.getRoleManager().getUsersOfRole(getCms(), role, true, true);
200                    Iterator<CmsUser> itUsers = users.iterator();
201                    while (itUsers.hasNext()) {
202                        CmsUser user = itUsers.next();
203                        if (user.getOuFqn().equals(getParamOufqn())) {
204                            html.append(user.getSimpleName());
205                        } else {
206                            html.append(user.getDisplayName(getCms(), getLocale()));
207                        }
208                        if (itUsers.hasNext()) {
209                            html.append("<br>");
210                        }
211                        html.append("\n");
212                    }
213                } else {
214                    continue;
215                }
216            } catch (Exception e) {
217                // noop
218            }
219            item.set(detailId, html.toString());
220        }
221    }
222
223    /**
224     * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#getRoles()
225     */
226    @Override
227    protected List<CmsRole> getRoles() throws CmsException {
228
229        List<CmsRole> roles = new ArrayList<CmsRole>(
230            OpenCms.getRoleManager().getRoles(getCms(), getParamOufqn(), false));
231        // ensure the role sorting matches the system roles order
232        CmsRole.applySystemRoleOrder(roles);
233        return roles;
234    }
235
236    /**
237     * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#includeOuDetails()
238     */
239    @Override
240    protected boolean includeOuDetails() {
241
242        return false;
243    }
244
245    /**
246     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
247     */
248    @Override
249    protected void setColumns(CmsListMetadata metadata) {
250
251        super.setColumns(metadata);
252
253        // create default overview action
254        CmsListDefaultAction defOverviewAction = new CmsListDefaultAction(LIST_DEFACTION_OVERVIEW);
255        defOverviewAction.setName(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_DEFACTION_OVERVIEW_NAME_0));
256        defOverviewAction.setHelpText(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_DEFACTION_OVERVIEW_HELP_0));
257        metadata.getColumnDefinition(LIST_COLUMN_NAME).addDefaultAction(defOverviewAction);
258
259        // activate icon action and set a more descriptive help text
260        metadata.getColumnDefinition(LIST_COLUMN_ICON).getDirectAction(LIST_ACTION_ICON).setEnabled(true);
261        metadata.getColumnDefinition(LIST_COLUMN_ICON).setHelpText(
262            Messages.get().container(Messages.GUI_ROLEEDIT_LIST_COLS_EDIT_HELP_0));
263    }
264
265    /**
266     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
267     */
268    @Override
269    protected void setIndependentActions(CmsListMetadata metadata) {
270
271        super.setIndependentActions(metadata);
272
273        // add users details
274        CmsListItemDetails usersDetails = new CmsListItemDetails(LIST_DETAIL_USERS);
275        usersDetails.setAtColumn(LIST_COLUMN_NAME);
276        usersDetails.setVisible(false);
277        usersDetails.setShowActionName(Messages.get().container(Messages.GUI_ROLES_DETAIL_SHOW_USERS_NAME_0));
278        usersDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_ROLES_DETAIL_SHOW_USERS_HELP_0));
279        usersDetails.setHideActionName(Messages.get().container(Messages.GUI_ROLES_DETAIL_HIDE_USERS_NAME_0));
280        usersDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_ROLES_DETAIL_HIDE_USERS_HELP_0));
281        usersDetails.setName(Messages.get().container(Messages.GUI_ROLES_DETAIL_USERS_NAME_0));
282        usersDetails.setFormatter(
283            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_ROLES_DETAIL_USERS_NAME_0)));
284        metadata.addItemDetails(usersDetails);
285    }
286
287    /**
288     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
289     */
290    @Override
291    protected void setMultiActions(CmsListMetadata metadata) {
292
293        // noop
294    }
295}