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.Iterator;
21  import java.util.List;
22  
23  import javax.jcr.AccessDeniedException;
24  import javax.jcr.Node;
25  
26  import org.exoplatform.ecm.permission.info.UIPermissionInputSet;
27  import org.exoplatform.ecm.webui.core.UIPermissionFormBase;
28  import org.exoplatform.ecm.webui.selector.UIGroupMemberSelector;
29  import org.exoplatform.ecm.webui.selector.UISelectable;
30  import org.exoplatform.ecm.utils.lock.LockUtil;
31  import org.exoplatform.ecm.webui.utils.PermissionUtil;
32  import org.exoplatform.ecm.webui.utils.Utils;
33  import org.exoplatform.services.jcr.access.AccessControlEntry;
34  import org.exoplatform.services.jcr.access.PermissionType;
35  import org.exoplatform.services.jcr.core.ExtendedNode;
36  import org.exoplatform.services.log.ExoLogger;
37  import org.exoplatform.services.log.Log;
38  import org.exoplatform.services.security.IdentityConstants;
39  import org.exoplatform.services.wcm.core.NodeLocation;
40  import org.exoplatform.web.application.ApplicationMessage;
41  import org.exoplatform.webui.config.annotation.ComponentConfig;
42  import org.exoplatform.webui.config.annotation.EventConfig;
43  import org.exoplatform.webui.core.UIApplication;
44  import org.exoplatform.webui.core.UIPopupContainer;
45  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
46  import org.exoplatform.webui.event.Event;
47  import org.exoplatform.webui.event.Event.Phase;
48  import org.exoplatform.webui.event.EventListener;
49  
50  /**
51   * Created by The eXo Platform SARL Author : nqhungvn
52   * nguyenkequanghung@yahoo.com July 3, 2006 10:07:15 AM Editor : tuanp
53   * phamtuanchip@yahoo.de Oct 13, 2006
54   */
55  @ComponentConfig(
56      lifecycle = UIFormLifecycle.class,
57      template = "classpath:groovy/wcm/webui/core/UIPermissionForm.gtmpl",
58      events = {
59        @EventConfig(listeners = UIPermissionForm.SaveActionListener.class),
60        @EventConfig(phase = Phase.DECODE, listeners = UIPermissionForm.CloseActionListener.class),
61        @EventConfig(phase = Phase.DECODE, listeners = UIPermissionForm.ResetActionListener.class),
62        @EventConfig(phase = Phase.DECODE, listeners = UIPermissionForm.SelectUserActionListener.class),
63        @EventConfig(phase = Phase.DECODE, listeners = UIPermissionForm.SelectMemberActionListener.class),
64        @EventConfig(phase = Phase.DECODE, listeners = UIPermissionForm.AddAnyActionListener.class),
65        @EventConfig(phase = Phase.DECODE, listeners = UIPermissionInputSet.OnChangeActionListener.class)
66      }
67  )
68  
69  public class UIPermissionForm extends UIPermissionFormBase implements UISelectable {
70  
71    public static final String PERMISSION   = "permission";
72  
73    public static final String POPUP_SELECT = "SelectUserOrGroup";
74    
75    private static final String[] PERMISSION_TYPES = {PermissionType.READ, PermissionType.ADD_NODE, PermissionType.REMOVE};
76  
77    public static final String SELECT_GROUP_ID = "TaxoSelectUserOrGroup";
78    private static final Log LOG  = ExoLogger.getLogger(UIPermissionForm.class.getName());
79    private NodeLocation  currentNode;
80  
81    public UIPermissionForm() throws Exception {
82      addChild(new UIPermissionInputSet(PERMISSION));
83      setActions(new String[] { "Close" });
84    }
85  
86    protected boolean isEditable(Node node) throws Exception {
87      return PermissionUtil.canChangePermission(node);
88    }
89    public void fillForm(String user, ExtendedNode node) throws Exception {
90      UIPermissionInputSet uiInputSet = getChildById(PERMISSION) ;
91      refresh() ;
92      uiInputSet.getUIStringInput(UIPermissionInputSet.FIELD_USERORGROUP).setValue(user) ;
93  
94      if(user.equals(Utils.getNodeOwner(node))) {
95        for (String perm : PERMISSION_TYPES) {
96          uiInputSet.getUICheckBoxInput(perm).setChecked(true) ;
97        }
98      } else {
99        List<AccessControlEntry> permsList = node.getACL().getPermissionEntries() ;
100       Iterator<AccessControlEntry> perIter = permsList.iterator() ;
101       StringBuilder userPermission = new StringBuilder() ;
102       while(perIter.hasNext()) {
103         AccessControlEntry accessControlEntry = perIter.next() ;
104         if(user.equals(accessControlEntry.getIdentity())) {
105           userPermission.append(accessControlEntry.getPermission()).append(" ");
106         }
107       }
108       for (String perm : PERMISSION_TYPES) {
109         boolean isCheck = userPermission.toString().contains(perm) ;
110         uiInputSet.getUICheckBoxInput(perm).setChecked(isCheck) ;
111       }
112     }
113 
114   }
115 
116   public void doSelect(String selectField, Object value) {
117     try {
118       ExtendedNode node = (ExtendedNode)this.getCurrentNode();
119       checkAll(false);
120       fillForm(value.toString(), node) ;
121       getUIStringInput(selectField).setValue(value.toString());
122     } catch (Exception e) {
123       if (LOG.isErrorEnabled()) {
124         LOG.error("Unexpected error", e);
125       }
126     }
127   }
128 
129   static public class ResetActionListener extends EventListener<UIPermissionForm> {
130     public void execute(Event<UIPermissionForm> event) throws Exception {
131       UIPermissionForm uiForm = event.getSource();
132       uiForm.refresh() ;
133       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
134     }
135   }
136   static public class SaveActionListener extends EventListener<UIPermissionForm> {
137     public void execute(Event<UIPermissionForm> event) throws Exception {
138       UIPermissionForm uiForm = event.getSource();
139       Node currentNode = uiForm.getCurrentNode();
140       UIPermissionManager uiParent = uiForm.getParent();
141       UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class);
142       String userOrGroup = uiForm.getChild(UIPermissionInputSet.class).getUIStringInput(
143           UIPermissionInputSet.FIELD_USERORGROUP).getValue();
144       List<String> permsList = new ArrayList<String>();
145       List<String> permsRemoveList = new ArrayList<String>();
146       if (currentNode.isLocked()) {
147         String lockToken = LockUtil.getLockToken(currentNode);
148         if (lockToken != null)
149           currentNode.getSession().addLockToken(lockToken);
150       }
151       if (!currentNode.isCheckedOut()) {
152         uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.node-checkedin",
153                                                 null,
154                                                 ApplicationMessage.WARNING));
155         
156         return;
157       }
158       for (String perm : PermissionType.ALL) {
159         if (uiForm.getUICheckBoxInput(perm) != null &&
160             uiForm.getUICheckBoxInput(perm).isChecked()) permsList.add(perm);
161         else permsRemoveList.add(perm);
162       }
163     //check both ADD_NODE and SET_PROPERTY^M
164       if (uiForm.getUICheckBoxInput(PermissionType.ADD_NODE).isChecked()) {
165         if(!permsList.contains(PermissionType.SET_PROPERTY))
166           permsList.add(PermissionType.SET_PROPERTY);
167       }
168 
169       //uncheck both ADD_NODE and SET_PROPERTY^M
170       if (!uiForm.getUICheckBoxInput(PermissionType.ADD_NODE).isChecked()) {
171         if(!permsRemoveList.contains(PermissionType.SET_PROPERTY))
172           permsRemoveList.add(PermissionType.SET_PROPERTY);
173       }
174 
175       //------------------^M
176       if (Utils.isNameEmpty(userOrGroup)) {
177         uiApp.addMessage(new ApplicationMessage("UIPermissionForm.msg.userOrGroup-required",
178                                                 null,
179                                                 ApplicationMessage.WARNING));
180         
181         return;
182       }
183       if (permsList.size() == 0) {
184         uiApp.addMessage(new ApplicationMessage("UIPermissionForm.msg.checkbox-require",
185                                                 null,
186                                                 ApplicationMessage.WARNING));
187         
188         return;
189       }
190       String[] permsArray = permsList.toArray(new String[permsList.size()]);
191       ExtendedNode node = (ExtendedNode) currentNode;
192       if (PermissionUtil.canChangePermission(node)) {
193         if (node.canAddMixin("exo:privilegeable")){
194           node.addMixin("exo:privilegeable");
195           node.setPermission(Utils.getNodeOwner(node),PermissionType.ALL);
196         }
197         for (String perm : permsRemoveList) {
198           try {
199             node.removePermission(userOrGroup, perm);
200           } catch (AccessDeniedException ade) {
201             uiApp.addMessage(new ApplicationMessage("UIPermissionForm.msg.access-denied",
202                                                     null,
203                                                     ApplicationMessage.WARNING));
204             
205             return;
206           }
207         }
208         if(PermissionUtil.canChangePermission(node)) node.setPermission(userOrGroup, permsArray);
209         node.save();
210         uiParent.getChild(UIPermissionInfo.class).updateGrid();
211       } else {
212         uiApp.addMessage(new ApplicationMessage("UIPermissionForm.msg.not-change-permission", null,
213             ApplicationMessage.WARNING));
214         
215         return;
216       }
217 
218       uiForm.refresh();
219       currentNode.getSession().save();
220       event.getRequestContext().addUIComponentToUpdateByAjax(uiParent);
221     }
222   }
223 
224   static public class SelectUserActionListener extends EventListener<UIPermissionForm> {
225     public void execute(Event<UIPermissionForm> event) throws Exception {
226       UIPermissionForm uiForm = event.getSource();
227       ((UIPermissionManager)uiForm.getParent()).initUserSelector();
228       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
229     }
230   }
231 
232   static public class AddAnyActionListener extends EventListener<UIPermissionForm> {
233     public void execute(Event<UIPermissionForm> event) throws Exception {
234       UIPermissionForm uiForm = event.getSource();
235       UIPermissionInputSet uiInputSet = uiForm.getChildById(UIPermissionForm.PERMISSION);
236       uiInputSet.getUIStringInput(UIPermissionInputSet.FIELD_USERORGROUP).setValue(
237           IdentityConstants.ANY);
238       uiForm.checkAll(false);
239       uiInputSet.getUICheckBoxInput(PermissionType.READ).setChecked(true);
240       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
241     }
242   }
243 
244   static public class SelectMemberActionListener extends EventListener<UIPermissionForm> {
245     public void execute(Event<UIPermissionForm> event) throws Exception {
246       UIPermissionForm uiForm = event.getSource();
247       UIGroupMemberSelector uiGroupMemberSelector = uiForm.createUIComponent(UIGroupMemberSelector.class,
248                                                                              null,
249                                                                              UIPermissionForm.SELECT_GROUP_ID);
250       uiGroupMemberSelector.setSourceComponent(uiForm,
251                                                new String[] { UIPermissionInputSet.FIELD_USERORGROUP });
252       uiForm.getAncestorOfType(UIPermissionManager.class).initPopupPermission(uiGroupMemberSelector);
253       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
254     }
255   }
256 
257   static public class CloseActionListener extends EventListener<UIPermissionForm> {
258     public void execute(Event<UIPermissionForm> event) throws Exception {
259       UIPopupContainer uiPopupContainer = event.getSource().getAncestorOfType(UIPopupContainer.class);
260       uiPopupContainer.deActivate() ;
261       event.getRequestContext().addUIComponentToUpdateByAjax(uiPopupContainer) ;
262     }
263   }
264   
265   public Node getCurrentNode() {
266     return NodeLocation.getNodeByLocation(currentNode);
267   }
268 
269   public void setCurrentNode(Node currentNode) {
270     this.currentNode = NodeLocation.getNodeLocationByNode(currentNode);
271   }
272 }