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.core;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import javax.jcr.Node;
23  
24  import org.exoplatform.ecm.permission.info.UIPermissionInputSet;
25  import org.exoplatform.ecm.webui.selector.UIGroupMemberSelector;
26  import org.exoplatform.ecm.webui.selector.UISelectable;
27  import org.exoplatform.ecm.webui.utils.PermissionUtil;
28  import org.exoplatform.ecm.webui.utils.Utils;
29  import org.exoplatform.services.cms.link.LinkManager;
30  import org.exoplatform.services.jcr.access.AccessControlEntry;
31  import org.exoplatform.services.jcr.access.PermissionType;
32  import org.exoplatform.services.jcr.core.ExtendedNode;
33  import org.exoplatform.services.log.ExoLogger;
34  import org.exoplatform.services.log.Log;
35  import org.exoplatform.services.security.IdentityConstants;
36  import org.exoplatform.services.wcm.core.NodetypeConstant;
37  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
38  import org.exoplatform.webui.core.UIPopupContainer;
39  import org.exoplatform.webui.event.Event;
40  import org.exoplatform.webui.event.EventListener;
41  import org.exoplatform.webui.form.UIForm;
42  
43  public abstract class UIPermissionFormBase extends UIForm implements UISelectable {
44  
45    public static final String PERMISSION   = "permission";
46    public static final String POPUP_SELECT = "SelectUserOrGroup";
47    public static final String SYMLINK = "exo:symlink";
48    
49    private static final String[] PERMISSION_TYPES = {PermissionType.READ, PermissionType.ADD_NODE, PermissionType.REMOVE}; 
50    protected static final Log LOG  = ExoLogger.getLogger(UIPermissionFormBase.class.getName());
51  
52    public UIPermissionFormBase() throws Exception {
53      addChild(new UIPermissionInputSet(PERMISSION));
54      setActions(new String[] { "Save", "Reset", "Close" });
55    }
56    
57    public abstract Node getCurrentNode() throws Exception;
58  
59    public void fillForm(String user, ExtendedNode node) throws Exception {
60      UIPermissionInputSet uiInputSet = getChildById(PERMISSION);
61      refresh();
62      uiInputSet.getUIStringInput(UIPermissionInputSet.FIELD_USERORGROUP).setValue(user);
63      if(user.equals(Utils.getNodeOwner(node))) {
64        for (String perm : PERMISSION_TYPES) {
65          uiInputSet.getUICheckBoxInput(perm).setChecked(true);
66        }
67      } else {
68        List<AccessControlEntry> permsList = node.getACL().getPermissionEntries();
69        Iterator<AccessControlEntry> perIter = permsList.iterator();
70        StringBuilder userPermission = new StringBuilder();
71        while(perIter.hasNext()) {
72          AccessControlEntry accessControlEntry = perIter.next();
73          if(user.equals(accessControlEntry.getIdentity())) {
74            userPermission.append(accessControlEntry.getPermission()).append(" ");
75          }
76        }
77        for (String perm : PERMISSION_TYPES) {
78          boolean isCheck = userPermission.toString().contains(perm);
79          uiInputSet.getUICheckBoxInput(perm).setChecked(isCheck);
80        }
81      }
82    }
83  
84    public void updateSymlinks(Node node) throws Exception {
85      if(node.isNodeType(NodetypeConstant.MIX_REFERENCEABLE)){
86        LinkManager linkManager = WCMCoreUtils.getService(LinkManager.class);
87        List<Node> symlinks = linkManager.getAllLinks(node, SYMLINK);
88        for (Node symlink : symlinks) {
89          try {
90            linkManager.updateLink(symlink, node);
91          } catch (Exception e) {
92            if (LOG.isWarnEnabled()) {
93              LOG.warn(e.getMessage());
94            }
95          }
96        }
97      }
98    }
99    
100   protected void lockForm(boolean isLock) {
101     UIPermissionInputSet uiInputSet = getChildById(PERMISSION);
102     if(isLock) {
103       uiInputSet.setButtonActions(new String[] {"Reset" });
104     } else {
105       uiInputSet.setButtonActions(new String[] {"Save", "Reset" });
106     }
107     for (String perm : PERMISSION_TYPES) {
108       uiInputSet.getUICheckBoxInput(perm).setDisabled(isLock);
109     }
110   }
111 
112   protected boolean isEditable(Node node) throws Exception {
113     return PermissionUtil.canChangePermission(node);
114   }
115 
116   protected void refresh() {
117     reset();
118     checkAll(false);
119   }
120 
121   protected void checkAll(boolean check) {
122     UIPermissionInputSet uiInputSet = getChildById(PERMISSION);
123     for (String perm : PERMISSION_TYPES) {
124       uiInputSet.getUICheckBoxInput(perm).setChecked(check);
125     }
126   }
127 
128   protected String  getExoOwner(Node node) throws Exception {
129     return Utils.getNodeOwner(node);
130   }
131 
132   static public class ResetActionListener extends EventListener<UIPermissionFormBase> {
133     public void execute(Event<UIPermissionFormBase> event) throws Exception {
134       UIPermissionFormBase uiForm = event.getSource();
135       uiForm.lockForm(false);
136       uiForm.refresh();
137       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
138     }
139   }
140 
141   static public class SelectUserActionListener extends EventListener<UIPermissionFormBase> {
142     public void execute(Event<UIPermissionFormBase> event) throws Exception {
143       UIPermissionFormBase uiForm = event.getSource();
144       ((UIPermissionManagerBase)uiForm.getParent()).initUserSelector();
145       UIPopupContainer popupContainer = uiForm.getAncestorOfType(UIPopupContainer.class);
146       if (popupContainer != null) {
147         event.getRequestContext().addUIComponentToUpdateByAjax(popupContainer);
148       } else {
149         event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
150       }
151     }
152   }
153 
154   static public class AddAnyActionListener extends EventListener<UIPermissionFormBase> {
155     public void execute(Event<UIPermissionFormBase> event) throws Exception {
156       UIPermissionFormBase uiForm = event.getSource();
157       UIPermissionInputSet uiInputSet = uiForm.getChildById(UIPermissionFormBase.PERMISSION);
158       uiInputSet.getUIStringInput(UIPermissionInputSet.FIELD_USERORGROUP).setValue(IdentityConstants.ANY);
159       uiForm.checkAll(false);
160       uiInputSet.getUICheckBoxInput(PermissionType.READ).setChecked(true);
161       UIPopupContainer popupContainer = uiForm.getAncestorOfType(UIPopupContainer.class);
162       if (popupContainer != null) {
163         event.getRequestContext().addUIComponentToUpdateByAjax(popupContainer);
164       } else {
165         event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
166       }
167     }
168   }
169 
170   static public class SelectMemberActionListener extends EventListener<UIPermissionFormBase> {
171     public void execute(Event<UIPermissionFormBase> event) throws Exception {
172       UIPermissionFormBase uiForm = event.getSource();
173       UIGroupMemberSelector uiGroupMemberSelector = uiForm.createUIComponent(UIGroupMemberSelector.class, null, null);
174       uiGroupMemberSelector.setSourceComponent(uiForm, new String[] { UIPermissionInputSet.FIELD_USERORGROUP });
175       uiForm.getAncestorOfType(UIPermissionManagerBase.class).initPopupPermission(uiGroupMemberSelector);
176       UIPopupContainer popupContainer = uiForm.getAncestorOfType(UIPopupContainer.class);
177       if (popupContainer != null) {
178         event.getRequestContext().addUIComponentToUpdateByAjax(popupContainer);
179       } else {
180         event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
181       }
182     }
183   }
184 }