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, 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.CmsResource;
032import org.opencms.file.CmsUser;
033import org.opencms.i18n.CmsMessageContainer;
034import org.opencms.jsp.CmsJspActionElement;
035import org.opencms.main.CmsException;
036import org.opencms.security.CmsAccessControlEntry;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.util.CmsUUID;
039import org.opencms.workplace.list.A_CmsListDialog;
040import org.opencms.workplace.list.CmsHtmlList;
041import org.opencms.workplace.list.CmsListColumnAlignEnum;
042import org.opencms.workplace.list.CmsListColumnDefinition;
043import org.opencms.workplace.list.CmsListDirectAction;
044import org.opencms.workplace.list.CmsListItem;
045import org.opencms.workplace.list.CmsListItemActionIconComparator;
046import org.opencms.workplace.list.CmsListMetadata;
047import org.opencms.workplace.list.CmsListOrderEnum;
048import org.opencms.workplace.tools.CmsIdentifiableObjectContainer;
049
050import java.util.Iterator;
051import java.util.List;
052import java.util.Set;
053
054import javax.servlet.http.HttpServletRequest;
055import javax.servlet.http.HttpServletResponse;
056import javax.servlet.jsp.PageContext;
057
058/**
059 * Group dependencies list view.<p>
060 *
061 * @since 8.0.0
062 */
063public class CmsGroupPrincipalDependenciesList extends A_CmsListDialog {
064
065    /** list action id constant. */
066    public static final String LIST_ACTION_ICON = "ai";
067
068    /** list column id constant. */
069    public static final String LIST_COLUMN_ICON = "ci";
070
071    /** list column id constant. */
072    public static final String LIST_COLUMN_NAME = "cn";
073
074    /** list column id constant. */
075    public static final String LIST_COLUMN_PERMISSIONS = "cp";
076
077    /** list column id constant. */
078    public static final String LIST_COLUMN_TYPE = "ct";
079
080    /** List id constant. */
081    public static final String LIST_ID = "lgd";
082
083    /** Stores the value of the request parameter for the group id, could be a list of ids. */
084    private String m_paramGroupid;
085
086    /** Flag to show resources with file attributes. */
087    protected boolean m_showAttributes;
088
089    /**
090     * Public constructor.<p>
091     *
092     * @param jsp an initialized JSP action element
093     */
094    public CmsGroupPrincipalDependenciesList(CmsJspActionElement jsp) {
095
096        this(LIST_ID, jsp);
097        m_showAttributes = false;
098    }
099
100    /**
101     * Public constructor with JSP variables.<p>
102     *
103     * @param context the JSP page context
104     * @param req the JSP request
105     * @param res the JSP response
106     */
107    public CmsGroupPrincipalDependenciesList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
108
109        this(new CmsJspActionElement(context, req, res));
110        m_showAttributes = false;
111    }
112
113    /**
114     * Protected constructor.<p>
115     *
116     * @param listId the id of the specialized list
117     * @param jsp an initialized JSP action element
118     */
119    protected CmsGroupPrincipalDependenciesList(String listId, CmsJspActionElement jsp) {
120
121        super(
122            jsp,
123            listId,
124            Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_NAME_0),
125            LIST_COLUMN_NAME,
126            CmsListOrderEnum.ORDER_ASCENDING,
127            LIST_COLUMN_NAME);
128        m_showAttributes = false;
129    }
130
131    /**
132     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
133     */
134    @Override
135    public void executeListMultiActions() {
136
137        throwListUnsupportedActionException();
138    }
139
140    /**
141     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
142     */
143    @Override
144    public void executeListSingleActions() {
145
146        throwListUnsupportedActionException();
147    }
148
149    /**
150     * Returns the group id parameter value.<p>
151     *
152     * @return the group id parameter value
153     */
154    public String getParamGroupid() {
155
156        return m_paramGroupid;
157    }
158
159    /**
160     * Sets the group id parameter value.<p>
161     *
162     * @param groupId the group id parameter value
163     */
164    public void setParamGroupid(String groupId) {
165
166        m_paramGroupid = groupId;
167    }
168
169    /**
170     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
171     */
172    @Override
173    protected void fillDetails(String detailId) {
174
175        // no-op
176    }
177
178    /**
179     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
180     */
181    @Override
182    protected List<CmsListItem> getListItems() throws CmsException {
183
184        CmsIdentifiableObjectContainer<CmsListItem> ret = new CmsIdentifiableObjectContainer<CmsListItem>(true, false);
185        Iterator<String> itGroups = CmsStringUtil.splitAsList(
186            getParamGroupid(),
187            CmsHtmlList.ITEM_SEPARATOR,
188            true).iterator();
189        String storedSiteRoot = getCms().getRequestContext().getSiteRoot();
190        try {
191            getCms().getRequestContext().setSiteRoot("/");
192            while (itGroups.hasNext()) {
193                CmsGroup group = getCms().readGroup(new CmsUUID(itGroups.next()));
194                // get content
195                Set<CmsResource> resources = getCms().getResourcesForPrincipal(group.getId(), null, m_showAttributes);
196                Iterator<CmsResource> itRes = resources.iterator();
197                while (itRes.hasNext()) {
198                    CmsResource resource = itRes.next();
199                    CmsListItem item = ret.getObject(resource.getResourceId().toString());
200                    if (item == null) {
201                        item = getList().newItem(resource.getResourceId().toString());
202                        item.set(LIST_COLUMN_NAME, resource.getRootPath());
203                        item.set(LIST_COLUMN_TYPE, Integer.valueOf(resource.getTypeId()));
204                        Iterator<CmsAccessControlEntry> itAces = getCms().getAccessControlEntries(
205                            resource.getRootPath(),
206                            false).iterator();
207                        while (itAces.hasNext()) {
208                            CmsAccessControlEntry ace = itAces.next();
209                            if (ace.getPrincipal().equals(group.getId())) {
210                                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(
211                                    ace.getPermissions().getPermissionString())) {
212                                    item.set(
213                                        LIST_COLUMN_PERMISSIONS,
214                                        group.getName() + ": " + ace.getPermissions().getPermissionString());
215                                }
216                                break;
217                            }
218                        }
219                        ret.addIdentifiableObject(item.getId(), item);
220                    } else {
221                        String oldData = (String)item.get(LIST_COLUMN_PERMISSIONS);
222                        Iterator<CmsAccessControlEntry> itAces = getCms().getAccessControlEntries(
223                            resource.getRootPath(),
224                            false).iterator();
225                        while (itAces.hasNext()) {
226                            CmsAccessControlEntry ace = itAces.next();
227                            if (ace.getPrincipal().equals(group.getId())) {
228                                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(
229                                    ace.getPermissions().getPermissionString())) {
230                                    String data = group.getName() + ": " + ace.getPermissions().getPermissionString();
231                                    if (oldData != null) {
232                                        data = oldData + ", " + data;
233                                    }
234                                    item.set(LIST_COLUMN_PERMISSIONS, data);
235                                }
236                                break;
237                            }
238                        }
239                    }
240                }
241                // add users
242                Iterator<CmsUser> itUsers = getCms().getUsersOfGroup(group.getName()).iterator();
243                while (itUsers.hasNext()) {
244                    CmsUser user = itUsers.next();
245                    CmsListItem item = ret.getObject(user.getId().toString());
246                    if (item == null) {
247                        item = getList().newItem(user.getId().toString());
248                        item.set(LIST_COLUMN_NAME, user.getName());
249                        item.set(LIST_COLUMN_PERMISSIONS, "--");
250                        ret.addIdentifiableObject(item.getId(), item);
251                    }
252                }
253                // add child groups
254                Iterator<CmsGroup> itChildren = getCms().getChildren(group.getName(), false).iterator();
255                while (itChildren.hasNext()) {
256                    CmsGroup child = itChildren.next();
257                    CmsListItem item = ret.getObject(child.getId().toString());
258                    if (item == null) {
259                        item = getList().newItem(child.getId().toString());
260                        item.set(LIST_COLUMN_NAME, child.getName());
261                        item.set(LIST_COLUMN_PERMISSIONS, "--");
262                        ret.addIdentifiableObject(item.getId(), item);
263                    }
264                }
265            }
266        } finally {
267            getCms().getRequestContext().setSiteRoot(storedSiteRoot);
268        }
269        return ret.elementList();
270    }
271
272    /**
273     * @see org.opencms.workplace.CmsWorkplace#initMessages()
274     */
275    @Override
276    protected void initMessages() {
277
278        // add specific dialog resource bundle
279        addMessages(Messages.get().getBundleName());
280        // add cms dialog resource bundle
281        addMessages(org.opencms.workplace.Messages.get().getBundleName());
282        // add default resource bundles
283        super.initMessages();
284    }
285
286    /**
287     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
288     */
289    @Override
290    protected void setColumns(CmsListMetadata metadata) {
291
292        // create column for edit
293        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
294        iconCol.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_COLS_ICON_0));
295        iconCol.setHelpText(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_COLS_ICON_HELP_0));
296        iconCol.setWidth("20");
297        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
298        iconCol.setListItemComparator(new CmsListItemActionIconComparator());
299
300        // add icon actions
301        setIconActions(iconCol);
302
303        // add it to the list definition
304        metadata.addColumn(iconCol);
305
306        // add column for name
307        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
308        nameCol.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_COLS_NAME_0));
309        nameCol.setWidth("80%");
310        metadata.addColumn(nameCol);
311
312        // add column for permissions
313        CmsListColumnDefinition permissionsCol = new CmsListColumnDefinition(LIST_COLUMN_PERMISSIONS);
314        permissionsCol.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_COLS_PERMISSIONS_0));
315        permissionsCol.setWidth("20%");
316        metadata.addColumn(permissionsCol);
317
318        // add column for type
319        CmsListColumnDefinition typeCol = new CmsListColumnDefinition(LIST_COLUMN_TYPE);
320        typeCol.setName(new CmsMessageContainer(null, "type"));
321        typeCol.setVisible(false);
322        metadata.addColumn(typeCol);
323    }
324
325    /**
326     * Sets the right icon actions for the dialog.<p>
327     *
328     * @param iconCol the column to set the actions
329     */
330    protected void setIconActions(CmsListColumnDefinition iconCol) {
331
332        // add resource icon action
333        CmsListDirectAction resourceIconAction = new CmsDependencyIconAction(
334            LIST_ACTION_ICON,
335            CmsDependencyIconActionType.RESOURCE,
336            getCms());
337        resourceIconAction.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_RES_NAME_0));
338        resourceIconAction.setHelpText(
339            Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_RES_HELP_0));
340        resourceIconAction.setEnabled(false);
341        iconCol.addDirectAction(resourceIconAction);
342
343        // add group icon action
344        CmsListDirectAction groupIconAction = new CmsDependencyIconAction(
345            LIST_ACTION_ICON,
346            CmsDependencyIconActionType.GROUP,
347            getCms());
348        groupIconAction.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_GRP_NAME_0));
349        groupIconAction.setHelpText(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_GRP_HELP_0));
350        groupIconAction.setEnabled(false);
351        iconCol.addDirectAction(groupIconAction);
352
353        // add group icon action
354        CmsListDirectAction userIconAction = new CmsDependencyIconAction(
355            LIST_ACTION_ICON,
356            CmsDependencyIconActionType.USER,
357            getCms());
358        userIconAction.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_USR_NAME_0));
359        userIconAction.setHelpText(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_USR_HELP_0));
360        userIconAction.setEnabled(false);
361        iconCol.addDirectAction(userIconAction);
362    }
363
364    /**
365     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
366     */
367    @Override
368    protected void setIndependentActions(CmsListMetadata metadata) {
369
370        // no-op
371    }
372
373    /**
374     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
375     */
376    @Override
377    protected void setMultiActions(CmsListMetadata metadata) {
378
379        // no-op
380    }
381}