1 package org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.commands
;
3 import java
.security
.Principal
;
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
;
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
;
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";
48 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
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;
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();
68 String targetPath
= jcrNode
.getPath();
69 Dialog dialog
= new RemovePrivDialog(
70 HandlerUtil
.getActiveShell(event
),
71 jcrNode
.getSession(), targetPath
);
74 } catch (RepositoryException re
) {
75 throw new EclipseUiException("Unable to retrieve "
76 + "path or JCR session to add privilege on " + jcrNode
,
80 ErrorFeedback
.show("Cannot add privileges");
85 private class RemovePrivDialog
extends TitleAreaDialog
{
86 private static final long serialVersionUID
= 280139710002698692L;
88 private Composite body
;
90 private final String path
;
91 private final Session session
;
93 public RemovePrivDialog(Shell parentShell
, Session session
, String path
) {
95 this.session
= session
;
100 protected void configureShell(Shell newShell
) {
101 super.configureShell(newShell
);
102 newShell
.setText("Remove privileges");
105 protected Control
createDialogArea(Composite parent
) {
106 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
107 dialogarea
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, true,
109 body
= new Composite(dialogarea
, SWT
.NONE
);
110 body
.setLayoutData(EclipseUiUtils
.fillAll());
116 private void refreshContent() {
117 EclipseUiUtils
.clear(body
);
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
);
126 body
.setLayout(new GridLayout(3, false));
127 for (AccessControlEntry ace
: acl
.getAccessControlEntries()) {
128 addOnePrivRow(body
, ace
);
130 setMessage("Remove some of the defined privileges",
131 IMessageProvider
.INFORMATION
);
133 } catch (RepositoryException e
) {
134 throw new EclipseUiException("Unable to list privileges on "
137 body
.layout(true, true);
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
146 final Button rmBtn
= new Button(parent
, SWT
.FLAT
);
147 rmBtn
.setImage(JcrImages
.REMOVE
);
149 rmBtn
.addSelectionListener(new SelectionAdapter() {
150 private static final long serialVersionUID
= 7566938841363890730L;
153 public void widgetSelected(SelectionEvent e
) {
155 if (MessageDialog
.openConfirm(rmBtn
.getShell(),
157 "Are you sure you want to remove this privilege?")) {
160 JcrUtils
.clearAccessControList(session
, path
,
164 } catch (RepositoryException re
) {
165 throw new EclipseUiException("Unable to "
166 + "remove privilege for "
167 + currPrincipalName
+ " on " + path
, re
);
171 super.widgetSelected(e
);
177 private String
privAsString(Privilege
[] currentPrivileges
) {
179 StringBuilder builder
= new StringBuilder();
180 builder
.append("[ ");
181 for (Privilege priv
: currentPrivileges
) {
182 builder
.append(priv
.getName()).append(", ");
184 if (builder
.length() > 3)
185 return builder
.substring(0, builder
.length() - 2) + " ]";