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