2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
.workbench
.commands
;
18 import java
.security
.Principal
;
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
;
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
;
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";
62 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
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;
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();
82 String targetPath
= jcrNode
.getPath();
83 Dialog dialog
= new RemovePrivDialog(
84 HandlerUtil
.getActiveShell(event
),
85 jcrNode
.getSession(), targetPath
);
88 } catch (RepositoryException re
) {
89 throw new EclipseUiException("Unable to retrieve "
90 + "path or JCR session to add privilege on " + jcrNode
,
94 ErrorFeedback
.show("Cannot add privileges");
99 private class RemovePrivDialog
extends TitleAreaDialog
{
100 private static final long serialVersionUID
= 280139710002698692L;
102 private Composite body
;
104 private final String path
;
105 private final Session session
;
107 public RemovePrivDialog(Shell parentShell
, Session session
, String path
) {
109 this.session
= session
;
114 protected void configureShell(Shell newShell
) {
115 super.configureShell(newShell
);
116 newShell
.setText("Remove privileges");
119 protected Control
createDialogArea(Composite parent
) {
120 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
121 dialogarea
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, true,
123 body
= new Composite(dialogarea
, SWT
.NONE
);
124 body
.setLayoutData(EclipseUiUtils
.fillAll());
130 private void refreshContent() {
131 EclipseUiUtils
.clear(body
);
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
);
140 body
.setLayout(new GridLayout(3, false));
141 for (AccessControlEntry ace
: acl
.getAccessControlEntries()) {
142 addOnePrivRow(body
, ace
);
144 setMessage("Remove some of the defined privileges",
145 IMessageProvider
.INFORMATION
);
147 } catch (RepositoryException e
) {
148 throw new EclipseUiException(
149 "Unable to list privileges on " + path
, e
);
151 body
.layout(true, true);
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
160 final Button rmBtn
= new Button(parent
, SWT
.FLAT
);
161 rmBtn
.setImage(JcrImages
.REMOVE
);
163 rmBtn
.addSelectionListener(new SelectionAdapter() {
164 private static final long serialVersionUID
= 7566938841363890730L;
167 public void widgetSelected(SelectionEvent e
) {
169 if (MessageDialog
.openConfirm(rmBtn
.getShell(),
171 "Are you sure you want to remove this privilege?")) {
174 JcrUtils
.clearAccessControList(session
, path
,
178 } catch (RepositoryException re
) {
179 throw new EclipseUiException("Unable to "
180 + "remove privilege for "
181 + currPrincipalName
+ " on " + path
, re
);
185 super.widgetSelected(e
);
191 private String
privAsString(Privilege
[] currentPrivileges
) {
193 StringBuilder builder
= new StringBuilder();
194 builder
.append("[ ");
195 for (Privilege priv
: currentPrivileges
) {
196 builder
.append(priv
.getName()).append(", ");
198 if (builder
.length() > 3)
199 return builder
.substring(0, builder
.length() - 2) + " ]";