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
.cms
.ui
.workbench
.internal
.jcr
.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
.cms
.ui
.jcr
.JcrImages
;
29 import org
.argeo
.cms
.ui
.jcr
.model
.SingleJcrNodeElem
;
30 import org
.argeo
.cms
.ui
.jcr
.model
.WorkspaceElem
;
31 import org
.argeo
.cms
.ui
.workbench
.WorkbenchUiPlugin
;
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
;
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";
63 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
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;
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();
83 String targetPath
= jcrNode
.getPath();
84 Dialog dialog
= new RemovePrivDialog(
85 HandlerUtil
.getActiveShell(event
),
86 jcrNode
.getSession(), targetPath
);
89 } catch (RepositoryException re
) {
90 throw new EclipseUiException("Unable to retrieve "
91 + "path or JCR session to add privilege on " + jcrNode
,
95 ErrorFeedback
.show("Cannot add privileges");
100 private class RemovePrivDialog
extends TitleAreaDialog
{
101 private static final long serialVersionUID
= 280139710002698692L;
103 private Composite body
;
105 private final String path
;
106 private final Session session
;
108 public RemovePrivDialog(Shell parentShell
, Session session
, String path
) {
110 this.session
= session
;
115 protected void configureShell(Shell newShell
) {
116 super.configureShell(newShell
);
117 newShell
.setText("Remove privileges");
120 protected Control
createDialogArea(Composite parent
) {
121 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
122 dialogarea
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, true,
124 body
= new Composite(dialogarea
, SWT
.NONE
);
125 body
.setLayoutData(EclipseUiUtils
.fillAll());
131 private void refreshContent() {
132 EclipseUiUtils
.clear(body
);
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
);
141 body
.setLayout(new GridLayout(3, false));
142 for (AccessControlEntry ace
: acl
.getAccessControlEntries()) {
143 addOnePrivRow(body
, ace
);
145 setMessage("Remove some of the defined privileges",
146 IMessageProvider
.INFORMATION
);
148 } catch (RepositoryException e
) {
149 throw new EclipseUiException("Unable to list privileges on "
152 body
.layout(true, true);
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
161 final Button rmBtn
= new Button(parent
, SWT
.FLAT
);
162 rmBtn
.setImage(JcrImages
.REMOVE
);
164 rmBtn
.addSelectionListener(new SelectionAdapter() {
165 private static final long serialVersionUID
= 7566938841363890730L;
168 public void widgetSelected(SelectionEvent e
) {
170 if (MessageDialog
.openConfirm(rmBtn
.getShell(),
172 "Are you sure you want to remove this privilege?")) {
175 JcrUtils
.clearAccessControList(session
, path
,
179 } catch (RepositoryException re
) {
180 throw new EclipseUiException("Unable to "
181 + "remove privilege for "
182 + currPrincipalName
+ " on " + path
, re
);
186 super.widgetSelected(e
);
192 private String
privAsString(Privilege
[] currentPrivileges
) {
194 StringBuilder builder
= new StringBuilder();
195 builder
.append("[ ");
196 for (Privilege priv
: currentPrivileges
) {
197 builder
.append(priv
.getName()).append(", ");
199 if (builder
.length() > 3)
200 return builder
.substring(0, builder
.length() - 2) + " ]";