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