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, 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.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.main.OpenCms; 035import org.opencms.security.CmsOrganizationalUnit; 036import org.opencms.security.CmsRole; 037import org.opencms.security.CmsRoleViolationException; 038import org.opencms.ui.A_CmsUI; 039import org.opencms.ui.CmsCssIcon; 040import org.opencms.ui.CmsVaadinUtils; 041import org.opencms.ui.apps.Messages; 042import org.opencms.ui.components.CmsBasicDialog; 043import org.opencms.ui.components.CmsBasicDialog.DialogWidth; 044import org.opencms.ui.components.OpenCmsTheme; 045import org.opencms.ui.contextmenu.CmsContextMenu; 046import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode; 047import org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry; 048import org.opencms.util.CmsStringUtil; 049 050import java.util.ArrayList; 051import java.util.Collections; 052import java.util.List; 053import java.util.Locale; 054import java.util.Set; 055 056import org.apache.commons.logging.Log; 057 058import com.vaadin.server.Resource; 059import com.vaadin.shared.MouseEventDetails.MouseButton; 060import com.vaadin.ui.Window; 061import com.vaadin.ui.themes.ValoTheme; 062import com.vaadin.v7.data.Item; 063import com.vaadin.v7.data.util.IndexedContainer; 064import com.vaadin.v7.data.util.filter.Or; 065import com.vaadin.v7.data.util.filter.SimpleStringFilter; 066import com.vaadin.v7.event.ItemClickEvent; 067import com.vaadin.v7.event.ItemClickEvent.ItemClickListener; 068import com.vaadin.v7.ui.Table; 069import com.vaadin.v7.ui.VerticalLayout; 070 071/** 072 * Class to show ous in table for account management.<p> 073 */ 074public class CmsOUTable extends Table implements I_CmsFilterableTable { 075 076 /** 077 * Menu Entry for Delete option.<p> 078 */ 079 class EntryDelete implements I_CmsSimpleContextMenuEntry<Set<String>> { 080 081 /** 082 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object) 083 */ 084 public void executeAction(final Set<String> context) { 085 086 Window window = CmsBasicDialog.prepareWindow(); 087 CmsDeleteOUDialog dialog = new CmsDeleteOUDialog(m_cms, context.iterator().next(), window, m_app); 088 window.setContent(dialog); 089 window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_OU_DELETE_0)); 090 A_CmsUI.get().addWindow(window); 091 } 092 093 /** 094 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale) 095 */ 096 public String getTitle(Locale locale) { 097 098 return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_OU_DELETE_0); 099 } 100 101 /** 102 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object) 103 */ 104 public CmsMenuItemVisibilityMode getVisibility(Set<String> context) { 105 106 if (getItem(context.iterator().next()).getItemProperty(TableProperty.Type).getValue().equals( 107 CmsOuTreeType.OU)) { 108 if (isAdmin()) { 109 return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE; 110 } 111 return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE; 112 } 113 114 return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 115 116 } 117 } 118 119 /** 120 * Edit menu entry.<p> 121 */ 122 class EntryEdit implements I_CmsSimpleContextMenuEntry<Set<String>> { 123 124 /** 125 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object) 126 */ 127 public void executeAction(Set<String> context) { 128 129 Window window = CmsBasicDialog.prepareWindow(); 130 window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_OU_EDIT_WINDOW_CAPTION_0)); 131 window.setContent(new CmsOUEditDialog(m_cms, context.iterator().next(), window, m_app)); 132 133 A_CmsUI.get().addWindow(window); 134 } 135 136 /** 137 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale) 138 */ 139 public String getTitle(Locale locale) { 140 141 return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_OU_EDIT_0); 142 } 143 144 /** 145 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object) 146 */ 147 public CmsMenuItemVisibilityMode getVisibility(Set<String> context) { 148 149 if (getItem(context.iterator().next()).getItemProperty(TableProperty.Type).getValue().equals( 150 CmsOuTreeType.OU)) { 151 if (isAdmin()) { 152 return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE; 153 } 154 return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE; 155 } 156 157 return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 158 159 } 160 161 } 162 163 /** 164 * Entry for new user in the context menu.<p> 165 */ 166 class EntryImportExportUser implements I_CmsSimpleContextMenuEntry<Set<String>> { 167 168 /** 169 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object) 170 */ 171 public void executeAction(Set<String> context) { 172 173 openImportExportDialog(m_cms, context.iterator().next()); 174 } 175 176 /** 177 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale) 178 */ 179 public String getTitle(Locale locale) { 180 181 return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_IMEXPORT_CONTEXTMENUNAME_0); 182 } 183 184 /** 185 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object) 186 */ 187 public CmsMenuItemVisibilityMode getVisibility(Set<String> context) { 188 189 if (getItem(context.iterator().next()).getItemProperty(TableProperty.Type).getValue().equals( 190 CmsOuTreeType.OU)) { 191 return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE; 192 } 193 return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 194 } 195 196 } 197 198 /** 199 * New ou menu entry.<p> 200 */ 201 class EntryNewGroup implements I_CmsSimpleContextMenuEntry<Set<String>> { 202 203 /** 204 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object) 205 */ 206 public void executeAction(Set<String> context) { 207 208 Window window = CmsBasicDialog.prepareWindow(); 209 window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_GROUP_0)); 210 window.setContent(new CmsGroupEditDialog(m_cms, window, m_parentOu, m_app)); 211 212 A_CmsUI.get().addWindow(window); 213 } 214 215 /** 216 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale) 217 */ 218 public String getTitle(Locale locale) { 219 220 return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_GROUP_0); 221 } 222 223 /** 224 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object) 225 */ 226 public CmsMenuItemVisibilityMode getVisibility(Set<String> context) { 227 228 Object typeObj = getItem(context.iterator().next()).getItemProperty(TableProperty.Type).getValue(); 229 if (((I_CmsOuTreeType)typeObj).isGroup()) { 230 return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE; 231 232 } 233 return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 234 } 235 236 } 237 238 /** 239 * Entry for new user in the context menu.<p> 240 */ 241 class EntryNewUser implements I_CmsSimpleContextMenuEntry<Set<String>> { 242 243 /** 244 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object) 245 */ 246 public void executeAction(Set<String> context) { 247 248 Window window = CmsBasicDialog.prepareWindow(); 249 window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_USER_0)); 250 window.setContent(new CmsUserEditDialog(m_cms, window, m_parentOu, m_app)); 251 252 A_CmsUI.get().addWindow(window); 253 } 254 255 /** 256 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale) 257 */ 258 public String getTitle(Locale locale) { 259 260 return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_USER_0); 261 } 262 263 /** 264 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object) 265 */ 266 public CmsMenuItemVisibilityMode getVisibility(Set<String> context) { 267 268 Object typeObj = getItem(context.iterator().next()).getItemProperty(TableProperty.Type).getValue(); 269 if (((I_CmsOuTreeType)typeObj).isUser()) { 270 return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE; 271 } 272 return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 273 } 274 275 } 276 277 /** 278 * Entry to open item in context menu.<p> 279 */ 280 class EntryOpen implements I_CmsSimpleContextMenuEntry<Set<String>>, I_CmsSimpleContextMenuEntry.I_HasCssStyles { 281 282 /** 283 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object) 284 */ 285 public void executeAction(Set<String> context) { 286 287 String itemId = context.iterator().next(); 288 updateApp(itemId); 289 290 } 291 292 /** 293 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry.I_HasCssStyles#getStyles() 294 */ 295 public String getStyles() { 296 297 return ValoTheme.LABEL_BOLD; 298 } 299 300 /** 301 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale) 302 */ 303 public String getTitle(Locale locale) { 304 305 return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_OPEN_0); 306 } 307 308 /** 309 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object) 310 */ 311 public CmsMenuItemVisibilityMode getVisibility(Set<String> context) { 312 313 if (m_app != null) { 314 return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE; 315 } else { 316 return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 317 } 318 } 319 320 } 321 322 /** 323 * Table properties.<p> 324 */ 325 enum TableProperty { 326 327 /** Description property.*/ 328 Description(Messages.GUI_USERMANAGEMENT_OU_DESCRIPTION_0, String.class, ""), 329 /**Icon property. */ 330 Icon(null, Resource.class, new CmsCssIcon(OpenCmsTheme.ICON_OU)), 331 /** Name property. */ 332 Name(Messages.GUI_USERMANAGEMENT_OU_NAME_0, String.class, ""), 333 /** Resources property.*/ 334 Ressources(Messages.GUI_USERMANAGEMENT_OU_EDIT_PANEL2_0, String.class, ""), 335 /** Type property. */ 336 Type(null, I_CmsOuTreeType.class, CmsOuTreeType.OU); 337 338 /**Default value for column.*/ 339 private Object m_defaultValue; 340 341 /**Header Message key.*/ 342 private String m_headerMessage; 343 344 /**Type of column property.*/ 345 private Class<?> m_type; 346 347 /** 348 * constructor.<p> 349 * 350 * @param name Name 351 * @param type type 352 * @param defaultValue value 353 */ 354 TableProperty(String name, Class<?> type, Object defaultValue) { 355 356 m_headerMessage = name; 357 m_type = type; 358 m_defaultValue = defaultValue; 359 } 360 361 /** 362 * The default value.<p> 363 * 364 * @return the default value object 365 */ 366 Object getDefault() { 367 368 return m_defaultValue; 369 } 370 371 /** 372 * Gets the name of the property.<p> 373 * 374 * @return a name 375 */ 376 String getName() { 377 378 return m_headerMessage == null ? "" : CmsVaadinUtils.getMessageText(m_headerMessage); 379 380 } 381 382 /** 383 * Gets the type of property.<p> 384 * 385 * @return the type 386 */ 387 Class<?> getType() { 388 389 return m_type; 390 } 391 392 } 393 394 /** Log instance for this class. */ 395 static final Log LOG = CmsLog.getLog(CmsOUTable.class); 396 397 /**vaadin serial id. */ 398 private static final long serialVersionUID = -1080519790145391678L; 399 400 /**Calling app. */ 401 protected CmsAccountsApp m_app; 402 403 /**Parent ou. */ 404 protected String m_parentOu; 405 406 /**CmsObject. */ 407 CmsObject m_cms; 408 409 /** The context menu. */ 410 CmsContextMenu m_menu; 411 412 /**Indexed container. */ 413 private IndexedContainer m_container; 414 415 /** The available menu entries. */ 416 private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries; 417 418 /** 419 * public constructor.<p> 420 * 421 * @param ou ou 422 * @param app calling app 423 */ 424 public CmsOUTable(String ou, CmsAccountsApp app) { 425 426 m_app = app; 427 init(ou); 428 } 429 430 /** 431 * Opens the import/export dialog for a specific OU. 432 * 433 * @param cms the CMS context 434 * @param ou the OU 435 */ 436 public static void openImportExportDialog(CmsObject cms, String ou) { 437 438 boolean includeTechnicalFields = false; 439 try { 440 OpenCms.getRoleManager().checkRole(cms, CmsRole.ADMINISTRATOR); 441 includeTechnicalFields = true; 442 } catch (CmsRoleViolationException e) { 443 // ok 444 } 445 Window window = CmsBasicDialog.prepareWindow(DialogWidth.wide); 446 window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_IMEXPORT_DIALOGNAME_0)); 447 window.setContent(CmsImportExportUserDialog.getExportUserDialogForOU(ou, window, includeTechnicalFields)); 448 449 A_CmsUI.get().addWindow(window); 450 } 451 452 /** 453 * @see org.opencms.ui.apps.user.I_CmsFilterableTable#filter(java.lang.String) 454 */ 455 public void filter(String data) { 456 457 m_container.removeAllContainerFilters(); 458 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(data)) { 459 m_container.addContainerFilter( 460 new Or( 461 new SimpleStringFilter(TableProperty.Name, data, true, false), 462 new SimpleStringFilter(TableProperty.Description, data, true, false))); 463 } 464 465 } 466 467 /** 468 * @see org.opencms.ui.apps.user.I_CmsFilterableTable#getEmptyLayout() 469 */ 470 public VerticalLayout getEmptyLayout() { 471 472 VerticalLayout layout = new VerticalLayout(); 473 layout.setVisible(false); 474 return layout; 475 } 476 477 /** 478 * Checks if user has role to edit ou.<p> 479 * 480 * @return true if admin 481 */ 482 protected boolean isAdmin() { 483 484 return OpenCms.getRoleManager().hasRole(m_cms, CmsRole.ADMINISTRATOR.forOrgUnit(m_parentOu)); 485 } 486 487 /** 488 * Updates app.<p> 489 * 490 * @param itemId of current item 491 */ 492 protected void updateApp(String itemId) { 493 494 I_CmsOuTreeType foundType = null; 495 for (I_CmsOuTreeType type : m_app.getTreeTypeProvider().getTreeTypes()) { 496 if (itemId.equals(type.getId())) { 497 foundType = type; 498 break; 499 } 500 } 501 if (foundType != null) { 502 m_app.update(m_parentOu, foundType, null); 503 return; 504 } 505 m_app.update(itemId, CmsOuTreeType.OU, null, ""); 506 } 507 508 /** 509 * Returns the available menu entries.<p> 510 * 511 * @return the menu entries 512 */ 513 List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() { 514 515 if (m_menuEntries == null) { 516 m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>(); 517 m_menuEntries.add(new EntryOpen()); 518 m_menuEntries.add(new EntryEdit()); 519 m_menuEntries.add(new EntryNewGroup()); 520 m_menuEntries.add(new EntryNewUser()); 521 m_menuEntries.add(new EntryImportExportUser()); 522 m_menuEntries.add(new EntryDelete()); 523 } 524 return m_menuEntries; 525 } 526 527 /** 528 * Adds ou to table.<p> 529 * 530 * @param ou to be added 531 */ 532 private void addOuToTable(CmsOrganizationalUnit ou) { 533 534 if (m_app.isParentOfManagableOU(ou.getName())) { 535 Item item = m_container.addItem(ou.getName()); 536 if (item != null) { 537 item.getItemProperty(TableProperty.Name).setValue(ou.getName()); 538 item.getItemProperty(TableProperty.Description).setValue(ou.getDisplayName(A_CmsUI.get().getLocale())); 539 if (ou.hasFlagWebuser()) { 540 item.getItemProperty(TableProperty.Icon).setValue(new CmsCssIcon(OpenCmsTheme.ICON_OU_WEB)); 541 } 542 } 543 } 544 } 545 546 /** 547 * initializes table.<p> 548 * 549 * @param parentOu ou name 550 */ 551 private void init(String parentOu) { 552 553 m_parentOu = parentOu; 554 m_menu = new CmsContextMenu(); 555 m_menu.setAsTableContextMenu(this); 556 557 m_container = new IndexedContainer(); 558 559 setContainerDataSource(m_container); 560 561 for (TableProperty prop : TableProperty.values()) { 562 m_container.addContainerProperty(prop, prop.getType(), prop.getDefault()); 563 setColumnHeader(prop, prop.getName()); 564 } 565 setContainerDataSource(m_container); 566 setItemIconPropertyId(TableProperty.Icon); 567 setRowHeaderMode(RowHeaderMode.ICON_ONLY); 568 569 setColumnWidth(null, 40); 570 setSelectable(true); 571 572 addGeneratedColumn(TableProperty.Ressources, new ColumnGenerator() { 573 574 private static final long serialVersionUID = 4624734503799549261L; 575 576 public Object generateCell(Table source, Object itemId, Object columnId) { 577 578 String out = ""; 579 try { 580 boolean isOu = true; 581 for (I_CmsOuTreeType type : m_app.getTreeTypeProvider().getTreeTypes()) { 582 if (type.getId().equals(itemId)) { 583 isOu = false; 584 } 585 } 586 if (isOu) { 587 List<CmsResource> resources = OpenCms.getOrgUnitManager().getResourcesForOrganizationalUnit( 588 m_cms, 589 (String)itemId); 590 if (!resources.isEmpty()) { 591 out = resources.get(0).getRootPath(); 592 int i = 1; 593 while ((resources.size() > i) & (out.length() < 50)) { 594 out += ", " + resources.get(i).getRootPath(); 595 } 596 if (resources.size() > i) { 597 out += " ..."; 598 } 599 } 600 } 601 } catch (CmsException e) { 602 LOG.error("unable to read resources.", e); 603 } 604 return out; 605 } 606 }); 607 608 setVisibleColumns(TableProperty.Name, TableProperty.Description, TableProperty.Ressources); 609 610 try { 611 m_cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject()); 612 m_cms.getRequestContext().setSiteRoot(""); 613 } catch (CmsException e) { 614 m_cms = A_CmsUI.getCmsObject(); 615 } 616 try { 617 if (m_app.isOUManagable(m_parentOu)) { 618 619 for (I_CmsOuTreeType treeType : m_app.getTreeTypeProvider().getTreeTypes()) { 620 if (treeType.showInOuTable() && treeType.isValidForOu(m_cms, m_parentOu)) { 621 Item item = m_container.addItem(treeType.getId()); 622 item.getItemProperty(TableProperty.Name).setValue(treeType.getName()); 623 item.getItemProperty(TableProperty.Icon).setValue(treeType.getIcon()); 624 item.getItemProperty(TableProperty.Type).setValue(treeType); 625 } 626 } 627 } 628 List<CmsOrganizationalUnit> webOus = new ArrayList<CmsOrganizationalUnit>(); 629 for (CmsOrganizationalUnit ou : OpenCms.getOrgUnitManager().getOrganizationalUnits( 630 m_cms, 631 parentOu, 632 false)) { 633 634 if (ou.hasFlagWebuser()) { 635 webOus.add(ou); 636 } else { 637 addOuToTable(ou); 638 } 639 } 640 for (CmsOrganizationalUnit ou : webOus) { 641 addOuToTable(ou); 642 } 643 } catch (CmsException e) { 644 LOG.error("Unable to read ous", e); 645 } 646 647 addItemClickListener(new ItemClickListener() { 648 649 private static final long serialVersionUID = 4807195510202231174L; 650 651 public void itemClick(ItemClickEvent event) { 652 653 setValue(null); 654 select(event.getItemId()); 655 if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) { 656 m_menu.setEntries(getMenuEntries(), Collections.singleton((String)getValue())); 657 m_menu.openForTable(event, event.getItemId(), event.getPropertyId(), CmsOUTable.this); 658 return; 659 } 660 if (event.getButton().equals(MouseButton.LEFT) && event.getPropertyId().equals(TableProperty.Name)) { 661 updateApp((String)getValue()); 662 } 663 664 } 665 666 }); 667 setCellStyleGenerator(new CellStyleGenerator() { 668 669 private static final long serialVersionUID = 1L; 670 671 public String getStyle(Table source, Object itemId, Object propertyId) { 672 673 if (TableProperty.Name.equals(propertyId)) { 674 return " " + OpenCmsTheme.HOVER_COLUMN; 675 } 676 677 return ""; 678 } 679 }); 680 } 681}