]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/internal/jcr/parts/NodePrivilegesPage.java
Reactivate experimental page to edit String properties.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / ui / workbench / internal / jcr / parts / NodePrivilegesPage.java
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.eclipse.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.ArgeoException;
27 import org.argeo.eclipse.ui.workbench.WorkbenchUiPlugin;
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 hardcoded 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
63 .getMessage("nodeRightsManagementPageTitle"));
64 FillLayout layout = new FillLayout();
65 layout.marginHeight = 5;
66 layout.marginWidth = 5;
67 form.getBody().setLayout(layout);
68 if (isJackRabbit())
69 createRightsPart(form.getBody());
70 }
71
72 /** Creates the authorization part */
73 protected void createRightsPart(Composite parent) {
74 Table table = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
75 table.setLinesVisible(true);
76 table.setHeaderVisible(true);
77 viewer = new TableViewer(table);
78
79 // Group / user name
80 TableViewerColumn column = createTableViewerColumn(viewer,
81 "User/Group Name", 280);
82 column.setLabelProvider(new ColumnLabelProvider() {
83 private static final long serialVersionUID = -2290781173498395973L;
84
85 public String getText(Object element) {
86 Node node = (Node) element;
87 try {
88 if (node.hasProperty("rep:principalName"))
89 return node.getProperty("rep:principalName")
90 .getString();
91 } catch (RepositoryException e) {
92 throw new ArgeoException("Unable to retrieve "
93 + "principal name on " + node, e);
94 }
95 return "";
96 }
97
98 public Image getImage(Object element) {
99 return null;
100 }
101 });
102
103 // Privileges
104 column = createTableViewerColumn(viewer, "Assigned privileges", 300);
105 column.setLabelProvider(new ColumnLabelProvider() {
106 private static final long serialVersionUID = -2290781173498395973L;
107 private String propertyName = "rep:privileges";
108
109 public String getText(Object element) {
110 Node node = (Node) element;
111 try {
112 if (node.hasProperty(propertyName)) {
113 String separator = ", ";
114 Value[] langs = node.getProperty(propertyName)
115 .getValues();
116 StringBuilder builder = new StringBuilder();
117 for (Value val : langs) {
118 String currStr = val.getString();
119 builder.append(currStr).append(separator);
120 }
121 if (builder.lastIndexOf(separator) >= 0)
122 return builder.substring(0, builder.length()
123 - separator.length());
124 else
125 return builder.toString();
126
127 }
128 } catch (RepositoryException e) {
129 throw new ArgeoException("Unable to retrieve "
130 + "privileges on " + node, e);
131 }
132 return "";
133 }
134
135 public Image getImage(Object element) {
136 return null;
137 }
138 });
139
140 // Relevant node
141 column = createTableViewerColumn(viewer, "Relevant node", 300);
142 column.setLabelProvider(new ColumnLabelProvider() {
143 /**
144 *
145 */
146 private static final long serialVersionUID = 4245522992038244849L;
147
148 public String getText(Object element) {
149 Node node = (Node) element;
150 try {
151 return node.getParent().getParent().getPath();
152 } catch (RepositoryException e) {
153 throw new ArgeoException("Unable get path for " + node, e);
154 }
155 }
156
157 public Image getImage(Object element) {
158 return null;
159 }
160 });
161
162 viewer.setContentProvider(new RightsContentProvider());
163 viewer.setInput(getEditorSite());
164 }
165
166 protected TableViewerColumn createTableViewerColumn(TableViewer viewer,
167 String title, int bound) {
168 final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
169 SWT.NONE);
170 final TableColumn column = viewerColumn.getColumn();
171 column.setText(title);
172 column.setWidth(bound);
173 column.setResizable(true);
174 column.setMoveable(true);
175 return viewerColumn;
176 }
177
178 private class RightsContentProvider implements IStructuredContentProvider {
179 private static final long serialVersionUID = -7631476348552802706L;
180
181 public void dispose() {
182 }
183
184 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
185 }
186
187 // TODO JackRabbit specific retrieval of authorization. Clean and
188 // generalize
189 public Object[] getElements(Object inputElement) {
190 try {
191 List<Node> privs = new ArrayList<Node>();
192
193 Node currNode = context;
194 String currPath = currNode.getPath();
195
196 loop: while (true) {
197 if (currNode.hasNode("rep:policy")) {
198 NodeIterator nit = currNode.getNode("rep:policy")
199 .getNodes();
200 while (nit.hasNext()) {
201 Node currPrivNode = nit.nextNode();
202 if (currPrivNode.getName().startsWith("allow"))
203 privs.add(currPrivNode);
204 }
205 }
206 if ("/".equals(currPath))
207 break loop;
208 else {
209 currNode = currNode.getParent();
210 currPath = currNode.getPath();
211 }
212 }
213
214 // AccessControlManager acm = context.getSession()
215 // .getAccessControlManager();
216 // AccessControlPolicyIterator acpi = acm
217 // .getApplicablePolicies(context.getPath());
218 //
219 // List<AccessControlPolicy> acps = new
220 // ArrayList<AccessControlPolicy>();
221 // try {
222 // while (true) {
223 // Object obj = acpi.next();
224 // acps.add((AccessControlPolicy) obj);
225 // }
226 // } catch (Exception e) {
227 // // No more elements
228 // }
229 //
230 // AccessControlList acl = ((AccessControlList) acps.get(0));
231 // AccessControlEntry[] entries = acl.getAccessControlEntries();
232
233 return privs.toArray();
234 } catch (Exception e) {
235 throw new ArgeoException("Cannot retrieve authorization for "
236 + context, e);
237 }
238 }
239 }
240
241 // simply check if we are using jackrabbit without adding code dependencies
242 private boolean isJackRabbit() {
243 try {
244 String cname = context.getSession().getClass().getName();
245 return cname.startsWith("org.apache.jackrabbit");
246 } catch (RepositoryException e) {
247 throw new ArgeoException("Cannot check JCR implementation used on "
248 + context, e);
249 }
250 }
251
252 }