Add the ability to remove a JCR privilege from a given node
authorBruno Sinou <bsinou@argeo.org>
Mon, 23 Nov 2015 15:28:24 +0000 (15:28 +0000)
committerBruno Sinou <bsinou@argeo.org>
Mon, 23 Nov 2015 15:28:24 +0000 (15:28 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@8589 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.eclipse.ui.workbench/META-INF/spring/commands.xml
org.argeo.eclipse.ui.workbench/icons/addPrivileges.gif [new file with mode: 0644]
org.argeo.eclipse.ui.workbench/icons/addPrivileges.png [deleted file]
org.argeo.eclipse.ui.workbench/icons/removePrivileges.gif [new file with mode: 0644]
org.argeo.eclipse.ui.workbench/plugin.xml
org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/RemovePrivileges.java [new file with mode: 0644]
org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/JcrImages.java
org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/users/PickUpUserDialog.java

index 1d31e16b7d87ae3fc397ce2a41212da07a7ec671..39ab24243c9a01b802798c0ce00fff4a0850dbca 100644 (file)
@@ -14,5 +14,9 @@
        <bean id="addPrivileges" class="org.argeo.eclipse.ui.workbench.commands.AddPrivileges">
                <property name="userAdmin" ref="userAdmin" />
        </bean>
+       <bean id="removePrivileges"
+               class="org.argeo.eclipse.ui.workbench.commands.RemovePrivileges">
+               <!-- <property name="userAdmin" ref="userAdmin" /> -->
+       </bean>
 
 </beans>
\ No newline at end of file
diff --git a/org.argeo.eclipse.ui.workbench/icons/addPrivileges.gif b/org.argeo.eclipse.ui.workbench/icons/addPrivileges.gif
new file mode 100644 (file)
index 0000000..a6b251f
Binary files /dev/null and b/org.argeo.eclipse.ui.workbench/icons/addPrivileges.gif differ
diff --git a/org.argeo.eclipse.ui.workbench/icons/addPrivileges.png b/org.argeo.eclipse.ui.workbench/icons/addPrivileges.png
deleted file mode 100644 (file)
index a6b251f..0000000
Binary files a/org.argeo.eclipse.ui.workbench/icons/addPrivileges.png and /dev/null differ
diff --git a/org.argeo.eclipse.ui.workbench/icons/removePrivileges.gif b/org.argeo.eclipse.ui.workbench/icons/removePrivileges.gif
new file mode 100644 (file)
index 0000000..aa78fd2
Binary files /dev/null and b/org.argeo.eclipse.ui.workbench/icons/removePrivileges.gif differ
index b450f555535244a5af7924c7e6a93dd995ee8ea1..215f1203d559cc2a11e685cb7b87085fbde1846c 100644 (file)
                        id="org.argeo.eclipse.ui.workbench.addPrivileges"
                        name="Add Privileges">
                </command>
+               <command
+                       defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
+                       id="org.argeo.eclipse.ui.workbench.removePrivileges"
+                       name="Remove Privileges">
+               </command>
                <command
                        defaultHandler="org.argeo.eclipse.ui.workbench.commands.CreateWorkspace"
                        id="org.argeo.eclipse.ui.workbench.createWorkspace"
                        <command
                         commandId="org.argeo.eclipse.ui.workbench.addFolderNode"
                         icon="icons/addFolder.gif"
-                        label="Add Folder"
+                        label="Add Folder..."
                         style="push">
                                <visibleWhen>
                                        <iterate>
                        </command>
                        <command
                         commandId="org.argeo.eclipse.ui.workbench.addPrivileges"
-                        icon="icons/addPrivileges.png"
-                        label="Add Privileges"
+                        icon="icons/addPrivileges.gif"
+                        label="Add Privileges..."
+                        style="push">
+                               <visibleWhen>
+                                       <iterate>
+                                     <and>
+                                        <or>
+                                           <instanceof
+                                                 value="org.argeo.eclipse.ui.workbench.internal.jcr.model.SingleJcrNodeElem">
+                                           </instanceof>
+                                           <instanceof
+                                                 value="org.argeo.eclipse.ui.workbench.internal.jcr.model.WorkspaceElem">
+                                           </instanceof>
+                                        </or>
+                               <with variable="activeMenuSelection"><count value="1"/></with>
+                                     </and>
+                                       </iterate>
+                               </visibleWhen>
+                       </command>
+                       <command
+                        commandId="org.argeo.eclipse.ui.workbench.removePrivileges"
+                        icon="icons/removePrivileges.gif"
+                        label="Remove Privileges..."
                         style="push">
                                <visibleWhen>
                                        <iterate>
                        <command
                         commandId="org.argeo.eclipse.ui.workbench.createWorkspace"
                         icon="icons/addWorkspace.png"
-                        label="Create Workspace"
+                        label="Create Workspace..."
                         style="push">
                                <visibleWhen>
                                        <iterate>
diff --git a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/RemovePrivileges.java b/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/RemovePrivileges.java
new file mode 100644 (file)
index 0000000..c11413d
--- /dev/null
@@ -0,0 +1,205 @@
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.argeo.eclipse.ui.workbench.commands;
+
+import java.security.Principal;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.security.AccessControlEntry;
+import javax.jcr.security.AccessControlList;
+import javax.jcr.security.AccessControlManager;
+import javax.jcr.security.Privilege;
+
+import org.argeo.ArgeoException;
+import org.argeo.eclipse.ui.EclipseUiUtils;
+import org.argeo.eclipse.ui.TreeParent;
+import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
+import org.argeo.eclipse.ui.workbench.WorkbenchUiPlugin;
+import org.argeo.eclipse.ui.workbench.internal.jcr.model.SingleJcrNodeElem;
+import org.argeo.eclipse.ui.workbench.internal.jcr.model.WorkspaceElem;
+import org.argeo.eclipse.ui.workbench.jcr.JcrImages;
+import org.argeo.jcr.JcrUtils;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.IMessageProvider;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.dialogs.TitleAreaDialog;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+/** Open a dialog to remove privileges from the selected node */
+public class RemovePrivileges extends AbstractHandler {
+       public final static String ID = WorkbenchUiPlugin.ID + ".removePrivileges";
+
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+
+               ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
+                               .getActivePage().getSelection();
+               if (selection != null && !selection.isEmpty()
+                               && selection instanceof IStructuredSelection) {
+                       Object obj = ((IStructuredSelection) selection).getFirstElement();
+                       TreeParent uiNode = null;
+                       Node jcrNode = null;
+
+                       if (obj instanceof SingleJcrNodeElem) {
+                               uiNode = (TreeParent) obj;
+                               jcrNode = ((SingleJcrNodeElem) uiNode).getNode();
+                       } else if (obj instanceof WorkspaceElem) {
+                               uiNode = (TreeParent) obj;
+                               jcrNode = ((WorkspaceElem) uiNode).getRootNode();
+                       } else
+                               return null;
+
+                       try {
+                               String targetPath = jcrNode.getPath();
+                               Dialog dialog = new RemovePrivDialog(
+                                               HandlerUtil.getActiveShell(event),
+                                               jcrNode.getSession(), targetPath);
+                               dialog.open();
+                               return null;
+                       } catch (RepositoryException re) {
+                               throw new ArgeoException("Unable to retrieve "
+                                               + "path or JCR session to add privilege on " + jcrNode,
+                                               re);
+                       }
+               } else {
+                       ErrorFeedback.show("Cannot add privileges");
+               }
+               return null;
+       }
+
+       private class RemovePrivDialog extends TitleAreaDialog {
+               private static final long serialVersionUID = 280139710002698692L;
+
+               private Composite body;
+
+               private final String path;
+               private final Session session;
+
+               public RemovePrivDialog(Shell parentShell, Session session, String path) {
+                       super(parentShell);
+                       this.session = session;
+                       this.path = path;
+               }
+
+               @Override
+               protected void configureShell(Shell newShell) {
+                       super.configureShell(newShell);
+                       newShell.setText("Remove privileges");
+               }
+
+               protected Control createDialogArea(Composite parent) {
+                       Composite dialogarea = (Composite) super.createDialogArea(parent);
+                       dialogarea.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true,
+                                       true));
+                       body = new Composite(dialogarea, SWT.NONE);
+                       body.setLayoutData(EclipseUiUtils.fillAll());
+                       refreshContent();
+                       parent.pack();
+                       return body;
+               }
+
+               private void refreshContent() {
+                       EclipseUiUtils.clear(body);
+                       try {
+                               AccessControlManager acm = session.getAccessControlManager();
+                               AccessControlList acl = JcrUtils
+                                               .getAccessControlList(acm, path);
+                               if (acl == null || acl.getAccessControlEntries().length <= 0)
+                                       setMessage("No privilege are defined on this node",
+                                                       IMessageProvider.INFORMATION);
+                               else {
+                                       body.setLayout(new GridLayout(3, false));
+                                       for (AccessControlEntry ace : acl.getAccessControlEntries()) {
+                                               addOnePrivRow(body, ace);
+                                       }
+                                       setMessage("Remove some of the defined privileges",
+                                                       IMessageProvider.INFORMATION);
+                               }
+                       } catch (RepositoryException e) {
+                               throw new ArgeoException(
+                                               "Unable to list privileges on " + path, e);
+                       }
+                       body.layout(true, true);
+               }
+
+               private void addOnePrivRow(Composite parent, AccessControlEntry ace) {
+                       Principal currentPrincipal = ace.getPrincipal();
+                       final String currPrincipalName = currentPrincipal.getName();
+                       new Label(parent, SWT.WRAP).setText(currPrincipalName);
+                       new Label(parent, SWT.WRAP).setText(privAsString(ace
+                                       .getPrivileges()));
+                       final Button rmBtn = new Button(parent, SWT.FLAT);
+                       rmBtn.setImage(JcrImages.REMOVE);
+
+                       rmBtn.addSelectionListener(new SelectionAdapter() {
+                               private static final long serialVersionUID = 7566938841363890730L;
+
+                               @Override
+                               public void widgetSelected(SelectionEvent e) {
+
+                                       if (MessageDialog.openConfirm(rmBtn.getShell(),
+                                                       "Confirm deletion",
+                                                       "Are you sure you want to remove this privilege?")) {
+                                               try {
+                                                       session.save();
+                                                       JcrUtils.clearAccessControList(session, path,
+                                                                       currPrincipalName);
+                                                       session.save();
+                                                       refreshContent();
+                                               } catch (RepositoryException re) {
+                                                       throw new ArgeoException("Unable to "
+                                                                       + "remove privilege for "
+                                                                       + currPrincipalName + " on " + path, re);
+                                               }
+                                       }
+
+                                       super.widgetSelected(e);
+                               }
+                       });
+
+               }
+
+               private String privAsString(Privilege[] currentPrivileges) {
+
+                       StringBuilder builder = new StringBuilder();
+                       builder.append("[ ");
+                       for (Privilege priv : currentPrivileges) {
+                               builder.append(priv.getName()).append(", ");
+                       }
+                       if (builder.length() > 3)
+                               return builder.substring(0, builder.length() - 2) + " ]";
+                       else
+                               return "[]";
+
+               }
+       }
+}
\ No newline at end of file
index db24d6c22d93b019dee9fab8575167b7c7c85878..2da973a4746e777c06c2207f470935e88a3b82d1 100644 (file)
@@ -32,6 +32,8 @@ public class JcrImages {
                        "icons/home.gif").createImage();
        public final static Image SORT = WorkbenchUiPlugin.getImageDescriptor(
                        "icons/sort.gif").createImage();
