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.CmsGroup;
031import org.opencms.file.CmsUser;
032import org.opencms.i18n.CmsMessageContainer;
033import org.opencms.jsp.CmsJspActionElement;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsRuntimeException;
036import org.opencms.main.OpenCms;
037import org.opencms.security.CmsRole;
038import org.opencms.util.CmsUUID;
039import org.opencms.workplace.list.CmsListColumnAlignEnum;
040import org.opencms.workplace.list.CmsListColumnDefinition;
041import org.opencms.workplace.list.CmsListDirectAction;
042import org.opencms.workplace.list.CmsListItem;
043import org.opencms.workplace.list.CmsListItemActionIconComparator;
044import org.opencms.workplace.list.CmsListItemDetails;
045import org.opencms.workplace.list.CmsListMetadata;
046import org.opencms.workplace.list.CmsListMultiAction;
047import org.opencms.workplace.list.I_CmsListFormatter;
048
049import java.util.Iterator;
050import java.util.List;
051import java.util.Locale;
052
053import javax.servlet.http.HttpServletRequest;
054import javax.servlet.http.HttpServletResponse;
055import javax.servlet.jsp.PageContext;
056
057/**
058 * User roles overview view.<p>
059 *
060 * @since 6.5.6
061 */
062public class CmsRoleEditList extends A_CmsRolesList {
063
064    /** list action id constant. */
065    public static final String LIST_ACTION_ACTIVATE = "aa";
066
067    /** list action id constant. */
068    public static final String LIST_ACTION_DEACTIVATE = "ac";
069
070    /** list column id constant. */
071    public static final String LIST_COLUMN_ACTIVATE = "ca";
072
073    /** list id constant. */
074    public static final String LIST_ID = "lsre";
075
076    /** list action id constant. */
077    public static final String LIST_MACTION_ACTIVATE = "ma";
078
079    /** list action id constant. */
080    public static final String LIST_MACTION_DEACTIVATE = "mc";
081
082    /** Stores the value of the request parameter for the user id. */
083    private String m_paramUserid;
084
085    /**
086     * Public constructor.<p>
087     *
088     * @param jsp an initialized JSP action element
089     */
090    public CmsRoleEditList(CmsJspActionElement jsp) {
091
092        this(jsp, LIST_ID);
093    }
094
095    /**
096     * Public constructor.<p>
097     *
098     * @param jsp an initialized JSP action element
099     * @param listId the id of the list
100     */
101    public CmsRoleEditList(CmsJspActionElement jsp, String listId) {
102
103        this(jsp, listId, Messages.get().container(Messages.GUI_ROLEEDIT_LIST_NAME_0));
104    }
105
106    /**
107     * Public constructor.<p>
108     *
109     * @param jsp an initialized JSP action element
110     * @param listId the id of the list
111     * @param listName the name of the list
112     */
113    public CmsRoleEditList(CmsJspActionElement jsp, String listId, CmsMessageContainer listName) {
114
115        super(jsp, listId, listName);
116    }
117
118    /**
119     * Public constructor with JSP variables.<p>
120     *
121     * @param context the JSP page context
122     * @param req the JSP request
123     * @param res the JSP response
124     */
125    public CmsRoleEditList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
126
127        this(new CmsJspActionElement(context, req, res));
128    }
129
130    /**
131     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
132     */
133    @Override
134    public void executeListMultiActions() throws CmsRuntimeException {
135
136        if (getParamListAction().equals(LIST_MACTION_ACTIVATE)) {
137            // execute the activate multiaction
138            try {
139                CmsUser user = getCms().readUser(new CmsUUID(getParamUserid()));
140                Iterator<CmsListItem> itItems = getSelectedItems().iterator();
141                while (itItems.hasNext()) {
142                    CmsListItem listItem = itItems.next();
143                    CmsGroup group = getCms().readGroup((String)listItem.get(LIST_COLUMN_GROUP_NAME));
144                    CmsRole role = CmsRole.valueOf(group);
145                    if (!OpenCms.getRoleManager().hasRole(getCms(), user.getName(), role)) {
146                        OpenCms.getRoleManager().addUserToRole(getCms(), role, user.getName());
147                        getCms().writeUser(user);
148                    }
149                }
150            } catch (CmsException e) {
151                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ACTIVATE_SELECTED_USERS_0), e);
152            }
153            // refreshing no needed becaus the activate action does not add/remove rows to the list
154        } else if (getParamListAction().equals(LIST_MACTION_DEACTIVATE)) {
155            // execute the activate multiaction
156            try {
157                CmsUser user = getCms().readUser(new CmsUUID(getParamUserid()));
158                Iterator<CmsListItem> itItems = getSelectedItems().iterator();
159                while (itItems.hasNext()) {
160                    CmsListItem listItem = itItems.next();
161                    CmsGroup group = getCms().readGroup((String)listItem.get(LIST_COLUMN_GROUP_NAME));
162                    CmsRole role = CmsRole.valueOf(group);
163                    if (OpenCms.getRoleManager().hasRole(getCms(), user.getName(), role)) {
164                        OpenCms.getRoleManager().removeUserFromRole(getCms(), role, user.getName());
165                        getCms().writeUser(user);
166                    }
167                }
168            } catch (CmsException e) {
169                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_DEACTIVATE_SELECTED_USERS_0), e);
170            }
171            // refreshing no needed becaus the activate action does not add/remove rows to the list
172        } else {
173            throwListUnsupportedActionException();
174        }
175        listSave();
176    }
177
178    /**
179     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
180     */
181    @Override
182    public void executeListSingleActions() throws CmsRuntimeException {
183
184        String roleName = getSelectedItem().getId();
185        try {
186            CmsRole role = CmsRole.valueOf(getCms().readGroup(roleName));
187            CmsUser user = getCms().readUser(new CmsUUID(getParamUserid()));
188
189            if (getParamListAction().equals(LIST_ACTION_ACTIVATE)) {
190                // execute the activate action
191                try {
192                    OpenCms.getRoleManager().addUserToRole(getCms(), role, user.getName());
193                    getCms().writeUser(user);
194                } catch (CmsException e) {
195                    throw new CmsRuntimeException(
196                        Messages.get().container(Messages.ERR_ACTIVATE_ROLE_2, roleName, user.getName()),
197                        e);
198                }
199            } else if (getParamListAction().equals(LIST_ACTION_DEACTIVATE)) {
200                // execute the activate action
201                if (!OpenCms.getRoleManager().getRolesOfUser(getCms(), user.getName(), "", true, true, true).contains(
202                    role)) {
203                    throw new CmsRuntimeException(
204                        Messages.get().container(Messages.ERR_DEACTIVATE_INDIRECT_ROLE_2, roleName, user.getName()));
205                }
206                try {
207                    OpenCms.getRoleManager().removeUserFromRole(getCms(), role, user.getName());
208                    getCms().writeUser(user);
209                } catch (CmsException e) {
210                    throw new CmsRuntimeException(
211                        Messages.get().container(Messages.ERR_DEACTIVATE_ROLE_2, roleName, user.getName()),
212                        e);
213                }
214            } else {
215                throwListUnsupportedActionException();
216            }
217            listSave();
218        } catch (CmsException e) {
219            // should never happen
220        }
221    }
222
223    /**
224     * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#getIconPath(CmsListItem)
225     */
226    @Override
227    public String getIconPath(CmsListItem item) {
228
229        try {
230            CmsRole role = CmsRole.valueOf(getCms().readGroup((String)item.get(LIST_COLUMN_GROUP_NAME)));
231            if (OpenCms.getRoleManager().hasRole(
232                getCms(),
233                getCms().readUser(new CmsUUID(getParamUserid())).getName(),
234                role)) {
235                return PATH_BUTTONS + "role.png";
236            }
237        } catch (Exception e) {
238            // ignore
239        }
240        return PATH_BUTTONS + "role_inactive.png";
241    }
242
243    /**
244     * Returns the User id parameter value.<p>
245     *
246     * @return the User id parameter value
247     */
248    public String getParamUserid() {
249
250        return m_paramUserid;
251    }
252
253    /**
254     * Sets the User id parameter value.<p>
255     *
256     * @param userid the userid to set
257     */
258    public void setParamUserid(String userid) {
259
260        m_paramUserid = userid;
261    }
262
263    /**
264     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlStart()
265     */
266    @Override
267    protected String defaultActionHtmlStart() {
268
269        return getList().listJs() + dialogContentStart(getParamTitle());
270    }
271
272    /**
273     * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#getRoles()
274     */
275    @Override
276    protected List<CmsRole> getRoles() throws CmsException {
277
278        return OpenCms.getRoleManager().getRolesOfUser(
279            getCms(),
280            getCms().getRequestContext().getCurrentUser().getName(),
281            getParamOufqn(),
282            false,
283            false,
284            true);
285    }
286
287    /**
288     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
289     */
290    @Override
291    protected void setColumns(CmsListMetadata metadata) {
292
293        super.setColumns(metadata);
294
295        // create column for activation/deactivation
296        CmsListColumnDefinition actCol = new CmsListColumnDefinition(LIST_COLUMN_ACTIVATE);
297        actCol.setName(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_COLS_ACTIVATE_0));
298        actCol.setHelpText(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_COLS_ACTIVATE_HELP_0));
299        actCol.setWidth("20");
300        actCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
301        actCol.setListItemComparator(new CmsListItemActionIconComparator());
302
303        // activate action
304        CmsListDirectAction actAction = new CmsListDirectAction(LIST_ACTION_ACTIVATE) {
305
306            /**
307             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
308             */
309            @Override
310            public boolean isVisible() {
311
312                if (getItem() != null) {
313                    String roleName = getItem().getId();
314                    try {
315                        CmsRole role = CmsRole.valueOf(getCms().readGroup(roleName));
316                        if (OpenCms.getRoleManager().hasRole(
317                            getCms(),
318                            getCms().readUser(new CmsUUID(((CmsRoleEditList)getWp()).getParamUserid())).getName(),
319                            role)) {
320                            return false;
321                        }
322                        return true;
323                    } catch (CmsException e) {
324                        return false;
325                    }
326                }
327                return super.isVisible();
328            }
329        };
330        actAction.setName(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_ACTION_ACTIVATE_NAME_0));
331        actAction.setHelpText(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_ACTION_ACTIVATE_HELP_0));
332        actAction.setIconPath(ICON_INACTIVE);
333        actCol.addDirectAction(actAction);
334
335        // deactivate action
336        CmsListDirectAction deactAction = new CmsListDirectAction(LIST_ACTION_DEACTIVATE) {
337
338            /**
339             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
340             */
341            @Override
342            public boolean isVisible() {
343
344                if (getItem() != null) {
345                    String roleName = getItem().getId();
346                    try {
347                        CmsRole role = CmsRole.valueOf(getCms().readGroup(roleName));
348                        if (OpenCms.getRoleManager().hasRole(
349                            getCms(),
350                            getCms().readUser(new CmsUUID(((CmsRoleEditList)getWp()).getParamUserid())).getName(),
351                            role)) {
352                            return true;
353                        }
354                        return false;
355                    } catch (CmsException e) {
356                        return false;
357                    }
358                }
359                return super.isVisible();
360            }
361        };
362        deactAction.setName(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_ACTION_DEACTIVATE_NAME_0));
363        deactAction.setHelpText(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_ACTION_DEACTIVATE_HELP_0));
364        deactAction.setIconPath(ICON_ACTIVE);
365        actCol.addDirectAction(deactAction);
366
367        // add it to the list definition
368        metadata.addColumn(actCol, 1);
369    }
370
371    /**
372     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
373     */
374    @Override
375    protected void setIndependentActions(CmsListMetadata metadata) {
376
377        // add description details
378        CmsListItemDetails descriptionDetails = new CmsListItemDetails(LIST_DETAIL_DESCRIPTION);
379        descriptionDetails.setAtColumn(LIST_COLUMN_NAME);
380        descriptionDetails.setVisible(true);
381        descriptionDetails.setShowActionName(
382            Messages.get().container(Messages.GUI_ROLEEDIT_DETAIL_SHOW_DESCRIPTION_NAME_0));
383        descriptionDetails.setShowActionHelpText(
384            Messages.get().container(Messages.GUI_ROLEEDIT_DETAIL_SHOW_DESCRIPTION_HELP_0));
385        descriptionDetails.setHideActionName(
386            Messages.get().container(Messages.GUI_ROLEEDIT_DETAIL_HIDE_DESCRIPTION_NAME_0));
387        descriptionDetails.setHideActionHelpText(
388            Messages.get().container(Messages.GUI_ROLEEDIT_DETAIL_HIDE_DESCRIPTION_HELP_0));
389        descriptionDetails.setName(Messages.get().container(Messages.GUI_ROLEEDIT_DETAIL_DESCRIPTION_NAME_0));
390        descriptionDetails.setFormatter(new I_CmsListFormatter() {
391
392            /**
393             * @see org.opencms.workplace.list.I_CmsListFormatter#format(java.lang.Object, java.util.Locale)
394             */
395            public String format(Object data, Locale locale) {
396
397                StringBuffer html = new StringBuffer(512);
398                html.append("<table border='0' cellspacing='0' cellpadding='0'>\n");
399                html.append("\t<tr>\n");
400                html.append("\t\t<td style='white-space:normal;' >\n");
401                html.append("\t\t\t");
402                html.append(data == null ? "" : data);
403                html.append("\n");
404                html.append("\t\t</td>\n");
405                html.append("\t</tr>\n");
406                html.append("</table>\n");
407                return html.toString();
408            }
409        });
410        metadata.addItemDetails(descriptionDetails);
411    }
412
413    /**
414     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
415     */
416    @Override
417    protected void setMultiActions(CmsListMetadata metadata) {
418
419        // add the activate role multi action
420        CmsListMultiAction activateRole = new CmsListMultiAction(LIST_MACTION_ACTIVATE);
421        activateRole.setName(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_MACTION_ACTIVATE_NAME_0));
422        activateRole.setHelpText(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_MACTION_ACTIVATE_HELP_0));
423        activateRole.setIconPath(ICON_MULTI_ACTIVATE);
424        metadata.addMultiAction(activateRole);
425
426        // add the deactivate role multi action
427        CmsListMultiAction deactivateRole = new CmsListMultiAction(LIST_MACTION_DEACTIVATE);
428        deactivateRole.setName(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_MACTION_DEACTIVATE_NAME_0));
429        deactivateRole.setHelpText(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_MACTION_DEACTIVATE_HELP_0));
430        deactivateRole.setIconPath(ICON_MULTI_DEACTIVATE);
431        metadata.addMultiAction(deactivateRole);
432    }
433}