]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/editors/GroupMainPage.java
Plug password management
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / editors / GroupMainPage.java
index 7e880bfc4e4074e01e4a7118f98c1d6db763936d..401aa400d5eeaabe811474a8b99fe35d09c39d96 100644 (file)
@@ -31,10 +31,22 @@ 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;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerDropAdapter;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.dnd.DND;
+import org.eclipse.swt.dnd.DropTargetEvent;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.dnd.TransferData;
+import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.forms.AbstractFormPart;
 import org.eclipse.ui.forms.IManagedForm;
 import org.eclipse.ui.forms.SectionPart;
@@ -53,9 +65,11 @@ public class GroupMainPage extends FormPage implements ArgeoNames {
        final static String ID = "GroupEditor.mainPage";
 
        private final UserEditor editor;
-
        private UserAdmin userAdmin;
 
+       // Local configuration
+       private final int PRE_TITLE_INDENT = 10;
+
        public GroupMainPage(FormEditor editor, UserAdmin userAdmin) {
                super(editor, ID, "Main");
                this.editor = (UserEditor) editor;
@@ -65,25 +79,51 @@ public class GroupMainPage extends FormPage implements ArgeoNames {
        protected void createFormContent(final IManagedForm mf) {
                try {
                        ScrolledForm form = mf.getForm();
-                       form.getBody().getParent().setLayoutData(EclipseUiUtils.fillAll());
-                       form.setExpandHorizontal(true);
-                       refreshFormTitle(form);
-                       // GridLayout mainLayout = new GridLayout(1, true);
-                       createGeneralPart(form.getBody());
+                       refreshFormTitle();
+
+                       // Body
+                       Composite body = form.getBody();
+                       GridLayout mainLayout = new GridLayout();
+                       body.setLayout(mainLayout);
+                       appendOverviewPart(body);
+                       appendMembersPart(body);
                } catch (RepositoryException e) {
                        throw new ArgeoException("Cannot create form content", e);
                }
        }
 
        /** Creates the general section */
-       protected void createGeneralPart(Composite parent)
+       protected void appendOverviewPart(Composite parent) {
+               FormToolkit tk = getManagedForm().getToolkit();
+               Composite body = addSection(tk, parent, "Main information");
+               GridLayout layout = new GridLayout(2, false);
+               body.setLayout(layout);
+
+               Text distinguishedName = createLT(body, "Group Name",
+                               editor.getProperty(UserAdminConstants.KEY_UID));
+               distinguishedName.setEnabled(false);
+
+               final Text commonName = createLT(body, "Common Name",
+                               editor.getProperty(UserAdminConstants.KEY_CN));
+               commonName.setEnabled(false);
+
+               // create form part (controller)
+               AbstractFormPart part = new SectionPart((Section) body.getParent()) {
+                       public void commit(boolean onSave) {
+                               super.commit(onSave);
+                       }
+               };
+               getManagedForm().addPart(part);
+       }
+
+       /** Filtered table with members. Has drag & drop ability */
+       protected void appendMembersPart(Composite parent)
                        throws RepositoryException {
-               parent.setLayout(new GridLayout());
 
                FormToolkit tk = getManagedForm().getToolkit();
                Section section = tk.createSection(parent, Section.TITLE_BAR);
                section.setLayoutData(EclipseUiUtils.fillAll());
-               section.setText("Members of "
+               section.setText("Members of group "
                                + editor.getProperty(UserAdminConstants.KEY_CN));
 
                // Composite body = tk.createComposite(section, SWT.NONE);
@@ -109,12 +149,6 @@ public class GroupMainPage extends FormPage implements ArgeoNames {
        private List<ColumnDefinition> columnDefs = new ArrayList<ColumnDefinition>();
 
        public void createMemberPart(Composite parent) {
-               // parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
-               // parent2.setLayoutData(EclipseUiUtils.fillAll());
-               // Composite parent = new Composite(parent2, SWT.NO_FOCUS);
-               // parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
-               // parent.setLayoutData(EclipseUiUtils.fillAll());
-
                parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
                // Define the displayed columns
                columnDefs.add(new ColumnDefinition(new RoleIconLP(), "", 0, 24));
@@ -139,6 +173,12 @@ public class GroupMainPage extends FormPage implements ArgeoNames {
                userViewer.addDoubleClickListener(new UserTableDefaultDClickListener());
                // Really?
                userTableViewerCmp.refresh();
+
+               // Drag and drop
+               int operations = DND.DROP_COPY | DND.DROP_MOVE;
+               Transfer[] tt = new Transfer[] { TextTransfer.getInstance() };
+               userViewer.addDropSupport(operations, tt, new GroupDropListener(
+                               userViewer, userAdmin, (Group) editor.getDisplayedUser()));
        }
 
        private class MyUserTableViewer extends UserTableViewer {
@@ -161,22 +201,113 @@ public class GroupMainPage extends FormPage implements ArgeoNames {
                }
        }
 
-       private void refreshFormTitle(ScrolledForm form) throws RepositoryException {
-               // form.setText(getProperty(Property.JCR_TITLE)
-               // + (userProfile.getProperty(ARGEO_ENABLED).getBoolean() ? ""
-               // : " [DISABLED]"));
+       private void refreshFormTitle() throws RepositoryException {
+               getManagedForm().getForm().setText(
+                               editor.getProperty(UserAdminConstants.KEY_CN));
        }
 
-       // private class FormPartML implements ModifyListener {
-       // private static final long serialVersionUID = 6299808129505381333L;
-       // private AbstractFormPart formPart;
-       //
-       // public FormPartML(AbstractFormPart generalPart) {
-       // this.formPart = generalPart;
-       // }
-       //
-       // public void modifyText(ModifyEvent e) {
-       // formPart.markDirty();
-       // }
-       // }
+       /**
+        * Defines this table as being a potential target to add group membership
+        * (roles) to this user
+        */
+       private class GroupDropListener extends ViewerDropAdapter {
+               private static final long serialVersionUID = 2893468717831451621L;
+
+               private final UserAdmin myUserAdmin;
+               private final Group myGroup;
+
+               public GroupDropListener(Viewer viewer, UserAdmin userAdmin, Group group) {
+                       super(viewer);
+                       this.myUserAdmin = userAdmin;
+                       this.myGroup = group;
+               }
+
+               @Override
+               public boolean validateDrop(Object target, int operation,
+                               TransferData transferType) {
+                       // Target is always OK in a list only view
+                       // TODO check if not a string
+                       boolean validDrop = true;
+                       return validDrop;
+               }
+
+               @Override
+               public void drop(DropTargetEvent event) {
+                       String newUserName = (String) event.data;
+                       Role role = myUserAdmin.getRole(newUserName);
+                       // TODO this check should be done before.
+                       if (role.getType() == Role.USER) {
+                               // TODO check if the user is already member of this group
+                               // we expect here that there is already a begun transaction
+                               // TODO implement the dirty state
+                               editor.beginTransactionIfNeeded();
+                               User user = (User) role;
+                               myGroup.addMember(user);
+                       } else if (role.getType() == Role.GROUP) {
+                               Group newGroup = (Group) role;
+
+                               Shell shell = getViewer().getControl().getShell();
+                               // Sanity checks
+                               if (myGroup == newGroup) { // Equality
+
+                                       MessageDialog.openError(shell, "Forbidden addition ",
+                                                       "A group cannot be a member of itself.");
+                                       return;
+                               }
+
+                               // Cycle
+                               String myName = myGroup.getName();
+                               List<User> myMemberships = editor.getFlatGroups(myGroup);
+                               if (myMemberships.contains(newGroup)) {
+                                       MessageDialog.openError(shell, "Forbidden addition: cycle",
+                                                       "Cannot add " + newUserName + " to group " + myName
+                                                                       + ". This would create a cycle");
+                                       return;
+                               }
+
+                               // Already member
+                               List<User> newGroupMemberships = editor.getFlatGroups(newGroup);
+                               if (newGroupMemberships.contains(myGroup)) {
+                                       MessageDialog.openError(shell, "Forbidden addition",
+                                                       "Cannot add " + newUserName + " to group " + myName
+                                                                       + ", this membership already exists");
+                                       return;
+                               }
+
+                               editor.beginTransactionIfNeeded();
+                               // TODO implement the dirty state
+                               myGroup.addMember(newGroup);
+                       }
+                       super.drop(event);
+               }
+
+               @Override
+               public boolean performDrop(Object data) {
+                       userTableViewerCmp.refresh();
+                       return true;
+               }
+       }
+
+       // LOCAL HELPERS
+       private Composite addSection(FormToolkit tk, Composite parent, String title) {
+               Section section = tk.createSection(parent, Section.TITLE_BAR);
+               GridData gd = EclipseUiUtils.fillWidth();
+               gd.verticalAlignment = PRE_TITLE_INDENT;
+               section.setLayoutData(gd);
+               section.setText(title);
+               Composite body = tk.createComposite(section, SWT.WRAP);
+               body.setLayoutData(EclipseUiUtils.fillAll());
+               section.setClient(body);
+               return body;
+       }
+
+       /** Creates label and text. */
+       protected Text createLT(Composite body, String label, String value) {
+               FormToolkit toolkit = getManagedForm().getToolkit();
+               Label lbl = toolkit.createLabel(body, label);
+               lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
+               Text text = toolkit.createText(body, value, SWT.BORDER);
+               text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+               return text;
+       }
 }
\ No newline at end of file