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