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.ui.apps.user;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.security.CmsAccessControlEntry;
035import org.opencms.security.CmsPrincipal;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.apps.A_CmsWorkplaceApp;
038import org.opencms.ui.apps.CmsAppWorkplaceUi;
039import org.opencms.ui.apps.CmsFileExplorerConfiguration;
040import org.opencms.ui.apps.Messages;
041import org.opencms.ui.apps.user.CmsShowResourcesDialog.DialogType;
042import org.opencms.ui.contextmenu.CmsContextMenu;
043import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
044import org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry;
045import org.opencms.util.CmsUUID;
046import org.opencms.workplace.explorer.CmsResourceUtil;
047
048import java.util.ArrayList;
049import java.util.Collections;
050import java.util.Iterator;
051import java.util.List;
052import java.util.Locale;
053import java.util.Set;
054
055import org.apache.commons.logging.Log;
056
057import com.vaadin.shared.MouseEventDetails.MouseButton;
058import com.vaadin.v7.data.Item;
059import com.vaadin.v7.data.util.IndexedContainer;
060import com.vaadin.v7.event.ItemClickEvent;
061import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
062import com.vaadin.v7.ui.Table;
063
064/**
065 * Table with resources for which a given principal has permissions.<p>
066 */
067public class CmsShowResourceTable extends Table {
068
069    /**
070     * The menu entry to switch to the explorer of concerning site.<p>
071     */
072    class ExplorerEntry implements I_CmsSimpleContextMenuEntry<Set<String>> {
073
074        /**
075         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
076         */
077        public void executeAction(Set<String> data) {
078
079            CmsResource res;
080            try {
081                res = getCms().readResource(data.iterator().next());
082                openExplorerForParent(res.getRootPath(), res.getStructureId().getStringValue());
083            } catch (CmsException e) {
084                e.printStackTrace();
085            }
086
087        }
088
089        /**
090         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
091         */
092        public String getTitle(Locale locale) {
093
094            return Messages.get().getBundle(locale).key(Messages.GUI_EXPLORER_TITLE_0);
095        }
096
097        /**
098         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
099         */
100        public CmsMenuItemVisibilityMode getVisibility(Set<String> data) {
101
102            if (data == null) {
103                return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
104            }
105
106            return data.size() == 1
107            ? CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE
108            : CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
109        }
110
111    }
112
113    /**vaadin serial id. */
114    private static final long serialVersionUID = -7843045876535036146L;
115
116    /** Log instance for this class. */
117    private static final Log LOG = CmsLog.getLog(CmsShowResourceTable.class);
118
119    /**Icon column. */
120    private static final String PROP_ICON = "icon";
121
122    /**Name column. */
123    private static final String PROP_NAME = "name";
124
125    /**Permission column. */
126    private static final String PROP_PERMISSION = "permission";
127
128    /** The available menu entries. */
129    private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries;
130
131    /** The context menu. */
132    CmsContextMenu m_menu;
133
134    /**Indexed Container. */
135    private IndexedContainer m_container;
136
137    /**CmsPrincipal. */
138    private CmsPrincipal m_principal;
139
140    /**CmsObject. */
141    private CmsObject m_cms;
142
143    /**
144     * public constructor.<p>
145     *
146     * @param cms CmsObject
147     * @param principalID id of principal
148     * @param type of dialog
149     */
150    public CmsShowResourceTable(CmsObject cms, CmsUUID principalID, DialogType type) {
151
152        //Set menu
153        m_menu = new CmsContextMenu();
154        m_menu.setAsTableContextMenu(this);
155
156        setSelectable(true);
157
158        try {
159            m_cms = cms;
160            m_principal = getPrincipal(cms, type, principalID);
161
162            setSizeFull();
163            m_container = new IndexedContainer();
164
165            m_container.addContainerProperty(PROP_ICON, com.vaadin.server.Resource.class, null);
166            m_container.addContainerProperty(PROP_NAME, String.class, "");
167            m_container.addContainerProperty(PROP_PERMISSION, String.class, "");
168
169            Iterator<CmsResource> iterator = getResourcesFromPrincipal(cms, principalID).iterator();
170            while (iterator.hasNext()) {
171                CmsResource res = iterator.next();
172                CmsResourceUtil resUtil = new CmsResourceUtil(cms, res);
173
174                Item item = m_container.addItem(res);
175                item.getItemProperty(PROP_ICON).setValue(resUtil.getSmallIconResource());
176                item.getItemProperty(PROP_NAME).setValue(res.getRootPath());
177                item.getItemProperty(PROP_PERMISSION).setValue(getPermissionString(cms, res, type));
178            }
179            setContainerDataSource(m_container);
180            setItemIconPropertyId(PROP_ICON);
181            setRowHeaderMode(RowHeaderMode.ICON_ONLY);
182
183            setColumnWidth(null, 40);
184            setVisibleColumns(PROP_NAME, PROP_PERMISSION);
185            setColumnHeader(PROP_NAME, CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_GROUP_NAME_0));
186            setColumnHeader(
187                PROP_PERMISSION,
188                CmsVaadinUtils.getMessageText(org.opencms.workplace.commons.Messages.GUI_PERMISSION_0));
189        } catch (CmsException e) {
190            LOG.error("Can not read user information.", e);
191        }
192        addItemClickListener(new ItemClickListener() {
193
194            private static final long serialVersionUID = -4738296706762013443L;
195
196            public void itemClick(ItemClickEvent event) {
197
198                setValue(null);
199                select(event.getItemId());
200
201                //Right click or click on icon column (=null) -> show menu
202                if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) {
203                    m_menu.setEntries(getMenuEntries(), Collections.singleton(((CmsResource)getValue()).getRootPath()));
204                    m_menu.openForTable(event, event.getItemId(), event.getPropertyId(), CmsShowResourceTable.this);
205                }
206
207            }
208        });
209    }
210
211    /**
212     * Checks if table is empty.<p>
213     *
214     * @return true if table is empty
215     */
216    public boolean hasNoEntries() {
217
218        return m_container.size() == 0;
219    }
220
221    /**
222     * Gets CmsObject.<p>
223     *
224     * @return CmsObject
225     */
226    protected CmsObject getCms() {
227
228        return m_cms;
229    }
230
231    /**
232     * Returns the available menu entries.<p>
233     *
234     * @return the menu entries
235     */
236    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
237
238        if (m_menuEntries == null) {
239            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
240            m_menuEntries.add(new ExplorerEntry());
241        }
242        return m_menuEntries;
243    }
244
245    /**
246     * Opens the explorer for given path and selected resource.<p>
247     *
248     * @param rootPath to be opened
249     * @param uuid to be selected
250     */
251    void openExplorerForParent(String rootPath, String uuid) {
252
253        String parentPath = CmsResource.getParentFolder(rootPath);
254
255        if (!rootPath.startsWith(m_cms.getRequestContext().getSiteRoot())) {
256            m_cms.getRequestContext().setSiteRoot("");
257        }
258        CmsAppWorkplaceUi.get().showApp(
259            CmsFileExplorerConfiguration.APP_ID,
260            m_cms.getRequestContext().getCurrentProject().getUuid()
261                + A_CmsWorkplaceApp.PARAM_SEPARATOR
262                + m_cms.getRequestContext().getSiteRoot()
263                + A_CmsWorkplaceApp.PARAM_SEPARATOR
264                + parentPath.substring(m_cms.getRequestContext().getSiteRoot().length())
265                + A_CmsWorkplaceApp.PARAM_SEPARATOR
266                + uuid
267                + A_CmsWorkplaceApp.PARAM_SEPARATOR);
268    }
269
270    /**
271     * Gets the permission string.<p>
272     *
273     * @param cms CmsObject
274     * @param res Resource to get permission for
275     * @param type dialog type
276     * @return permission string for given resource
277     * @throws CmsException thrown if ACE can not be read
278     */
279    private String getPermissionString(CmsObject cms, CmsResource res, DialogType type) throws CmsException {
280
281        if (type.equals(DialogType.User)) {
282            return cms.getPermissions(res.getRootPath(), m_principal.getName()).getPermissionString();
283        } else if (type.equals(DialogType.Group)) {
284            Iterator<CmsAccessControlEntry> itAces = cms.getAccessControlEntries(res.getRootPath(), false).iterator();
285            while (itAces.hasNext()) {
286                CmsAccessControlEntry ace = itAces.next();
287                if (ace.getPrincipal().equals(m_principal.getId())) {
288                    return ace.getPermissions().getPermissionString();
289                }
290            }
291        }
292        return "";
293    }
294
295    /**
296     * Get principal from id.<p>
297     *
298     * @param cms CmsObject
299     * @param type user or group
300     * @param id id of principal
301     * @return Principal
302     * @throws CmsException exception
303     */
304    private CmsPrincipal getPrincipal(CmsObject cms, DialogType type, CmsUUID id) throws CmsException {
305
306        if (type.equals(DialogType.Group)) {
307            return cms.readGroup(id);
308        }
309        if (type.equals(DialogType.User)) {
310            return cms.readUser(id);
311        }
312        return null;
313    }
314
315    /**
316     * Get resources set for the given principal.<p>
317     *
318     * @param cms CmsObject
319     * @param id id of principal
320     * @return Set of CmsResource
321     * @throws CmsException if resources can not be read
322     */
323    private Set<CmsResource> getResourcesFromPrincipal(CmsObject cms, CmsUUID id) throws CmsException {
324
325        return cms.getResourcesForPrincipal(id, null, false);
326    }
327
328}