001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.CmsResource;
032import org.opencms.file.CmsUser;
033import org.opencms.i18n.CmsMessageContainer;
034import org.opencms.jsp.CmsJspActionElement;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsRuntimeException;
037import org.opencms.main.OpenCms;
038import org.opencms.security.CmsOrganizationalUnit;
039import org.opencms.security.CmsRole;
040import org.opencms.workplace.CmsDialog;
041import org.opencms.workplace.list.A_CmsListDialog;
042import org.opencms.workplace.list.CmsListColumnAlignEnum;
043import org.opencms.workplace.list.CmsListColumnDefinition;
044import org.opencms.workplace.list.CmsListDefaultAction;
045import org.opencms.workplace.list.CmsListDirectAction;
046import org.opencms.workplace.list.CmsListItem;
047import org.opencms.workplace.list.CmsListItemDetails;
048import org.opencms.workplace.list.CmsListItemDetailsFormatter;
049import org.opencms.workplace.list.CmsListMetadata;
050import org.opencms.workplace.list.CmsListOrderEnum;
051import org.opencms.workplace.list.CmsListSearchAction;
052
053import java.io.IOException;
054import java.util.ArrayList;
055import java.util.HashMap;
056import java.util.Iterator;
057import java.util.List;
058import java.util.Map;
059
060import jakarta.servlet.ServletException;
061
062/**
063 * Main organization unit management view.<p>
064 *
065 * @since 6.5.6
066 */
067public abstract class A_CmsOrgUnitsList extends A_CmsListDialog {
068
069    /** list action id constant. */
070    public static final String LIST_ACTION_DEACTIVE = "add";
071
072    /** list action id constant. */
073    public static final String LIST_ACTION_DELETE = "ad";
074
075    /** list action id constant. */
076    public static final String LIST_ACTION_EDIT = "ae";
077
078    /** list action id constant. */
079    public static final String LIST_ACTION_GROUP = "ag";
080
081    /** list action id constant. */
082    public static final String LIST_ACTION_USER = "au";
083
084    /** list column id constant. */
085    public static final String LIST_COLUMN_ADMIN = "ca";
086
087    /** list column id constant. */
088    public static final String LIST_COLUMN_DELETE = "cd";
089
090    /** list column id constant. */
091    public static final String LIST_COLUMN_DESCRIPTION = "cb";
092
093    /** list column id constant. */
094    public static final String LIST_COLUMN_EDIT = "ce";
095
096    /** list column id constant. */
097    public static final String LIST_COLUMN_GROUP = "cg";
098
099    /** list column id constant. */
100    public static final String LIST_COLUMN_NAME = "cn";
101
102    /** list column id constant. */
103    public static final String LIST_COLUMN_USER = "cu";
104
105    /** list column id constant. */
106    public static final String LIST_COLUMN_WEBUSER = "cw";
107
108    /** list action id constant. */
109    public static final String LIST_DEFACTION_OVERVIEW = "do";
110
111    /** list item detail id constant. */
112    public static final String LIST_DETAIL_GROUPS = "dg";
113
114    /** list item detail id constant. */
115    public static final String LIST_DETAIL_RESOURCES = "dr";
116
117    /** list item detail id constant. */
118    public static final String LIST_DETAIL_USERS = "du";
119
120    /** list action id constant. */
121    public static final String LIST_MACTION_DELETE = "md";
122
123    /** Path to the list buttons. */
124    public static final String PATH_BUTTONS = "tools/accounts/buttons/";
125
126    /** Cached list of OUs. */
127    private List<CmsOrganizationalUnit> m_ous;
128
129    /**
130     * Public constructor.<p>
131     *
132     * @param jsp an initialized JSP action element
133     * @param listId the id of the list
134     * @param listName the list name
135     */
136    public A_CmsOrgUnitsList(CmsJspActionElement jsp, String listId, CmsMessageContainer listName) {
137
138        super(jsp, listId, listName, LIST_COLUMN_NAME, CmsListOrderEnum.ORDER_ASCENDING, null);
139    }
140
141    /**
142     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
143     */
144    @Override
145    public void executeListMultiActions() {
146
147        Iterator<CmsListItem> itItems = getSelectedItems().iterator();
148
149        if (getParamListAction().equals(LIST_MACTION_DELETE)) {
150            while (itItems.hasNext()) {
151                CmsListItem item = itItems.next();
152                String ouFqn = item.get(LIST_COLUMN_NAME).toString();
153                try {
154                    OpenCms.getOrgUnitManager().deleteOrganizationalUnit(getCms(), ouFqn.substring(1));
155                } catch (CmsException e) {
156                    throw new CmsRuntimeException(Messages.get().container(Messages.ERR_DELETE_ORGUNIT_1, ouFqn), e);
157                }
158            }
159        } else {
160            throwListUnsupportedActionException();
161        }
162        listSave();
163    }
164
165    /**
166     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
167     */
168    @Override
169    public void executeListSingleActions() throws IOException, ServletException {
170
171        String ouFqn = getSelectedItem().get(LIST_COLUMN_NAME).toString();
172        Map<String, String[]> params = new HashMap<String, String[]>();
173        params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, new String[] {ouFqn.substring(1)});
174        params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
175        if (getParamListAction().equals(LIST_ACTION_EDIT)) {
176            // forward to the edit user screen
177            getToolManager().jspForwardTool(this, "/accounts/orgunit/mgmt/edit", params);
178        } else if (getParamListAction().equals(LIST_ACTION_USER)) {
179            // forward to the edit user screen
180            getToolManager().jspForwardTool(this, "/accounts/orgunit/users", params);
181        } else if (getParamListAction().equals(LIST_ACTION_GROUP)) {
182            // forward to the edit user screen
183            getToolManager().jspForwardTool(this, "/accounts/orgunit/groups", params);
184        } else if (getParamListAction().equals(LIST_ACTION_DELETE)) {
185            // forward to the edit user screen
186            getToolManager().jspForwardTool(this, "/accounts/orgunit/mgmt/delete", params);
187        } else if (getParamListAction().equals(LIST_DEFACTION_OVERVIEW)) {
188            // forward to the edit user screen
189            getToolManager().jspForwardTool(this, "/accounts/orgunit", params);
190        } else {
191            throwListUnsupportedActionException();
192        }
193        listSave();
194    }
195
196    /**
197     * Returns the path of the edit icon.<p>
198     *
199     * @return the path of the edit icon
200     */
201    public String getEditIcon() {
202
203        return PATH_BUTTONS + "orgunit.png";
204    }
205
206    /**
207     * Returns the path of the group icon.<p>
208     *
209     * @return the path of the group icon
210     */
211    public String getGroupIcon() {
212
213        return PATH_BUTTONS + "group.png";
214    }
215
216    /**
217     * Returns the path of the user icon.<p>
218     *
219     * @return the path of the user icon
220     */
221    public String getUserIcon() {
222
223        return PATH_BUTTONS + "user.png";
224    }
225
226    /**
227     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
228     */
229    @Override
230    protected void fillDetails(String detailId) {
231
232        // get content
233        List<CmsListItem> orgUnits = getList().getAllContent();
234        Iterator<CmsListItem> itOrgUnits = orgUnits.iterator();
235        while (itOrgUnits.hasNext()) {
236            CmsListItem item = itOrgUnits.next();
237            String ouFqn = item.get(LIST_COLUMN_NAME).toString();
238            StringBuffer html = new StringBuffer(512);
239            try {
240                if (detailId.equals(LIST_DETAIL_USERS)) {
241                    List<CmsUser> usersOrgUnit = OpenCms.getOrgUnitManager().getUsers(getCms(), ouFqn, false);
242                    Iterator<CmsUser> itUsersOrgUnit = usersOrgUnit.iterator();
243                    while (itUsersOrgUnit.hasNext()) {
244                        CmsUser user = itUsersOrgUnit.next();
245                        html.append(user.getFullName());
246                        if (itUsersOrgUnit.hasNext()) {
247                            html.append("<br>");
248                        }
249                        html.append("\n");
250                    }
251                } else if (detailId.equals(LIST_DETAIL_GROUPS)) {
252                    List<CmsGroup> groupsOrgUnit = OpenCms.getOrgUnitManager().getGroups(getCms(), ouFqn, false);
253                    Iterator<CmsGroup> itGroupsOrgUnit = groupsOrgUnit.iterator();
254                    while (itGroupsOrgUnit.hasNext()) {
255                        CmsGroup group = itGroupsOrgUnit.next();
256                        String niceGroupName = OpenCms.getWorkplaceManager().translateGroupName(group.getName(), false);
257                        html.append(niceGroupName);
258                        if (itGroupsOrgUnit.hasNext()) {
259                            html.append("<br>");
260                        }
261                        html.append("\n");
262                    }
263                } else if (detailId.equals(LIST_DETAIL_RESOURCES)) {
264                    List<CmsResource> resourcesOrgUnit = OpenCms.getOrgUnitManager().getResourcesForOrganizationalUnit(
265                        getCms(),
266                        ouFqn);
267                    Iterator<CmsResource> itResourcesOrgUnit = resourcesOrgUnit.iterator();
268                    while (itResourcesOrgUnit.hasNext()) {
269                        CmsResource resource = itResourcesOrgUnit.next();
270                        html.append(resource.getRootPath());
271                        if (itResourcesOrgUnit.hasNext()) {
272                            html.append("<br>");
273                        }
274                        html.append("\n");
275                    }
276                } else {
277                    continue;
278                }
279            } catch (Exception e) {
280                // noop
281            }
282            item.set(detailId, html.toString());
283        }
284    }
285
286    /**
287     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
288     */
289    @Override
290    protected List<CmsListItem> getListItems() throws CmsException {
291
292        List<CmsListItem> ret = new ArrayList<CmsListItem>();
293        List<CmsOrganizationalUnit> orgUnits = getOrgUnits();
294        Iterator<CmsOrganizationalUnit> itOrgUnits = orgUnits.iterator();
295        while (itOrgUnits.hasNext()) {
296            CmsOrganizationalUnit childOrgUnit = itOrgUnits.next();
297            CmsListItem item = getList().newItem(childOrgUnit.getName());
298            item.set(LIST_COLUMN_NAME, CmsOrganizationalUnit.SEPARATOR + childOrgUnit.getName());
299            item.set(LIST_COLUMN_DESCRIPTION, childOrgUnit.getDescription(getLocale()));
300            item.set(
301                LIST_COLUMN_ADMIN,
302                Boolean.valueOf(
303                    OpenCms.getRoleManager().hasRole(
304                        getCms(),
305                        CmsRole.ADMINISTRATOR.forOrgUnit(childOrgUnit.getName()))));
306            item.set(LIST_COLUMN_WEBUSER, Boolean.valueOf(childOrgUnit.hasFlagWebuser()));
307            ret.add(item);
308        }
309        return ret;
310    }
311
312    /**
313     * Returns the organizational units to display.<p>
314     *
315     * @return the organizational units
316     *
317     * @throws CmsException if something goes wrong
318     */
319    protected List<CmsOrganizationalUnit> getOrgUnits() throws CmsException {
320
321        if (m_ous == null) {
322            m_ous = OpenCms.getRoleManager().getOrgUnitsForRole(getCms(), CmsRole.ACCOUNT_MANAGER.forOrgUnit(""), true);
323        }
324        return m_ous;
325    }
326
327    /**
328     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
329     */
330    @Override
331    protected void setColumns(CmsListMetadata metadata) {
332
333        // create column for edit
334        CmsListColumnDefinition editCol = new CmsListColumnDefinition(LIST_COLUMN_EDIT);
335        editCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_EDIT_0));
336        editCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_EDIT_HELP_0));
337        editCol.setWidth("20");
338        editCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
339        editCol.setSorteable(false);
340        // add edit action
341        CmsListDirectAction editAction = new CmsListDirectAction(LIST_ACTION_EDIT) {
342
343            /**
344             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getHelpText()
345             */
346            @Override
347            public CmsMessageContainer getHelpText() {
348
349                if (!isEnabled()) {
350                    return Messages.get().container(Messages.GUI_ORGUNIT_ADMIN_TOOL_DISABLED_EDIT_HELP_0);
351                }
352                return super.getHelpText();
353            }
354
355            /**
356             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
357             */
358            @Override
359            public String getIconPath() {
360
361                if (getItem() != null) {
362                    if (((Boolean)getItem().get(LIST_COLUMN_WEBUSER)).booleanValue()) {
363                        return PATH_BUTTONS + "webuser_ou.png";
364                    }
365                }
366                return super.getIconPath();
367            }
368
369            /**
370             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isEnabled()
371             */
372            @Override
373            public boolean isEnabled() {
374
375                if (getItem() != null) {
376                    return ((Boolean)getItem().get(LIST_COLUMN_ADMIN)).booleanValue();
377                }
378                return super.isVisible();
379            }
380        };
381        editAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_ACTION_EDIT_NAME_0));
382        editAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_EDIT_HELP_0));
383        editAction.setIconPath(getEditIcon());
384        editCol.addDirectAction(editAction);
385        // add it to the list definition
386        metadata.addColumn(editCol);
387
388        // create column for user
389        CmsListColumnDefinition userCol = new CmsListColumnDefinition(LIST_COLUMN_USER);
390        userCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_USER_0));
391        userCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_USER_HELP_0));
392        userCol.setWidth("20");
393        userCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
394        userCol.setSorteable(false);
395        // add user action
396        CmsListDirectAction userAction = new CmsListDirectAction(LIST_ACTION_USER);
397        userAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_ACTION_USER_NAME_0));
398        userAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_USER_HELP_0));
399        userAction.setIconPath(getUserIcon());
400        userCol.addDirectAction(userAction);
401        // add it to the list definition
402        metadata.addColumn(userCol);
403
404        // create column for group
405        CmsListColumnDefinition groupCol = new CmsListColumnDefinition(LIST_COLUMN_GROUP);
406        groupCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_GROUP_0));
407        groupCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_GROUP_HELP_0));
408        groupCol.setWidth("20");
409        groupCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
410        groupCol.setSorteable(false);
411        // add group action
412        CmsListDirectAction groupAction = new CmsListDirectAction(LIST_ACTION_GROUP);
413        groupAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_ACTION_GROUP_NAME_0));
414        groupAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_GROUP_HELP_0));
415        groupAction.setIconPath(getGroupIcon());
416        groupCol.addDirectAction(groupAction);
417        // add it to the list definition
418        metadata.addColumn(groupCol);
419
420        // create column for description
421        CmsListColumnDefinition descCol = new CmsListColumnDefinition(LIST_COLUMN_DESCRIPTION);
422        descCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_DESCRIPTION_0));
423        descCol.setWidth("60%");
424        descCol.setSorteable(true);
425        // create default overview action
426        CmsListDefaultAction defOverviewAction = new CmsListDefaultAction(LIST_DEFACTION_OVERVIEW);
427        defOverviewAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_DEFACTION_OVERVIEW_NAME_0));
428        defOverviewAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_DEFACTION_OVERVIEW_HELP_0));
429        descCol.addDefaultAction(defOverviewAction);
430        // add it to the list definition
431        metadata.addColumn(descCol);
432
433        // create column for name / path
434        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
435        nameCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_NAME_0));
436        nameCol.setWidth("40%");
437        // add it to the list definition
438        metadata.addColumn(nameCol);
439
440        // create column for manageable flag
441        CmsListColumnDefinition adminCol = new CmsListColumnDefinition(LIST_COLUMN_ADMIN);
442        adminCol.setVisible(false);
443        metadata.addColumn(adminCol);
444
445        // create column for webuser flag
446        CmsListColumnDefinition webuserCol = new CmsListColumnDefinition(LIST_COLUMN_WEBUSER);
447        webuserCol.setVisible(false);
448        metadata.addColumn(webuserCol);
449    }
450
451    /**
452     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
453     */
454    @Override
455    protected void setIndependentActions(CmsListMetadata metadata) {
456
457        // add users details
458        CmsListItemDetails usersDetails = new CmsListItemDetails(LIST_DETAIL_USERS);
459        usersDetails.setAtColumn(LIST_COLUMN_DESCRIPTION);
460        usersDetails.setVisible(false);
461        usersDetails.setShowActionName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_USERS_NAME_0));
462        usersDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_USERS_HELP_0));
463        usersDetails.setHideActionName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_USERS_NAME_0));
464        usersDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_USERS_HELP_0));
465        usersDetails.setName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_USERS_NAME_0));
466        usersDetails.setFormatter(
467            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_USERS_NAME_0)));
468        metadata.addItemDetails(usersDetails);
469
470        // add groups details
471        CmsListItemDetails groupsDetails = new CmsListItemDetails(LIST_DETAIL_GROUPS);
472        groupsDetails.setAtColumn(LIST_COLUMN_DESCRIPTION);
473        groupsDetails.setVisible(false);
474        groupsDetails.setShowActionName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_GROUPS_NAME_0));
475        groupsDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_GROUPS_HELP_0));
476        groupsDetails.setHideActionName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_GROUPS_NAME_0));
477        groupsDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_GROUPS_HELP_0));
478        groupsDetails.setName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_GROUPS_NAME_0));
479        groupsDetails.setFormatter(
480            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_GROUPS_NAME_0)));
481        metadata.addItemDetails(groupsDetails);
482
483        // add resources details
484        CmsListItemDetails resourcesDetails = new CmsListItemDetails(LIST_DETAIL_RESOURCES);
485        resourcesDetails.setAtColumn(LIST_COLUMN_DESCRIPTION);
486        resourcesDetails.setVisible(false);
487        resourcesDetails.setShowActionName(
488            Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_RESOURCES_NAME_0));
489        resourcesDetails.setShowActionHelpText(
490            Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_RESOURCES_HELP_0));
491        resourcesDetails.setHideActionName(
492            Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_RESOURCES_NAME_0));
493        resourcesDetails.setHideActionHelpText(
494            Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_RESOURCES_HELP_0));
495        resourcesDetails.setName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_RESOURCES_NAME_0));
496        resourcesDetails.setFormatter(
497            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_RESOURCES_NAME_0)));
498        metadata.addItemDetails(resourcesDetails);
499
500        // makes the list searchable
501        CmsListSearchAction searchAction = new CmsListSearchAction(metadata.getColumnDefinition(LIST_COLUMN_NAME));
502        searchAction.addColumn(metadata.getColumnDefinition(LIST_COLUMN_DESCRIPTION));
503        metadata.setSearchAction(searchAction);
504    }
505
506    /**
507     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
508     */
509    @Override
510    protected void setMultiActions(CmsListMetadata metadata) {
511
512        // noop
513    }
514
515}