UIPermissionInfo.java

  1. /*
  2.  * Copyright (C) 2003-2007 eXo Platform SAS.
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Affero General Public License
  6.  * as published by the Free Software Foundation; either version 3
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, see<http://www.gnu.org/licenses/>.
  16.  */
  17. package org.exoplatform.ecm.webui.component.explorer.popup.info;

  18. import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
  19. import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorerPortlet;
  20. import org.exoplatform.ecm.webui.core.UIPermissionInfoBase;
  21. import org.exoplatform.ecm.webui.utils.PermissionUtil;
  22. import org.exoplatform.ecm.webui.utils.Utils;
  23. import org.exoplatform.services.cms.link.LinkManager;
  24. import org.exoplatform.services.cms.link.LinkUtils;
  25. import org.exoplatform.services.jcr.access.PermissionType;
  26. import org.exoplatform.services.jcr.core.ExtendedNode;
  27. import org.exoplatform.services.log.ExoLogger;
  28. import org.exoplatform.services.log.Log;
  29. import org.exoplatform.services.security.IdentityConstants;
  30. import org.exoplatform.services.wcm.core.NodetypeConstant;
  31. import org.exoplatform.web.application.ApplicationMessage;
  32. import org.exoplatform.webui.config.annotation.ComponentConfig;
  33. import org.exoplatform.webui.config.annotation.EventConfig;
  34. import org.exoplatform.webui.core.UIApplication;
  35. import org.exoplatform.webui.core.UIGrid;
  36. import org.exoplatform.webui.core.UIPopupContainer;
  37. import org.exoplatform.webui.core.lifecycle.UIContainerLifecycle;
  38. import org.exoplatform.webui.event.Event;
  39. import org.exoplatform.webui.event.EventListener;

  40. import javax.jcr.AccessDeniedException;
  41. import javax.jcr.Node;
  42. import java.util.List;

  43. @ComponentConfig(lifecycle = UIContainerLifecycle.class, events = {
  44.     @EventConfig(listeners = UIPermissionInfo.DeleteActionListener.class,
  45.         confirm = "UIPermissionInfo.msg.confirm-delete-permission"),
  46.     @EventConfig(listeners = UIPermissionInfo.EditActionListener.class) })
  47. public class UIPermissionInfo extends UIPermissionInfoBase {

  48.   public UIPermissionInfo() throws Exception {
  49.     super();
  50.   }

  51.   private static final Log LOG = ExoLogger.getLogger(UIPermissionInfo.class.getName());

  52.   static public class DeleteActionListener extends EventListener<UIPermissionInfo> {
  53.     public void execute(Event<UIPermissionInfo> event) throws Exception {
  54.       UIPermissionInfo uicomp = event.getSource() ;
  55.       UIJCRExplorer uiJCRExplorer = uicomp.getAncestorOfType(UIJCRExplorer.class) ;
  56.       Node currentNode = uiJCRExplorer.getCurrentNode() ;
  57.       uiJCRExplorer.addLockToken(currentNode);
  58.       ExtendedNode node = (ExtendedNode)currentNode;
  59.       String owner = IdentityConstants.SYSTEM ;
  60.       int iSystemOwner = 0;
  61.       if (uicomp.getExoOwner(node) != null) owner = uicomp.getExoOwner(node);
  62.       if (owner.equals(IdentityConstants.SYSTEM)) iSystemOwner = -1;
  63.       UIApplication uiApp = uicomp.getAncestorOfType(UIApplication.class) ;
  64.       if (uicomp.getSizeOfListPermission() < 2 + iSystemOwner) {
  65.         uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.no-permission-remove",
  66.             null, ApplicationMessage.WARNING));

  67.         return;
  68.       }
  69.       String name = event.getRequestContext().getRequestParameter(OBJECTID) ;
  70.       if(!currentNode.isCheckedOut()) {
  71.         uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.node-checkedin", null,
  72.             ApplicationMessage.WARNING)) ;

  73.         return ;
  74.       }
  75.       String nodeOwner = Utils.getNodeOwner(node);
  76.       if(name.equals(nodeOwner)) {
  77.         uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.no-permission-remove", null,
  78.                 ApplicationMessage.WARNING)) ;
  79.         return ;
  80.       }
  81.       if(PermissionUtil.canChangePermission(node)) {
  82.         if(node.canAddMixin("exo:privilegeable"))  {
  83.           node.addMixin("exo:privilegeable");
  84.           node.setPermission(nodeOwner,PermissionType.ALL);
  85.         }
  86.         try {
  87.           node.removePermission(name) ;
  88.           node.save() ;
  89.         } catch(AccessDeniedException ace) {
  90.           node.getSession().refresh(false) ;
  91.           uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.access-denied", null,
  92.                                                   ApplicationMessage.WARNING)) ;

  93.           return ;
  94.         }
  95.         if(uiJCRExplorer.getRootNode().equals(node)) {
  96.           if(!PermissionUtil.canRead(currentNode)) {
  97.             uiJCRExplorer.getAncestorOfType(UIJCRExplorerPortlet.class).reloadWhenBroken(uiJCRExplorer) ;
  98.             return ;
  99.           }
  100.         }
  101.         node.getSession().save() ;
  102.       } else {
  103.         uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.no-permission-tochange", null,
  104.             ApplicationMessage.WARNING)) ;

  105.         return ;
  106.       }
  107.       UIPopupContainer uiPopup = uicomp.getAncestorOfType(UIPopupContainer.class) ;
  108.       if(!PermissionUtil.canRead(node)) {
  109.         uiJCRExplorer.setSelectNode(LinkUtils.getParentPath(uiJCRExplorer.getCurrentPath()));
  110.         uiPopup.deActivate() ;
  111.       } else {
  112.         uicomp.updateGrid(uicomp.getChild(UIGrid.class).getUIPageIterator().getCurrentPage());
  113.         event.getRequestContext().addUIComponentToUpdateByAjax(uicomp.getParent()) ;
  114.       }
  115.       Node realNode = uiJCRExplorer.getRealCurrentNode();
  116.       LinkManager linkManager = uiJCRExplorer.getApplicationComponent(LinkManager.class);
  117.       if (linkManager.isLink(realNode)) {
  118.         // Reset the permissions
  119.         linkManager.updateLink(realNode, currentNode);
  120.       }

  121.       if(currentNode.isNodeType(NodetypeConstant.MIX_REFERENCEABLE)){
  122.         List<Node> symlinks = linkManager.getAllLinks(currentNode, "exo:symlink");
  123.         for (Node symlink : symlinks) {
  124.           try {
  125.             linkManager.updateLink(symlink, currentNode);
  126.           } catch (Exception e) {
  127.             if (LOG.isWarnEnabled()) {
  128.               LOG.warn(e.getMessage());
  129.             }
  130.           }
  131.         }
  132.       }
  133.       currentNode.getSession().save();
  134.       uiJCRExplorer.setIsHidePopup(true) ;
  135.       if(!PermissionUtil.canRead(currentNode)){
  136.         uiPopup.cancelPopupAction();
  137.         uiJCRExplorer.refreshExplorer(currentNode.getSession().getRootNode(), true);
  138.       }else {
  139.         uiJCRExplorer.refreshExplorer(currentNode, false);
  140.       }
  141.       event.getRequestContext().addUIComponentToUpdateByAjax(uiJCRExplorer) ;
  142.     }
  143.   }

  144.   public Node getCurrentNode() throws Exception {
  145.     return this.getAncestorOfType(UIJCRExplorer.class).getCurrentNode();
  146.   }
  147. }