Prevent modification on current user while launching user batch update
authorBruno Sinou <bsinou@argeo.org>
Wed, 8 Jan 2014 18:43:49 +0000 (18:43 +0000)
committerBruno Sinou <bsinou@argeo.org>
Wed, 8 Jan 2014 18:43:49 +0000 (18:43 +0000)
see https://www.argeo.org/bugzilla/show_bug.cgi?id=205

git-svn-id: https://svn.argeo.org/commons/trunk@6717 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/UserTableComposite.java
security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/wizards/MainUserInfoWizardPage.java
security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/wizards/UserBatchUpdateWizard.java

index 5e969653cd4fe00489eeee52ac5210e6feea8d55..720faded4c808de1273df7a86141331f87cb4aaa 100644 (file)
@@ -51,6 +51,8 @@ public class UserTableComposite extends Composite implements ArgeoNames {
        // private final static Log log =
        // LogFactory.getLog(UserTableComposite.class);
 
+       private static final long serialVersionUID = -7385959046279360420L;
+
        private TableViewer usersViewer;
        private Text filterTxt;
        private final static String FILTER_HELP_MSG = "Type filter criterion "
@@ -139,6 +141,13 @@ public class UserTableComposite extends Composite implements ArgeoNames {
        public TableViewer getTableViewer() {
                return usersViewer;
        }
+       
+       /** Returns filter String or null*/
+       protected String getFilterString() {
+               return hasFilter ? filterTxt.getText() : null;
+       }
+       
+       
 
        private TableViewer createTableViewer(final Composite parent) {
                int style = SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL;
@@ -161,19 +170,21 @@ public class UserTableComposite extends Composite implements ArgeoNames {
 
                TableViewerColumn column;
                int offset = 0;
-               // if (hasSelectionColumn) {
                if (hasSelectionColumn) {
                        offset = 1;
                        column = ViewerUtils.createTableViewerColumn(viewer, "", SWT.NONE,
                                        25);
-                       // column.setEditingSupport(new SelectedEditingSupport(viewer));
                        column.setLabelProvider(new ColumnLabelProvider() {
+                               private static final long serialVersionUID = 1L;
+
                                @Override
                                public String getText(Object element) {
                                        return null;
                                }
                        });
                        SelectionAdapter selectionAdapter = new SelectionAdapter() {
+                               private static final long serialVersionUID = 1L;
+
                                boolean allSelected = false;
 
                                @Override
@@ -211,41 +222,10 @@ public class UserTableComposite extends Composite implements ArgeoNames {
                return viewer;
        }
 
-       // private class SelectedEditingSupport extends EditingSupport {
-       // private final TableViewer viewer;
-       //
-       // public SelectedEditingSupport(TableViewer viewer) {
-       // super(viewer);
-       // this.viewer = viewer;
-       // }
-       //
-       // @Override
-       // protected CellEditor getCellEditor(Object element) {
-       // return new CheckboxCellEditor(viewer.getTable());
-       // }
-       //
-       // @Override
-       // protected boolean canEdit(Object element) {
-       // return true;
-       // }
-       //
-       // @Override
-       // protected Object getValue(Object element) {
-       // return selectedItems.contains(element);
-       // }
-       //
-       // @Override
-       // protected void setValue(Object element, Object value) {
-       // if ((Boolean) value && !selectedItems.contains(element))
-       // selectedItems.add((Node) element);
-       // else if (!(Boolean) value && selectedItems.contains(element))
-       // selectedItems.remove((Node) element);
-       // viewer.update(element, null);
-       // }
-       // }
-
        private class CLProvider extends SimpleJcrNodeLabelProvider {
 
+               private static final long serialVersionUID = 1L;
+
                public CLProvider(String propertyName) {
                        super(propertyName);
                }
@@ -301,6 +281,7 @@ public class UserTableComposite extends Composite implements ArgeoNames {
        }
 
        private class UsersContentProvider implements IStructuredContentProvider {
+               private static final long serialVersionUID = 1L;
 
                public Object[] getElements(Object inputElement) {
                        return (Object[]) inputElement;
@@ -322,6 +303,7 @@ public class UserTableComposite extends Composite implements ArgeoNames {
                filterTxt.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
                                | GridData.HORIZONTAL_ALIGN_FILL));
                filterTxt.addModifyListener(new ModifyListener() {
+                       private static final long serialVersionUID = 1L;
 
                        public void modifyText(ModifyEvent event) {
                                refreshFilteredList();
@@ -331,12 +313,9 @@ public class UserTableComposite extends Composite implements ArgeoNames {
 
        /**
         * Refresh the user list: caller might overwrite in order to display a
-        * subset of all users
+        * subset of all users, typically to remove current user from the list
         */
        protected void refreshFilteredList() {
-               // if (hasSelectionColumn)
-               // selectedItems.clear();
-
                List<Node> nodes;
                try {
                        nodes = JcrUtils.nodeIteratorToList(listFilteredElements(session,
@@ -347,18 +326,6 @@ public class UserTableComposite extends Composite implements ArgeoNames {
                }
        }
 
-       // /**
-       // * Enable a children class to directly set an array of users to the
-       // viewer,
-       // * when such a behaviour cannot be achieved by overwriting the
-       // * refreshFilteredList() method
-       // */
-       // protected void setFilteredList(Object[] nodes) {
-       // // if (hasSelectionColumn)
-       // // selectedItems.clear();
-       // usersViewer.setInput(nodes);
-       // }
-
        /**
         * Build repository request : caller might overwrite in order to display a
         * subset of all users
@@ -371,21 +338,9 @@ public class UserTableComposite extends Composite implements ArgeoNames {
                Selector source = factory.selector(ArgeoTypes.ARGEO_USER_PROFILE,
                                ArgeoTypes.ARGEO_USER_PROFILE);
 
-               // // Create a dynamic operand for each property on which we want to
-               // filter
-               // DynamicOperand userIdDO = factory.propertyValue(
-               // source.getSelectorName(), ARGEO_USER_ID);
-               // DynamicOperand fullNameDO = factory.propertyValue(
-               // source.getSelectorName(), Property.JCR_TITLE);
-               // DynamicOperand mailDO =
-               // factory.propertyValue(source.getSelectorName(),
-               // ARGEO_PRIMARY_EMAIL);
-
-               // Default Constraint: no source artifacts
+               // Dynamically build constraint depending on the filter String
                Constraint defaultC = null;
-               // Build constraints based the textArea content
                if (filter != null && !"".equals(filter.trim())) {
-                       // Parse the String
                        String[] strs = filter.trim().split(" ");
                        for (String token : strs) {
                                StaticOperand so = factory.literal(session.getValueFactory()
index 0a285bfd8d1715388bfb102548d05943628a6f2f..ad3662b6fc151f16f82c9fb9fdc29cc9caa66753 100644 (file)
@@ -57,23 +57,34 @@ public class MainUserInfoWizardPage extends WizardPage implements
                password2 = EclipseUiUtils.createGridLP(composite, "Repeat password",
                                this);
                setControl(composite);
+               
+               // Initialize buttons
+               setPageComplete(false);
+               getContainer().updateButtons();
        }
 
        @Override
        public void modifyText(ModifyEvent event) {
                String message = checkComplete();
-               if (message != null)
+               if (message != null) {
                        setMessage(message, WizardPage.ERROR);
-               else {
+                       setPageComplete(false);
+               } else {
                        setMessage("Complete", WizardPage.INFORMATION);
                        setPageComplete(true);
                }
+               getContainer().updateButtons();
        }
 
        /** @return error message or null if complete */
        protected String checkComplete() {
-//             if (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
-//                     return "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
+               // if (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
+               // return
+               // "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
+               
+               if (username.getText().trim().equals(""))
+                       return "User name must not be empty";
+               
                try {
                        UserDetails userDetails = userAdminService
                                        .loadUserByUsername(username.getText());
index 933a390193f738d5efecac38405a041acdb8ee88..71e4a1b734df17147c4023db823a6e1783a9c594 100644 (file)
@@ -21,6 +21,7 @@ import java.util.List;
 import java.util.Map;
 
 import javax.jcr.Node;
+import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.version.VersionManager;
@@ -306,6 +307,8 @@ public class UserBatchUpdateWizard extends Wizard {
        // Pages definition
        /** Displays a combo box that enables user to choose which action to perform */
        private class ChooseCommandWizardPage extends WizardPage {
+               private static final long serialVersionUID = 1L;
+
                private Combo chooseCommandCmb;
                private Button trueChk;
                private Text valueTxt;
@@ -338,6 +341,8 @@ public class UserBatchUpdateWizard extends Wizard {
                                        true));
 
                        chooseCommandCmb.addSelectionListener(new SelectionListener() {
+                               private static final long serialVersionUID = 1L;
+
                                @Override
                                public void widgetSelected(SelectionEvent e) {
                                        if (getCommand().equals(CMD_UPDATE_PASSWORD))
@@ -459,6 +464,7 @@ public class UserBatchUpdateWizard extends Wizard {
         */
        private class ChooseUsersWizardPage extends WizardPage implements
                        IPageChangedListener {
+               private static final long serialVersionUID = 1L;
                private UserTableComposite userTableCmp;
                private Composite container;
                private Session session;
@@ -473,8 +479,7 @@ public class UserBatchUpdateWizard extends Wizard {
                public void createControl(Composite parent) {
                        container = new Composite(parent, SWT.NONE);
                        container.setLayout(new FillLayout());
-                       userTableCmp = new UserTableComposite(container, SWT.NO_FOCUS,
-                                       session);
+                       userTableCmp = new MyUserTableCmp(container, SWT.NO_FOCUS, session);
                        userTableCmp.populate(true, true);
                        setControl(container);
 
@@ -498,6 +503,37 @@ public class UserBatchUpdateWizard extends Wizard {
                protected List<Node> getSelectedUsers() {
                        return userTableCmp.getSelectedUsers();
                }
+
+               private class MyUserTableCmp extends UserTableComposite {
+
+                       private static final long serialVersionUID = 1L;
+
+                       public MyUserTableCmp(Composite parent, int style, Session session) {
+                               super(parent, style, session);
+                       }
+
+                       @Override
+                       protected void refreshFilteredList() {
+                               List<Node> nodes = new ArrayList<Node>();
+                               try {
+                                       NodeIterator ni = listFilteredElements(session,
+                                                       getFilterString());
+
+                                       users: while (ni.hasNext()) {
+                                               Node currNode = ni.nextNode();
+                                               String username = currNode.hasProperty(ARGEO_USER_ID) ? currNode
+                                                               .getProperty(ARGEO_USER_ID).getString() : "";
+                                               if (username.equals(session.getUserID()))
+                                                       continue users;
+                                               else
+                                                       nodes.add(currNode);
+                                       }
+                                       getTableViewer().setInput(nodes.toArray());
+                               } catch (RepositoryException e) {
+                                       throw new ArgeoException("Unable to list users", e);
+                               }
+                       }
+               }
        }
 
        /**
@@ -505,6 +541,7 @@ public class UserBatchUpdateWizard extends Wizard {
         */
        private class ValidateAndLaunchWizardPage extends WizardPage implements
                        IPageChangedListener {
+               private static final long serialVersionUID = 1L;
                private UserTableComposite userTableCmp;
                private Session session;