+       public final static Image REMOVE = WorkbenchUiPlugin.getImageDescriptor(
+                       "icons/remove.gif").createImage();
 
        public final static Image REPOSITORIES = WorkbenchUiPlugin
                        .getImageDescriptor("icons/repositories.gif").createImage();
index 7caec2b97d767be143a416b6d3e9240052986d2e..6b9344b02974279125f38b4a482acad86b07bc9a 100644 (file)
@@ -24,6 +24,7 @@ import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.eclipse.ui.parts.LdifUsersTable;
 import org.argeo.eclipse.ui.workbench.internal.users.UsersUtils;
 import org.argeo.osgi.useradmin.LdifName;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.dialogs.TrayDialog;
 import org.eclipse.jface.viewers.DoubleClickEvent;
 import org.eclipse.jface.viewers.IDoubleClickListener;
@@ -35,8 +36,8 @@ import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
-import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
@@ -62,14 +63,17 @@ public class PickUpUserDialog extends TrayDialog {
        private TableViewer userViewer;
        private List<ColumnDefinition> columnDefs = new ArrayList<ColumnDefinition>();
 
+       /**
+        * A dialog to pick up a group or a user, showing a table with default
+        * columns
+        */
        public PickUpUserDialog(Shell parentShell, String title, UserAdmin userAdmin) {
                super(parentShell);
                this.title = title;
                this.userAdmin = userAdmin;
 
-               // Define the displayed columns
                columnDefs.add(new ColumnDefinition(new UserLP(UserLP.COL_ICON), "",
-                               26, 0));
+                               24, 24));
                columnDefs.add(new ColumnDefinition(
                                new UserLP(UserLP.COL_DISPLAY_NAME), "Common Name", 150, 100));
                columnDefs.add(new ColumnDefinition(new UserLP(UserLP.COL_DOMAIN),
@@ -78,8 +82,22 @@ public class PickUpUserDialog extends TrayDialog {
                                "Distinguished Name", 300, 100));
        }
 
-       protected Point getInitialSize() {
-               return new Point(700, 450);
+       /** A dialog to pick up a group or a user */
+       public PickUpUserDialog(Shell parentShell, String title,
+                       UserAdmin userAdmin, List<ColumnDefinition> columnDefs) {
+               super(parentShell);
+               this.title = title;
+               this.userAdmin = userAdmin;
+               this.columnDefs = columnDefs;
+       }
+
+       @Override
+       protected void okPressed() {
+               if (getSelected() == null)
+                       MessageDialog.openError(getShell(), "No user chosen",
+                                       "Please, choose a user or press Cancel.");
+               else
+                       super.okPressed();
        }
 
        protected Control createDialogArea(Composite parent) {
@@ -95,7 +113,9 @@ public class PickUpUserDialog extends TrayDialog {
 
                userTableViewerCmp.setColumnDefinitions(columnDefs);
                userTableViewerCmp.populateWithStaticFilters(false, false);
-               userTableViewerCmp.setLayoutData(EclipseUiUtils.fillAll());
+               GridData gd = EclipseUiUtils.fillAll();
+               gd.minimumHeight = 300;
+               userTableViewerCmp.setLayoutData(gd);
                userTableViewerCmp.refresh();
 
                // Controllers