View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.cms.ui.workbench.internal.jcr.parts;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.jcr.Node;
22  import javax.jcr.NodeIterator;
23  import javax.jcr.RepositoryException;
24  import javax.jcr.Value;
25  
26  import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
27  import org.argeo.eclipse.ui.EclipseUiException;
28  import org.eclipse.jface.viewers.ColumnLabelProvider;
29  import org.eclipse.jface.viewers.IStructuredContentProvider;
30  import org.eclipse.jface.viewers.TableViewer;
31  import org.eclipse.jface.viewers.TableViewerColumn;
32  import org.eclipse.jface.viewers.Viewer;
33  import org.eclipse.swt.SWT;
34  import org.eclipse.swt.graphics.Image;
35  import org.eclipse.swt.layout.FillLayout;
36  import org.eclipse.swt.widgets.Composite;
37  import org.eclipse.swt.widgets.Table;
38  import org.eclipse.swt.widgets.TableColumn;
39  import org.eclipse.ui.forms.IManagedForm;
40  import org.eclipse.ui.forms.editor.FormEditor;
41  import org.eclipse.ui.forms.editor.FormPage;
42  import org.eclipse.ui.forms.widgets.ScrolledForm;
43  
44  /**
45   * Display and edit a given node privilege. For the time being it is completely
46   * JackRabbit specific (and hard coded for this) and will display an empty page
47   * if using any other implementation
48   */
49  public class NodePrivilegesPage extends FormPage {
50  
51  	private Node context;
52  
53  	private TableViewer viewer;
54  
55  	public NodePrivilegesPage(FormEditor editor, String title, Node context) {
56  		super(editor, "NodePrivilegesPage", title);
57  		this.context = context;
58  	}
59  
60  	protected void createFormContent(IManagedForm managedForm) {
61  		ScrolledForm form = managedForm.getForm();
62  		form.setText(WorkbenchUiPlugin.getMessage("nodeRightsManagementPageTitle"));
63  		FillLayout layout = new FillLayout();
64  		layout.marginHeight = 5;
65  		layout.marginWidth = 5;
66  		form.getBody().setLayout(layout);
67  		if (isJackRabbit())
68  			createRightsPart(form.getBody());
69  	}
70  
71  	/** Creates the authorization part */
72  	protected void createRightsPart(Composite parent) {
73  		Table table = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
74  		table.setLinesVisible(true);
75  		table.setHeaderVisible(true);
76  		viewer = new TableViewer(table);
77  
78  		// Group / user name
79  		TableViewerColumn column = createTableViewerColumn(viewer, "User/Group Name", 280);
80  		column.setLabelProvider(new ColumnLabelProvider() {
81  			private static final long serialVersionUID = -2290781173498395973L;
82  
83  			public String getText(Object element) {
84  				Node node = (Node) element;
85  				try {
86  					if (node.hasProperty("rep:principalName"))
87  						return node.getProperty("rep:principalName").getString();
88  				} catch (RepositoryException e) {
89  					throw new EclipseUiException("Unable to retrieve " + "principal name on " + node, e);
90  				}
91  				return "";
92  			}
93  
94  			public Image getImage(Object element) {
95  				return null;
96  			}
97  		});
98  
99  		// Privileges
100 		column = createTableViewerColumn(viewer, "Assigned privileges", 300);
101 		column.setLabelProvider(new ColumnLabelProvider() {
102 			private static final long serialVersionUID = -2290781173498395973L;
103 			private String propertyName = "rep:privileges";
104 
105 			public String getText(Object element) {
106 				Node node = (Node) element;
107 				try {
108 					if (node.hasProperty(propertyName)) {
109 						String separator = ", ";
110 						Value[] langs = node.getProperty(propertyName).getValues();
111 						StringBuilder builder = new StringBuilder();
112 						for (Value val : langs) {
113 							String currStr = val.getString();
114 							builder.append(currStr).append(separator);
115 						}
116 						if (builder.lastIndexOf(separator) >= 0)
117 							return builder.substring(0, builder.length() - separator.length());
118 						else
119 							return builder.toString();
120 
121 					}
122 				} catch (RepositoryException e) {
123 					throw new EclipseUiException("Unable to retrieve " + "privileges on " + node, e);
124 				}
125 				return "";
126 			}
127 
128 			public Image getImage(Object element) {
129 				return null;
130 			}
131 		});
132 
133 		// Relevant node
134 		column = createTableViewerColumn(viewer, "Relevant node", 300);
135 		column.setLabelProvider(new ColumnLabelProvider() {
136 			private static final long serialVersionUID = 4245522992038244849L;
137 
138 			public String getText(Object element) {
139 				Node node = (Node) element;
140 				try {
141 					return node.getParent().getParent().getPath();
142 				} catch (RepositoryException e) {
143 					throw new EclipseUiException("Unable get path for " + node, e);
144 				}
145 			}
146 
147 			public Image getImage(Object element) {
148 				return null;
149 			}
150 		});
151 
152 		viewer.setContentProvider(new RightsContentProvider());
153 		viewer.setInput(getEditorSite());
154 	}
155 
156 	protected TableViewerColumn createTableViewerColumn(TableViewer viewer, String title, int bound) {
157 		TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
158 		TableColumn column = viewerColumn.getColumn();
159 		column.setText(title);
160 		column.setWidth(bound);
161 		column.setResizable(true);
162 		column.setMoveable(true);
163 		return viewerColumn;
164 	}
165 
166 	private class RightsContentProvider implements IStructuredContentProvider {
167 		private static final long serialVersionUID = -7631476348552802706L;
168 
169 		public void dispose() {
170 		}
171 
172 		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
173 		}
174 
175 		// TODO JackRabbit specific retrieval of authorization. Clean and
176 		// generalize
177 		public Object[] getElements(Object inputElement) {
178 			try {
179 				List<Node> privs = new ArrayList<Node>();
180 
181 				Node currNode = context;
182 				String currPath = currNode.getPath();
183 
184 				loop: while (true) {
185 					if (currNode.hasNode("rep:policy")) {
186 						NodeIterator nit = currNode.getNode("rep:policy").getNodes();
187 						while (nit.hasNext()) {
188 							Node currPrivNode = nit.nextNode();
189 							if (currPrivNode.getName().startsWith("allow"))
190 								privs.add(currPrivNode);
191 						}
192 					}
193 					if ("/".equals(currPath))
194 						break loop;
195 					else {
196 						currNode = currNode.getParent();
197 						currPath = currNode.getPath();
198 					}
199 				}
200 
201 				// AccessControlManager acm = context.getSession()
202 				// .getAccessControlManager();
203 				// AccessControlPolicyIterator acpi = acm
204 				// .getApplicablePolicies(context.getPath());
205 				//
206 				// List<AccessControlPolicy> acps = new
207 				// ArrayList<AccessControlPolicy>();
208 				// try {
209 				// while (true) {
210 				// Object obj = acpi.next();
211 				// acps.add((AccessControlPolicy) obj);
212 				// }
213 				// } catch (Exception e) {
214 				// // No more elements
215 				// }
216 				//
217 				// AccessControlList acl = ((AccessControlList) acps.get(0));
218 				// AccessControlEntry[] entries = acl.getAccessControlEntries();
219 
220 				return privs.toArray();
221 			} catch (Exception e) {
222 				throw new EclipseUiException("Cannot retrieve authorization for " + context, e);
223 			}
224 		}
225 	}
226 
227 	/**
228 	 * Simply checks if we are using jackrabbit without adding code dependencies
229 	 */
230 	private boolean isJackRabbit() {
231 		try {
232 			String cname = context.getSession().getClass().getName();
233 			return cname.startsWith("org.apache.jackrabbit");
234 		} catch (RepositoryException e) {
235 			throw new EclipseUiException("Cannot check JCR implementation used on " + context, e);
236 		}
237 	}
238 }