View Javadoc
1   /***************************************************************************
2    * Copyright (C) 2003-2009 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   **************************************************************************/
18  package org.exoplatform.ecm.webui.component.admin.taxonomy.tree.info;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.jcr.AccessDeniedException;
29  import javax.jcr.Node;
30  
31  import org.exoplatform.commons.utils.LazyPageList;
32  import org.exoplatform.commons.utils.ListAccess;
33  import org.exoplatform.commons.utils.ListAccessImpl;
34  import org.exoplatform.ecm.webui.core.UIPermissionInfoBase;
35  import org.exoplatform.ecm.webui.core.bean.PermissionBean;
36  import org.exoplatform.ecm.webui.utils.PermissionUtil;
37  import org.exoplatform.ecm.webui.utils.Utils;
38  import org.exoplatform.services.cms.taxonomy.TaxonomyService;
39  import org.exoplatform.services.jcr.access.AccessControlEntry;
40  import org.exoplatform.services.jcr.access.PermissionType;
41  import org.exoplatform.services.jcr.core.ExtendedNode;
42  import org.exoplatform.services.security.IdentityConstants;
43  import org.exoplatform.services.wcm.core.NodeLocation;
44  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
45  import org.exoplatform.web.application.ApplicationMessage;
46  import org.exoplatform.webui.config.annotation.ComponentConfig;
47  import org.exoplatform.webui.config.annotation.EventConfig;
48  import org.exoplatform.webui.core.UIApplication;
49  import org.exoplatform.webui.core.UIContainer;
50  import org.exoplatform.webui.core.UIGrid;
51  import org.exoplatform.webui.core.UIPopupContainer;
52  import org.exoplatform.webui.core.lifecycle.UIContainerLifecycle;
53  import org.exoplatform.webui.event.Event;
54  import org.exoplatform.webui.event.EventListener;
55  
56  /**
57   * Created by The eXo Platform SARL
58   * Author : Hoang Van Hung
59   *          hunghvit@gmail.com
60   * Apr 17, 2009
61   */
62  
63  @ComponentConfig(
64      lifecycle = UIContainerLifecycle.class,
65      events = {
66        @EventConfig (listeners = UIPermissionTreeInfo.DeleteActionListener.class, 
67                      confirm = "UIPermissionTreeInfo.msg.confirm-delete-permission"),
68        @EventConfig (listeners = UIPermissionTreeInfo.EditActionListener.class)
69      }
70  )
71  public class UIPermissionTreeInfo extends UIPermissionInfoBase {
72  
73    public static String[]  PERMISSION_BEAN_FIELD = { "usersOrGroups", "read", "addNode", "remove"};
74  
75    private static String[] PERMISSION_ACTION     = { "Edit", "Delete" };
76  
77    private NodeLocation            currentNode           = null;
78  
79    private int             sizeOfListPermission  = 0;
80  
81    private List<PermissionBean> permBeans = new ArrayList<PermissionBean>();
82  
83    public UIPermissionTreeInfo() throws Exception {
84      super();
85      
86      // Set default permission tree
87      permBeans = this.getDefaultPermissions();
88    }
89  
90    public void updateGrid() throws Exception {
91      Map<String, List<String>> permsMap = new HashMap<String, List<String>>();
92      int iSystemOwner = 0;
93      if (currentNode != null) {
94        permBeans = new ArrayList<PermissionBean>();
95        ExtendedNode node = (ExtendedNode) getCurrentNode();
96        List permsList = node.getACL().getPermissionEntries();
97        Iterator perIter = permsList.iterator();
98        while (perIter.hasNext()) {
99          AccessControlEntry accessControlEntry = (AccessControlEntry) perIter.next();
100         String currentIdentity = accessControlEntry.getIdentity();
101         String currentPermission = accessControlEntry.getPermission();
102         List<String> currentPermissionsList = permsMap.get(currentIdentity);
103         if (!permsMap.containsKey(currentIdentity)) {
104           permsMap.put(currentIdentity, null);
105         }
106         if (currentPermissionsList == null)
107           currentPermissionsList = new ArrayList<String>();
108         if (!currentPermissionsList.contains(currentPermission)) {
109           currentPermissionsList.add(currentPermission);
110         }
111         permsMap.put(currentIdentity, currentPermissionsList);
112       }
113       Set keys = permsMap.keySet();
114       Iterator keysIter = keys.iterator();
115       String owner = IdentityConstants.SYSTEM;
116 
117       if (getExoOwner(node) != null) owner = getExoOwner(node);
118       if (owner.equals(IdentityConstants.SYSTEM)) iSystemOwner = -1;
119       PermissionBean permOwnerBean = new PermissionBean();
120       if(!permsMap.containsKey(owner)) {
121         permOwnerBean.setUsersOrGroups(owner);
122         permOwnerBean.setRead(true);
123         permOwnerBean.setAddNode(true);
124 //        permOwnerBean.setSetProperty(true);
125         permOwnerBean.setRemove(true);
126         permBeans.add(permOwnerBean);
127       }
128       while(keysIter.hasNext()) {
129         String userOrGroup = (String) keysIter.next();
130         List<String> permissions = permsMap.get(userOrGroup);
131         PermissionBean permBean = new PermissionBean();
132         permBean.setUsersOrGroups(userOrGroup);
133         for(String perm : permissions) {
134           if(PermissionType.READ.equals(perm)) permBean.setRead(true);
135           else if(PermissionType.ADD_NODE.equals(perm)) permBean.setAddNode(true);
136 //          else if(PermissionType.SET_PROPERTY.equals(perm)) permBean.setSetProperty(true);
137           else if(PermissionType.REMOVE.equals(perm)) permBean.setRemove(true);
138         }
139         permBeans.add(permBean);
140       }
141     } else {
142       UIPermissionTreeForm uiForm = ((UIContainer)getParent()).getChild(UIPermissionTreeForm.class);
143       PermissionBean permBean = uiForm.getPermBean();
144       if (permBean != null) {
145         for (PermissionBean permBeanTemp : permBeans) {
146           if(permBeanTemp.equals(permBean)) {
147             permBeanTemp.setAddNode(permBean.isAddNode());
148             permBeanTemp.setRead(permBean.isRead());
149             permBeanTemp.setRemove(permBean.isRemove());
150 //            permBeanTemp.setSetProperty(permBean.isSetProperty());
151           }
152         }
153         if (!permBeans.contains(permBean)) {
154           permBeans.add(permBean);
155         }
156         uiForm.setPermBean(null);
157       }
158     }
159     sizeOfListPermission = permBeans.size() + iSystemOwner;
160     UIGrid uiGrid = findFirstComponentOfType(UIGrid.class);
161     ListAccess<PermissionBean> permList = new ListAccessImpl<PermissionBean>(PermissionBean.class,
162                                                                              permBeans);
163     LazyPageList<PermissionBean> dataPageList = new LazyPageList<PermissionBean>(permList, 10);
164     uiGrid.getUIPageIterator().setPageList(dataPageList);
165   }
166   
167   /**
168    * Get default permissions for taxonomy node
169    */
170   private List<PermissionBean> getDefaultPermissions() {
171     Set<PermissionBean> defaultPermissionList = new HashSet<PermissionBean>();
172     
173     // From taxonomy tree default permissions
174     TaxonomyService taxonomyService = WCMCoreUtils.getService(TaxonomyService.class);
175     Map<String, String[]> defaultTaxonomyTreePerms =  taxonomyService.getTaxonomyTreeDefaultUserPermission();
176     PermissionBean bean;
177     for(Map.Entry<String, String[]> entry : defaultTaxonomyTreePerms.entrySet()) {
178       bean = new PermissionBean();
179       bean.setUsersOrGroups(entry.getKey());
180       bean.setPermissions(entry.getValue());
181       defaultPermissionList.add(bean);
182     }
183 
184     return new ArrayList<PermissionBean>(defaultPermissionList);
185   }
186   
187   public static class DeleteActionListener extends EventListener<UIPermissionTreeInfo> {
188     public void execute(Event<UIPermissionTreeInfo> event) throws Exception {
189       UIPermissionTreeInfo uicomp = event.getSource();
190       UIPermissionTreeManager uiParent = uicomp.getParent();
191       Node currentNode = uicomp.getCurrentNode();
192       ExtendedNode node = (ExtendedNode)currentNode;
193       String owner = IdentityConstants.SYSTEM;
194       int iSystemOwner = 0;
195       if (uicomp.getExoOwner(node) != null) owner = uicomp.getExoOwner(node);
196       if (owner.equals(IdentityConstants.SYSTEM)) iSystemOwner = -1;
197       String name = event.getRequestContext().getRequestParameter(OBJECTID);
198       UIApplication uiApp = uicomp.getAncestorOfType(UIApplication.class);
199       UIPopupContainer uiPopup = uicomp.getAncestorOfType(UIPopupContainer.class);
200       if (currentNode != null) {
201         if (uicomp.getSizeOfListPermission() < 2 + iSystemOwner) {
202             uiApp.addMessage(new ApplicationMessage("UIPermissionTreeInfo.msg.no-permission-remove",
203                 null, ApplicationMessage.WARNING));
204             
205             return;
206         }
207         if(!currentNode.isCheckedOut()) {
208           uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.node-checkedin", null,
209               ApplicationMessage.WARNING)) ;
210           
211           return ;
212         }
213         String nodeOwner = Utils.getNodeOwner(node);
214         if(name.equals(nodeOwner)) {
215           uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.no-permission-remove", null,
216                                                   ApplicationMessage.WARNING)) ;
217           
218           return ;
219         }
220         if(PermissionUtil.canChangePermission(node)) {
221           if(node.canAddMixin("exo:privilegeable"))  {
222             node.addMixin("exo:privilegeable");
223             node.setPermission(nodeOwner,PermissionType.ALL);
224             node.save();
225           }
226           try {
227             node.removePermission(name);
228             node.save();
229           } catch(AccessDeniedException ace) {
230             node.getSession().refresh(false) ;
231             uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.access-denied", null,
232                                                     ApplicationMessage.WARNING)) ;
233             
234             return ;
235           }
236           node.getSession().save();
237           node.getSession().refresh(false);
238         } else {
239           uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.no-permission-tochange", null,
240               ApplicationMessage.WARNING)) ;
241           
242           return ;
243         }
244         if(!PermissionUtil.canRead(node)) {
245           uiPopup.deActivate() ;
246         } else {
247           uicomp.updateGrid();
248           event.getRequestContext().addUIComponentToUpdateByAjax(uiParent);
249         }
250       } else {
251         PermissionBean permBean = new PermissionBean();
252         permBean.setUsersOrGroups(name);
253         if (uicomp.getPermBeans().contains(permBean)) {
254           uicomp.getPermBeans().remove(permBean);
255           uicomp.updateGrid();
256         }
257       }
258       event.getRequestContext().addUIComponentToUpdateByAjax(uiParent) ;
259     }
260   }
261 
262   public Node getCurrentNode() {
263     return NodeLocation.getNodeByLocation(currentNode);
264   }
265 
266   public void setCurrentNode(Node currentNode) {
267     this.currentNode = NodeLocation.getNodeLocationByNode(currentNode);
268   }
269 
270   public int getSizeOfListPermission() {
271     return sizeOfListPermission;
272   }
273 
274   public void setSizeOfListPermission(int sizeOfListPermission) {
275     this.sizeOfListPermission = sizeOfListPermission;
276   }
277 
278   public List<PermissionBean> getPermBeans() {
279     return permBeans;
280   }
281 
282   public void setPermBeans(List<PermissionBean> permBeans) {
283     this.permBeans = permBeans;
284   }
285 }