View Javadoc
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.admin.taxonomy.info;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import javax.jcr.AccessDeniedException;
27  import javax.jcr.Node;
28  import javax.jcr.Session;
29  
30  import org.exoplatform.commons.utils.LazyPageList;
31  import org.exoplatform.commons.utils.ListAccess;
32  import org.exoplatform.commons.utils.ListAccessImpl;
33  import org.exoplatform.ecm.webui.core.UIPermissionInfoBase;
34  import org.exoplatform.ecm.webui.utils.PermissionUtil;
35  import org.exoplatform.ecm.webui.utils.Utils;
36  import org.exoplatform.services.jcr.RepositoryService;
37  import org.exoplatform.services.jcr.access.AccessControlEntry;
38  import org.exoplatform.services.jcr.access.PermissionType;
39  import org.exoplatform.services.jcr.core.ExtendedNode;
40  import org.exoplatform.services.jcr.core.ManageableRepository;
41  import org.exoplatform.services.security.IdentityConstants;
42  import org.exoplatform.services.wcm.core.NodeLocation;
43  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
44  import org.exoplatform.web.application.ApplicationMessage;
45  import org.exoplatform.webui.config.annotation.ComponentConfig;
46  import org.exoplatform.webui.config.annotation.EventConfig;
47  import org.exoplatform.webui.core.UIApplication;
48  import org.exoplatform.webui.core.UIGrid;
49  import org.exoplatform.webui.core.UIPopupContainer;
50  import org.exoplatform.webui.core.lifecycle.UIContainerLifecycle;
51  import org.exoplatform.webui.event.Event;
52  import org.exoplatform.webui.event.EventListener;
53  
54  /**
55   * Created by The eXo Platform SARL
56   * Author : nqhungvn
57   *          nguyenkequanghung@yahoo.com
58   * July 3, 2006
59   * 10:07:15 AM
60   * Editor : TuanP
61   *        phamtuanchip@yahoo.de
62   * Oct 13, 20006
63   */
64  
65  @ComponentConfig(
66      lifecycle = UIContainerLifecycle.class,
67      events = {
68        @EventConfig (listeners = UIPermissionInfo.DeleteActionListener.class,
69                      confirm = "UIPermissionInfo.msg.confirm-delete-permission"),
70        @EventConfig (listeners = UIPermissionInfo.EditActionListener.class)
71      }
72  )
73  
74  public class UIPermissionInfo extends UIPermissionInfoBase {
75    
76    private NodeLocation currentNode = null;
77    private int sizeOfListPermission = 0;
78    public UIPermissionInfo() throws Exception {
79      super();
80    }
81    public void updateGrid() throws Exception {
82      List<PermissionBean> permBeans = new ArrayList<PermissionBean>();
83      ExtendedNode node = (ExtendedNode) getCurrentNode();
84      List permsList = node.getACL().getPermissionEntries() ;
85      Map<String, List<String>> permsMap = new HashMap<String, List<String>>() ;
86      Iterator perIter = permsList.iterator() ;
87      while(perIter.hasNext()) {
88        AccessControlEntry accessControlEntry = (AccessControlEntry)perIter.next() ;
89        String currentIdentity = accessControlEntry.getIdentity();
90        String currentPermission = accessControlEntry.getPermission();
91        List<String> currentPermissionsList = permsMap.get(currentIdentity);
92        if(!permsMap.containsKey(currentIdentity)) {
93          permsMap.put(currentIdentity, null) ;
94        }
95        if(currentPermissionsList == null) currentPermissionsList = new ArrayList<String>() ;
96        if(!currentPermissionsList.contains(currentPermission)) {
97          currentPermissionsList.add(currentPermission) ;
98        }
99        permsMap.put(currentIdentity, currentPermissionsList) ;
100     }
101     Set keys = permsMap.keySet();
102     Iterator keysIter = keys.iterator() ;
103     //TODO Utils.getExoOwner(node) has exception return SystemIdentity.SYSTEM
104     String owner = IdentityConstants.SYSTEM ;
105     int iSystemOwner = 0;
106     if (getExoOwner(node) != null) owner = getExoOwner(node);
107     if (owner.equals(IdentityConstants.SYSTEM)) iSystemOwner = -1;
108     PermissionBean permOwnerBean = new PermissionBean();
109     if(!permsMap.containsKey(owner)) {
110       permOwnerBean.setUsersOrGroups(owner);
111       permOwnerBean.setRead(true) ;
112       permOwnerBean.setAddNode(true) ;
113       permOwnerBean.setRemove(true) ;
114       permBeans.add(permOwnerBean);
115     }
116     while(keysIter.hasNext()) {
117       String userOrGroup = (String) keysIter.next();
118       List<String> permissions = permsMap.get(userOrGroup);
119       PermissionBean permBean = new PermissionBean();
120       permBean.setUsersOrGroups(userOrGroup);
121       for(String perm : permissions) {
122         if(PermissionType.READ.equals(perm)) permBean.setRead(true);
123         else if(PermissionType.ADD_NODE.equals(perm)) permBean.setAddNode(true);
124         else if(PermissionType.REMOVE.equals(perm)) permBean.setRemove(true);
125       }
126       permBeans.add(permBean);
127     }
128     sizeOfListPermission = permBeans.size() + iSystemOwner;
129     UIGrid uiGrid = findFirstComponentOfType(UIGrid.class) ;
130     ListAccess<PermissionBean> permList = new ListAccessImpl<PermissionBean>(PermissionBean.class,
131                                                                              permBeans);
132     LazyPageList<PermissionBean> dataPageList = new LazyPageList<PermissionBean>(permList, 10);
133     uiGrid.getUIPageIterator().setPageList(dataPageList);
134   }
135   private Session getSession() throws Exception {
136     RepositoryService repositoryService  = getApplicationComponent(RepositoryService.class) ;
137     String systemWorkspace = repositoryService.getCurrentRepository().getConfiguration().getSystemWorkspaceName();
138     ManageableRepository manageableRepository = repositoryService.getCurrentRepository();
139     Session session = WCMCoreUtils.getSystemSessionProvider().getSession(systemWorkspace, manageableRepository);
140     return session;
141   }
142 
143   static public class DeleteActionListener extends EventListener<UIPermissionInfo> {
144     public void execute(Event<UIPermissionInfo> event) throws Exception {
145       UIPermissionInfo uicomp = event.getSource() ;
146       Node currentNode = uicomp.getCurrentNode();
147       ExtendedNode node = (ExtendedNode)currentNode;
148       String owner = IdentityConstants.SYSTEM ;
149       int iSystemOwner = 0;
150       if (uicomp.getExoOwner(node) != null) owner = uicomp.getExoOwner(node);
151       if (owner.equals(IdentityConstants.SYSTEM)) iSystemOwner = -1;
152       String name = event.getRequestContext().getRequestParameter(OBJECTID) ;
153       UIApplication uiApp = uicomp.getAncestorOfType(UIApplication.class) ;
154       if (uicomp.getSizeOfListPermission() < 2 + iSystemOwner) {
155           uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.no-permission-remove",
156               null, ApplicationMessage.WARNING));
157           
158           return;
159       }
160       if(!currentNode.isCheckedOut()) {
161         uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.node-checkedin", null,
162             ApplicationMessage.WARNING)) ;
163         
164         return ;
165       }
166       String nodeOwner = Utils.getNodeOwner(node);
167       if(name.equals(nodeOwner)) {
168         uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.no-permission-remove", null,
169                                                 ApplicationMessage.WARNING)) ;
170         
171         return ;
172       }
173       if(PermissionUtil.canChangePermission(node)) {
174         if(node.canAddMixin("exo:privilegeable"))  {
175           node.addMixin("exo:privilegeable");
176           node.setPermission(nodeOwner,PermissionType.ALL);
177           node.save();
178         }
179         try {
180           node.removePermission(name);
181           node.save();
182         } catch(AccessDeniedException ace) {
183           uicomp.getSession().refresh(false) ;
184           uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.access-denied", null,
185                                                   ApplicationMessage.WARNING)) ;
186           
187           return ;
188         }
189         Session session = uicomp.getSession();
190         session.save();
191         session.refresh(false);
192       } else {
193         uiApp.addMessage(new ApplicationMessage("UIPermissionInfo.msg.no-permission-tochange", null,
194             ApplicationMessage.WARNING)) ;
195         
196         return ;
197       }
198       UIPopupContainer uiPopup = uicomp.getAncestorOfType(UIPopupContainer.class) ;
199       if(!PermissionUtil.canRead(node)) {
200         uiPopup.deActivate() ;
201       } else {
202         uicomp.updateGrid();
203         event.getRequestContext().addUIComponentToUpdateByAjax(uicomp.getParent()) ;
204       }
205       event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup) ;
206     }
207   }
208 
209   public class PermissionBean {
210 
211     private String usersOrGroups ;
212     private boolean read ;
213     private boolean addNode ;
214     private boolean remove ;
215 
216     public String getUsersOrGroups() { return usersOrGroups ; }
217     public void setUsersOrGroups(String s) { usersOrGroups = s ; }
218 
219     public boolean isAddNode() { return addNode ; }
220     public void setAddNode(boolean b) { addNode = b ; }
221 
222     public boolean isRead() { return read ; }
223     public void setRead(boolean b) { read = b ; }
224 
225     public boolean isRemove() { return remove ; }
226     public void setRemove(boolean b) { remove = b ; }
227 
228   }
229 
230   public Node getCurrentNode() {
231     return NodeLocation.getNodeByLocation(currentNode);
232   }
233   public void setCurrentNode(Node currentNode) {
234     this.currentNode = NodeLocation.getNodeLocationByNode(currentNode);
235   }
236   public int getSizeOfListPermission() {
237     return sizeOfListPermission;
238   }
239   public void setSizeOfListPermission(int sizeOfListPermission) {
240     this.sizeOfListPermission = sizeOfListPermission;
241   }
242 }
243