]> 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/commands/RemovePrivileges.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / legacy / argeo-commons / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / internal / jcr / commands / RemovePrivileges.java
1 package org.argeo.cms.ui.workbench.internal.jcr.commands;
2
3 import java.security.Principal;
4
5 import javax.jcr.Node;
6 import javax.jcr.RepositoryException;
7 import javax.jcr.Session;
8 import javax.jcr.security.AccessControlEntry;
9 import javax.jcr.security.AccessControlList;
10 import javax.jcr.security.AccessControlManager;
11 import javax.jcr.security.Privilege;
12
13 import org.argeo.cms.ui.jcr.JcrImages;
14 import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
15 import org.argeo.cms.ui.jcr.model.WorkspaceElem;
16 import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
17 import org.argeo.eclipse.ui.EclipseUiException;
18 import org.argeo.eclipse.ui.EclipseUiUtils;
19 import org.argeo.eclipse.ui.TreeParent;
20 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
21 import org.argeo.jcr.JcrUtils;
22 import org.eclipse.core.commands.AbstractHandler;
23 import org.eclipse.core.commands.ExecutionEvent;
24 import org.eclipse.core.commands.ExecutionException;
25 import org.eclipse.jface.dialogs.Dialog;
26 import org.eclipse.jface.dialogs.IMessageProvider;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.jface.dialogs.TitleAreaDialog;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.ui.handlers.HandlerUtil;
42
43 /** Open a dialog to remove privileges from the selected node */
44 public class RemovePrivileges extends AbstractHandler {
45 public final static String ID = WorkbenchUiPlugin.PLUGIN_ID
46 + ".removePrivileges";
47
48 public Object execute(ExecutionEvent event) throws ExecutionException {
49
50 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
51 .getActivePage().getSelection();
52 if (selection != null && !selection.isEmpty()
53 && selection instanceof IStructuredSelection) {
54 Object obj = ((IStructuredSelection) selection).getFirstElement();
55 TreeParent uiNode = null;
56 Node jcrNode = null;
57
58 if (obj instanceof SingleJcrNodeElem) {
59 uiNode = (TreeParent) obj;
60 jcrNode = ((SingleJcrNodeElem) uiNode).getNode();
61 } else if (obj instanceof WorkspaceElem) {
62 uiNode = (TreeParent) obj;
63 jcrNode = ((WorkspaceElem) uiNode).getRootNode();
64 } else
65 return null;
66
67 try {
68 String targetPath = jcrNode.getPath();
69 Dialog dialog = new RemovePrivDialog(
70 HandlerUtil.getActiveShell(event),
71 jcrNode.getSession(), targetPath);
72 dialog.open();
73 return null;
74 } catch (RepositoryException re) {
75 throw new EclipseUiException("Unable to retrieve "
76 + "path or JCR session to add privilege on " + jcrNode,
77 re);
78 }
79 } else {
80 ErrorFeedback.show("Cannot add privileges");
81 }
82 return null;
83 }
84
85 private class RemovePrivDialog extends TitleAreaDialog {
86 private static final long serialVersionUID = 280139710002698692L;
87
88 private Composite body;
89
90 private final String path;
91 private final Session session;
92
93 public RemovePrivDialog(Shell parentShell, Session session, String path) {
94 super(parentShell);
95 this.session = session;
96 this.path = path;
97 }
98
99 @Override
100 protected void configureShell(Shell newShell) {
101 super.configureShell(newShell);
102 newShell.setText("Remove privileges");
103 }
104
105 protected Control createDialogArea(Composite parent) {
106 Composite dialogarea = (Composite) super.createDialogArea(parent);
107 dialogarea.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true,
108 true));
109 body = new Composite(dialogarea, SWT.NONE);
110 body.setLayoutData(EclipseUiUtils.fillAll());
111 refreshContent();
112 parent.pack();
113 return body;
114 }
115
116 private void refreshContent() {
117 EclipseUiUtils.clear(body);
118 try {
119 AccessControlManager acm = session.getAccessControlManager();
120 AccessControlList acl = JcrUtils
121 .getAccessControlList(acm, path);
122 if (acl == null || acl.getAccessControlEntries().length <= 0)
123 setMessage("No privilege are defined on this node",
124 IMessageProvider.INFORMATION);
125 else {
126 body.setLayout(new GridLayout(3, false));
127 for (AccessControlEntry ace : acl.getAccessControlEntries()) {
128 addOnePrivRow(body, ace);
129 }
130 setMessage("Remove some of the defined privileges",
131 IMessageProvider.INFORMATION);
132 }
133 } catch (RepositoryException e) {
134 throw new EclipseUiException("Unable to list privileges on "
135 + path, e);
136 }
137 body.layout(true, true);
138 }
139
140 private void addOnePrivRow(Composite parent, AccessControlEntry ace) {
141 Principal currentPrincipal = ace.getPrincipal();
142 final String currPrincipalName = currentPrincipal.getName();
143 new Label(parent, SWT.WRAP).setText(currPrincipalName);
144 new Label(parent, SWT.WRAP).setText(privAsString(ace
145 .getPrivileges()));
146 final Button rmBtn = new Button(parent, SWT.FLAT);
147 rmBtn.setImage(JcrImages.REMOVE);
148
149 rmBtn.addSelectionListener(new SelectionAdapter() {
150 private static final long serialVersionUID = 7566938841363890730L;
151
152 @Override
153 public void widgetSelected(SelectionEvent e) {
154
155 if (MessageDialog.openConfirm(rmBtn.getShell(),
156 "Confirm deletion",
157 "Are you sure you want to remove this privilege?")) {
158 try {
159 session.save();
160 JcrUtils.clearAccessControList(session, path,
161 currPrincipalName);
162 session.save();
163 refreshContent();
164 } catch (RepositoryException re) {
165 throw new EclipseUiException("Unable to "
166 + "remove privilege for "
167 + currPrincipalName + " on " + path, re);
168 }
169 }
170
171 super.widgetSelected(e);
172 }
173 });
174
175 }
176
177 private String privAsString(Privilege[] currentPrivileges) {
178
179 StringBuilder builder = new StringBuilder();
180 builder.append("[ ");
181 for (Privilege priv : currentPrivileges) {
182 builder.append(priv.getName()).append(", ");
183 }
184 if (builder.length() > 3)
185 return builder.substring(0, builder.length() - 2) + " ]";
186 else
187 return "[]";
188
189 }
190 }
191 }