Fix filtering on group and user views, remove legacy classes.
authorBruno Sinou <bsinou@argeo.org>
Mon, 14 Sep 2015 15:18:12 +0000 (15:18 +0000)
committerBruno Sinou <bsinou@argeo.org>
Mon, 14 Sep 2015 15:18:12 +0000 (15:18 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@8391 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.security.ui.admin/META-INF/spring/commands.xml
org.argeo.security.ui.admin/META-INF/spring/views.xml
org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/commands/NewUser.java
org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/UserAdminConstants.java
org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/views/GroupsView.java
org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/views/JcrRolesView.java [deleted file]
org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/views/JcrUsersView.java [deleted file]
org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/views/UsersView.java

index 24c0ac274a719c0130a5a4cdc8197e845093f495..23be58d8d959baec990782a95fd460fe95a28d6e 100644 (file)
@@ -8,6 +8,7 @@
        <bean id="newUser" class="org.argeo.security.ui.admin.commands.NewUser"
                scope="prototype">
                <property name="userAdmin" ref="userAdmin" />
+               <property name="userTransaction" ref="userTransaction" />
        </bean>
 
        <!-- <bean id="userBatchUpdate" class="org.argeo.security.ui.admin.commands.UserBatchUpdate" 
index 18a7b888141410f3cc60d7f0af23e972ff84a57b..0af9b377d1b540c63880ca9bbd7df1f323fbe77c 100644 (file)
@@ -7,7 +7,6 @@
        <bean id="usersView" class="org.argeo.security.ui.admin.views.UsersView"
                scope="prototype">
                <property name="userAdmin" ref="userAdmin" />
-               <property name="userTransaction" ref="userTransaction" />
        </bean>
 
        <bean id="groupsView" class="org.argeo.security.ui.admin.views.GroupsView"
index fce684a67c965cb1430c987c65594391d08fd961..9f1ae8d0d0d37363b670c0ba250ad8c8028fb3d6 100644 (file)
@@ -17,6 +17,10 @@ package org.argeo.security.ui.admin.commands;
 
 import java.util.Dictionary;
 
+import javax.transaction.Status;
+import javax.transaction.UserTransaction;
+
+import org.argeo.ArgeoException;
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
 import org.argeo.jcr.ArgeoNames;
@@ -49,10 +53,11 @@ import org.osgi.service.useradmin.UserAdmin;
 /** Open a wizard that enables creation of a new user. */
 public class NewUser extends AbstractHandler {
        // private final static Log log = LogFactory.getLog(NewUser.class);
-
        public final static String ID = SecurityAdminPlugin.PLUGIN_ID + ".newUser";
 
+       /* DEPENDENCY INJECTION */
        private UserAdmin userAdmin;
+       private UserTransaction userTransaction;
 
        // TODO implement a dynamic choice of the base dn
        private String getDn(String uid) {
@@ -107,6 +112,16 @@ public class NewUser extends AbstractHandler {
                        if (!canFinish())
                                return false;
                        String username = mainUserInfo.getUsername();
+
+                       // Begin transaction if needed
+                       try {
+                               if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION)
+                                       userTransaction.begin();
+                       } catch (Exception e) {
+                               throw new ArgeoException("Unable to start "
+                                               + "transaction to create user " + username, e);
+                       }
+
                        try {
                                char[] password = mainUserInfo.getPassword();
                                User user = (User) userAdmin.createRole(getDn(username),
@@ -250,9 +265,13 @@ public class NewUser extends AbstractHandler {
 
                }
        }
-       
+
        /* DEPENDENCY INJECTION */
        public void setUserAdmin(UserAdmin userAdmin) {
                this.userAdmin = userAdmin;
        }
+
+       public void setUserTransaction(UserTransaction userTransaction) {
+               this.userTransaction = userTransaction;
+       }
 }
\ No newline at end of file
index 7ec5984a9e1cc3629839026abbd08e00339de726..ab9f0fbe27d97e656b3b6802e3683c11662096ab 100644 (file)
@@ -4,9 +4,9 @@ package org.argeo.security.ui.admin.internal;
 public interface UserAdminConstants {
 
        public final static String KEY_UID = "uid";
+       public final static String KEY_DN = "dn";
        public final static String KEY_CN = "cn";
        public final static String KEY_FIRSTNAME = "givenname";
        public final static String KEY_LASTNAME = "sn";
        public final static String KEY_MAIL = "mail";
-       
-}
+}
\ No newline at end of file
index 1ee26a2ab160ab03e94ae0750030dadfe8953697..faf0e8025d3c07e0985493b299e1e87111bbc64b 100644 (file)
@@ -24,6 +24,8 @@ import org.argeo.jcr.ArgeoNames;
 import org.argeo.security.ui.admin.SecurityAdminPlugin;
 import org.argeo.security.ui.admin.internal.ColumnDefinition;
 import org.argeo.security.ui.admin.internal.CommonNameLP;
+import org.argeo.security.ui.admin.internal.UiAdminUtils;
+import org.argeo.security.ui.admin.internal.UserAdminConstants;
 import org.argeo.security.ui.admin.internal.UserNameLP;
 import org.argeo.security.ui.admin.internal.UserTableDefaultDClickListener;
 import org.argeo.security.ui.admin.internal.UserTableViewer;
@@ -56,7 +58,7 @@ public class GroupsView extends UsersView implements ArgeoNames {
                                150));
                columnDefs.add(new ColumnDefinition(new UserNameLP(),
                                "Distinguished Name", 300));
-               
+
                // Create and configure the table
                groupTableViewerCmp = new MyUserTableViewer(parent, SWT.MULTI
                                | SWT.H_SCROLL | SWT.V_SCROLL, userAdmin);
@@ -77,6 +79,9 @@ public class GroupsView extends UsersView implements ArgeoNames {
        private class MyUserTableViewer extends UserTableViewer {
                private static final long serialVersionUID = 8467999509931900367L;
 
+               private final String[] knownProps = { UserAdminConstants.KEY_UID,
+                               UserAdminConstants.KEY_CN, UserAdminConstants.KEY_DN };
+
                public MyUserTableViewer(Composite parent, int style,
                                UserAdmin userAdmin) {
                        super(parent, style, userAdmin);
@@ -86,15 +91,31 @@ public class GroupsView extends UsersView implements ArgeoNames {
                protected List<User> listFilteredElements(String filter) {
                        Role[] roles;
                        try {
-                               roles = userAdmin.getRoles(filter);
+                               StringBuilder builder = new StringBuilder();
+                               StringBuilder tmpBuilder = new StringBuilder();
+                               if (UiAdminUtils.notNull(filter))
+                                       for (String prop : knownProps) {
+                                               tmpBuilder.append("(");
+                                               tmpBuilder.append(prop);
+                                               tmpBuilder.append("=*");
+                                               tmpBuilder.append(filter);
+                                               tmpBuilder.append("*)");
+                                       }
+                               if (tmpBuilder.length() > 1) {
+                                       builder.append("(&(objectclass=groupOfNames)(|");
+                                       builder.append(tmpBuilder.toString());
+                                       builder.append("))");
+                               } else
+                                       builder.append("(objectclass=groupOfNames)");
+                               roles = userAdmin.getRoles(builder.toString());
                        } catch (InvalidSyntaxException e) {
                                throw new ArgeoException("Unable to get roles with filter: "
                                                + filter, e);
                        }
                        List<User> users = new ArrayList<User>();
                        for (Role role : roles)
-                               if (role.getType() == Role.GROUP)
-                                       users.add((User) role);
+                               // if (role.getType() == Role.GROUP)
+                               users.add((User) role);
                        return users;
                }
        }
@@ -103,7 +124,6 @@ public class GroupsView extends UsersView implements ArgeoNames {
                groupTableViewerCmp.refresh();
        }
 
-       
        // Override generic view methods
        @Override
        public void dispose() {
diff --git a/org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/views/JcrRolesView.java b/org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/views/JcrRolesView.java
deleted file mode 100644 (file)
index 66f7a03..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * 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.security.ui.admin.views;
-
-public class JcrRolesView {
-}
-
-/** List all roles. Legacy. TODO Remove */
-// public class JcrRolesView extends ViewPart {
-// public final static String ID = SecurityAdminPlugin.PLUGIN_ID
-// + ".jcrRolesView";
-//
-// private Text newRole;
-//
-// private TableViewer viewer;
-// private UserAdminService userAdminService;
-//
-// private String addNewRoleText = "<add new role here>";
-//
-// @Override
-// public void createPartControl(Composite parent) {
-// parent.setLayout(new GridLayout(1, false));
-//
-// // new role text field
-// newRole = new Text(parent, SWT.BORDER);
-// newRole.setText(addNewRoleText);
-// newRole.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
-// // default action is add role
-// newRole.addListener(SWT.DefaultSelection, new Listener() {
-// private static final long serialVersionUID = -2367261849260929505L;
-//
-// public void handleEvent(Event evt) {
-// IWorkbench iw = SecurityAdminPlugin.getDefault().getWorkbench();
-// IHandlerService handlerService = (IHandlerService) iw
-// .getService(IHandlerService.class);
-// try {
-// handlerService.executeCommand(AddRole.COMMAND_ID, evt);
-// } catch (Exception e) {
-// throw new ArgeoException("Cannot execute add role command",
-// e);
-// }
-// }
-// });
-// // select all on focus
-// newRole.addListener(SWT.FocusIn, new Listener() {
-// private static final long serialVersionUID = 2612811281477034356L;
-//
-// public void handleEvent(Event e) {
-// newRole.selectAll();
-// }
-// });
-//
-// // roles table
-// Table table = new Table(parent, SWT.V_SCROLL | SWT.BORDER);
-// table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-// table.setLinesVisible(false);
-// table.setHeaderVisible(false);
-// viewer = new TableViewer(table);
-// viewer.setContentProvider(new RolesContentProvider());
-// viewer.setLabelProvider(new UsersLabelProvider());
-// getViewSite().setSelectionProvider(viewer);
-// viewer.setInput(getViewSite());
-// }
-//
-// @Override
-// public void setFocus() {
-// viewer.getTable().setFocus();
-// }
-//
-// public void setUserAdminService(UserAdminService userAdminService) {
-// this.userAdminService = userAdminService;
-// }
-//
-// public String getAddNewRoleText() {
-// return addNewRoleText;
-// }
-//
-// private class RolesContentProvider implements IStructuredContentProvider {
-// private static final long serialVersionUID = 7446442682717419289L;
-//
-// public Object[] getElements(Object inputElement) {
-// return userAdminService.listEditableRoles().toArray();
-// }
-//
-// public void dispose() {
-// }
-//
-// public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
-// }
-//
-// }
-//
-// private class UsersLabelProvider extends LabelProvider implements
-// ITableLabelProvider {
-// private static final long serialVersionUID = -1886204791002421430L;
-//
-// public String getColumnText(Object element, int columnIndex) {
-// return element.toString();
-// }
-//
-// public Image getColumnImage(Object element, int columnIndex) {
-// return null;
-// }
-//
-// }
-//
-// public String getNewRole() {
-// return newRole.getText();
-// }
-//
-// public void refresh() {
-// viewer.refresh();
-// newRole.setText(addNewRoleText);
-// }
-// }
diff --git a/org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/views/JcrUsersView.java b/org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/views/JcrUsersView.java
deleted file mode 100644 (file)
index dfdd737..0000000
+++ /dev/null
@@ -1,133 +0,0 @@
-/*
- * 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.security.ui.admin.views;
-
-public class JcrUsersView {
-}
-/** List all users with filter. Legacy. TODO Remove */
-// public class JcrUsersView extends ViewPart implements ArgeoNames {
-// public final static String ID = SecurityAdminPlugin.PLUGIN_ID
-// + ".jcrUsersView";
-//
-// /* DEPENDENCY INJECTION */
-// private Session session;
-//
-// private UsersTable userTableCmp;
-// private JcrUserListener userStructureListener;
-// private JcrUserListener userPropertiesListener;
-//
-// @Override
-// public void createPartControl(Composite parent) {
-// parent.setLayout(new FillLayout());
-//
-// // Create the composite that displays the list and a filter
-// userTableCmp = new UsersTable(parent, SWT.NO_FOCUS, session);
-// userTableCmp.populate(true, false);
-//
-// // Configure
-// userTableCmp.getTableViewer().addDoubleClickListener(
-// new ViewDoubleClickListener());
-// getViewSite().setSelectionProvider(userTableCmp.getTableViewer());
-//
-// // Add listener to refresh the list when something changes
-// userStructureListener = new JcrUserListener(getSite().getShell()
-// .getDisplay());
-// JcrUtils.addListener(session, userStructureListener, Event.NODE_ADDED
-// | Event.NODE_REMOVED, ArgeoJcrConstants.PEOPLE_BASE_PATH, null);
-// userPropertiesListener = new JcrUserListener(getSite().getShell()
-// .getDisplay());
-// JcrUtils.addListener(session, userStructureListener,
-// Event.PROPERTY_CHANGED | Event.PROPERTY_ADDED
-// | Event.PROPERTY_REMOVED,
-// ArgeoJcrConstants.PEOPLE_BASE_PATH,
-// ArgeoTypes.ARGEO_USER_PROFILE);
-// }
-//
-// @Override
-// public void setFocus() {
-// userTableCmp.setFocus();
-// }
-//
-// @Override
-// public void dispose() {
-// JcrUtils.removeListenerQuietly(session, userStructureListener);
-// JcrUtils.removeListenerQuietly(session, userPropertiesListener);
-// JcrUtils.logoutQuietly(session);
-// super.dispose();
-// }
-//
-// // public void setSession(Session session) {
-// // this.session = session;
-// // }
-//
-// public void refresh() {
-// this.getSite().getShell().getDisplay().asyncExec(new Runnable() {
-// @Override
-// public void run() {
-// userTableCmp.refresh();
-// }
-// });
-// }
-//
-// private class JcrUserListener implements EventListener {
-// private final Display display;
-//
-// public JcrUserListener(Display display) {
-// super();
-// this.display = display;
-// }
-//
-// @Override
-// public void onEvent(EventIterator events) {
-// display.asyncExec(new Runnable() {
-// @Override
-// public void run() {
-// userTableCmp.refresh();
-// }
-// });
-// }
-// }
-//
-// class ViewDoubleClickListener implements IDoubleClickListener {
-// public void doubleClick(DoubleClickEvent evt) {
-// if (evt.getSelection().isEmpty())
-// return;
-//
-// Object obj = ((IStructuredSelection) evt.getSelection())
-// .getFirstElement();
-// if (obj instanceof Node) {
-// try {
-// String username = ((Node) obj).getProperty(ARGEO_USER_ID)
-// .getString();
-// String commandId = OpenArgeoUserEditor.COMMAND_ID;
-// String paramName = OpenArgeoUserEditor.PARAM_USERNAME;
-// CommandUtils.callCommand(commandId, paramName, username);
-// } catch (RepositoryException e) {
-// throw new ArgeoException("Cannot open user editor", e);
-// }
-// }
-// }
-// }
-//
-// /* DEPENDENCY INJECTION */
-// public void setRepository(Repository repository) {
-// try {
-// session = repository.login();
-// } catch (RepositoryException re) {
-// throw new ArgeoException("Unable to initialise local session", re);
-// }
-// }
-// }
\ No newline at end of file
index 6604e61061421e6d2f07df6a36d30b9e8b70ede2..3438d85b0ddb3eaec676127db611441dc68e0cb9 100644 (file)
@@ -18,10 +18,6 @@ package org.argeo.security.ui.admin.views;
 import java.util.ArrayList;
 import java.util.List;
 
-import javax.transaction.UserTransaction;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.jcr.ArgeoNames;
@@ -29,6 +25,8 @@ import org.argeo.security.ui.admin.SecurityAdminPlugin;
 import org.argeo.security.ui.admin.internal.ColumnDefinition;
 import org.argeo.security.ui.admin.internal.CommonNameLP;
 import org.argeo.security.ui.admin.internal.MailLP;
+import org.argeo.security.ui.admin.internal.UiAdminUtils;
+import org.argeo.security.ui.admin.internal.UserAdminConstants;
 import org.argeo.security.ui.admin.internal.UserNameLP;
 import org.argeo.security.ui.admin.internal.UserTableDefaultDClickListener;
 import org.argeo.security.ui.admin.internal.UserTableViewer;
@@ -43,13 +41,12 @@ import org.osgi.service.useradmin.UserAdmin;
 
 /** List all users with filter - based on Ldif userAdmin */
 public class UsersView extends ViewPart implements ArgeoNames {
-       private final static Log log = LogFactory.getLog(UsersView.class);
+       // private final static Log log = LogFactory.getLog(UsersView.class);
        public final static String ID = SecurityAdminPlugin.PLUGIN_ID
                        + ".usersView";
 
        /* DEPENDENCY INJECTION */
        private UserAdmin userAdmin;
-       private UserTransaction userTransaction;
 
        // UI Objects
        private UserTableViewer userTableViewerCmp;
@@ -82,17 +79,22 @@ public class UsersView extends ViewPart implements ArgeoNames {
                // Really?
                userTableViewerCmp.refresh();
 
-//             try {
-//                     if (userTransaction != null)
-//                             userTransaction.begin();
-//             } catch (Exception e) {
-//                     throw new ArgeoException("Cannot begin transaction", e);
-//             }
+               // try {
+               // if (userTransaction != null)
+               // userTransaction.begin();
+               // } catch (Exception e) {
+               // throw new ArgeoException("Cannot begin transaction", e);
+               // }
        }
 
        private class MyUserTableViewer extends UserTableViewer {
                private static final long serialVersionUID = 8467999509931900367L;
 
+               private final String[] knownProps = { UserAdminConstants.KEY_UID,
+                               UserAdminConstants.KEY_DN, UserAdminConstants.KEY_CN,
+                               UserAdminConstants.KEY_FIRSTNAME,
+                               UserAdminConstants.KEY_LASTNAME, UserAdminConstants.KEY_MAIL };
+
                public MyUserTableViewer(Composite parent, int style,
                                UserAdmin userAdmin) {
                        super(parent, style, userAdmin);
@@ -101,16 +103,35 @@ public class UsersView extends ViewPart implements ArgeoNames {
                @Override
                protected List<User> listFilteredElements(String filter) {
                        Role[] roles;
+
                        try {
-                               roles = userAdmin.getRoles(filter);
+                               StringBuilder builder = new StringBuilder();
+
+                               StringBuilder tmpBuilder = new StringBuilder();
+                               if (UiAdminUtils.notNull(filter))
+                                       for (String prop : knownProps) {
+                                               tmpBuilder.append("(");
+                                               tmpBuilder.append(prop);
+                                               tmpBuilder.append("=*");
+                                               tmpBuilder.append(filter);
+                                               tmpBuilder.append("*)");
+                                       }
+                               if (tmpBuilder.length() > 1) {
+                                       builder.append("(&(objectclass=inetOrgPerson)(|");
+                                       builder.append(tmpBuilder.toString());
+                                       builder.append("))");
+                               } else
+                                       builder.append("(objectclass=inetOrgPerson)");
+                               roles = userAdmin.getRoles(builder.toString());
                        } catch (InvalidSyntaxException e) {
                                throw new ArgeoException("Unable to get roles with filter: "
                                                + filter, e);
                        }
                        List<User> users = new ArrayList<User>();
                        for (Role role : roles)
-                               if (role.getType() == Role.USER && role.getType() != Role.GROUP)
-                                       users.add((User) role);
+                               // if (role.getType() == Role.USER && role.getType() !=
+                               // Role.GROUP)
+                               users.add((User) role);
                        return users;
                }
        }
@@ -141,9 +162,4 @@ public class UsersView extends ViewPart implements ArgeoNames {
        public void setUserAdmin(UserAdmin userAdmin) {
                this.userAdmin = userAdmin;
        }
-
-       public void setUserTransaction(UserTransaction userTransaction) {
-               this.userTransaction = userTransaction;
-       }
-
 }
\ No newline at end of file