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