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