X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.security.ui.admin%2Fsrc%2Forg%2Fargeo%2Fsecurity%2Fui%2Fadmin%2Finternal%2Fcommands%2FNewGroup.java;h=75b9b0b33e4e52e06d675014c644d096a42c0d2e;hb=8260f4470f514ea347ca53f5b4dfc632c4a4de66;hp=bc90a46d7a9b77a6dc669046b782cfd5d038e4bc;hpb=f0be5ec381492eff23d4576ee4122734af4ad877;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/commands/NewGroup.java b/org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/commands/NewGroup.java index bc90a46d7..75b9b0b33 100644 --- a/org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/commands/NewGroup.java +++ b/org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/commands/NewGroup.java @@ -16,15 +16,15 @@ package org.argeo.security.ui.admin.internal.commands; import java.util.Dictionary; -import java.util.List; +import java.util.Map; -import org.argeo.ArgeoException; +import org.argeo.cms.CmsException; import org.argeo.eclipse.ui.EclipseUiUtils; import org.argeo.eclipse.ui.dialogs.ErrorFeedback; import org.argeo.jcr.ArgeoNames; import org.argeo.osgi.useradmin.LdifName; +import org.argeo.osgi.useradmin.UserAdminConf; import org.argeo.security.ui.admin.SecurityAdminPlugin; -import org.argeo.security.ui.admin.internal.UiAdminUtils; import org.argeo.security.ui.admin.internal.UserAdminWrapper; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; @@ -33,8 +33,8 @@ import org.eclipse.jface.wizard.Wizard; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; -import org.eclipse.swt.events.ModifyEvent; -import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Combo; @@ -46,7 +46,7 @@ import org.osgi.service.useradmin.Group; import org.osgi.service.useradmin.Role; import org.osgi.service.useradmin.UserAdminEvent; -/** Create a new group. */ +/** Create a new group */ public class NewGroup extends AbstractHandler { public final static String ID = SecurityAdminPlugin.PLUGIN_ID + ".newGroup"; @@ -64,10 +64,10 @@ public class NewGroup extends AbstractHandler { private class NewGroupWizard extends Wizard { - // pages + // Pages private MainGroupInfoWizardPage mainGroupInfo; - // End user fields + // UI fields private Text dNameTxt, commonNameTxt, descriptionTxt; private Combo baseDnCmb; @@ -88,12 +88,14 @@ public class NewGroup extends AbstractHandler { String commonName = commonNameTxt.getText(); try { userAdminWrapper.beginTransactionIfNeeded(); + String dn = getDn(commonName); Group group = (Group) userAdminWrapper.getUserAdmin() - .createRole(getDn(commonName), Role.GROUP); + .createRole(dn, Role.GROUP); Dictionary props = group.getProperties(); String descStr = descriptionTxt.getText(); - if (UiAdminUtils.notNull(descStr)) + if (EclipseUiUtils.notEmpty(descStr)) props.put(LdifName.description.name(), descStr); + userAdminWrapper.commitOrNotifyTransactionStateChange(); userAdminWrapper.notifyListeners(new UserAdminEvent(null, UserAdminEvent.ROLE_CREATED, group)); return true; @@ -104,7 +106,7 @@ public class NewGroup extends AbstractHandler { } private class MainGroupInfoWizardPage extends WizardPage implements - ModifyListener, ArgeoNames { + FocusListener, ArgeoNames { private static final long serialVersionUID = -3150193365151601807L; public MainGroupInfoWizardPage() { @@ -117,40 +119,21 @@ public class NewGroup extends AbstractHandler { @Override public void createControl(Composite parent) { Composite bodyCmp = new Composite(parent, SWT.NONE); + setControl(bodyCmp); bodyCmp.setLayout(new GridLayout(2, false)); + dNameTxt = EclipseUiUtils.createGridLT(bodyCmp, - "Distinguished name"); // Read-only -> no listener + "Distinguished name"); dNameTxt.setEnabled(false); baseDnCmb = createGridLC(bodyCmp, "Base DN"); - // Initialise before adding the listener top avoid NPE + // Initialise before adding the listener to avoid NPE initialiseDnCmb(baseDnCmb); - baseDnCmb.addModifyListener(this); - baseDnCmb.addModifyListener(new ModifyListener() { - private static final long serialVersionUID = -1435351236582736843L; - - @Override - public void modifyText(ModifyEvent event) { - String name = commonNameTxt.getText(); - dNameTxt.setText(getDn(name)); - } - }); + baseDnCmb.addFocusListener(this); commonNameTxt = EclipseUiUtils.createGridLT(bodyCmp, - "Common name", this); - commonNameTxt.addModifyListener(new ModifyListener() { - private static final long serialVersionUID = -1435351236582736843L; - - @Override - public void modifyText(ModifyEvent event) { - String name = commonNameTxt.getText(); - if (name.trim().equals("")) { - dNameTxt.setText(""); - } else { - dNameTxt.setText(getDn(name)); - } - } - }); + "Common name"); + commonNameTxt.addFocusListener(this); Label descLbl = new Label(bodyCmp, SWT.LEAD); descLbl.setText("Description"); @@ -159,9 +142,7 @@ public class NewGroup extends AbstractHandler { descriptionTxt = new Text(bodyCmp, SWT.LEAD | SWT.MULTI | SWT.WRAP | SWT.BORDER); descriptionTxt.setLayoutData(EclipseUiUtils.fillAll()); - descriptionTxt.addModifyListener(this); - - setControl(bodyCmp); + descriptionTxt.addFocusListener(this); // Initialize buttons setPageComplete(false); @@ -169,7 +150,13 @@ public class NewGroup extends AbstractHandler { } @Override - public void modifyText(ModifyEvent event) { + public void focusLost(FocusEvent event) { + String name = commonNameTxt.getText(); + if (EclipseUiUtils.isEmpty(name)) + dNameTxt.setText(""); + else + dNameTxt.setText(getDn(name)); + String message = checkComplete(); if (message != null) { setMessage(message, WizardPage.ERROR); @@ -181,7 +168,11 @@ public class NewGroup extends AbstractHandler { getContainer().updateButtons(); } - /** @return error message or null if complete */ + @Override + public void focusGained(FocusEvent event) { + } + + /** @return the error message or null if complete */ protected String checkComplete() { String name = commonNameTxt.getText(); @@ -205,20 +196,32 @@ public class NewGroup extends AbstractHandler { } } + private Map getDns() { + return userAdminWrapper.getKnownBaseDns(true); + } + private String getDn(String cn) { - return "cn=" + cn + ",ou=groups," + baseDnCmb.getText(); + Map dns = getDns(); + String bdn = baseDnCmb.getText(); + if (EclipseUiUtils.notEmpty(bdn)) { + Dictionary props = UserAdminConf.uriAsProperties(dns + .get(bdn)); + String dn = LdifName.cn.name() + "=" + cn + "," + + UserAdminConf.groupBase.getValue(props) + "," + bdn; + return dn; + } + return null; } private void initialiseDnCmb(Combo combo) { - List dns = userAdminWrapper.getKnownBaseDns(true); + Map dns = userAdminWrapper.getKnownBaseDns(true); if (dns.isEmpty()) - throw new ArgeoException( - "No writable base dn found. Cannot create user"); - combo.setItems(dns.toArray(new String[0])); + throw new CmsException( + "No writable base dn found. Cannot create group"); + combo.setItems(dns.keySet().toArray(new String[0])); if (dns.size() == 1) combo.select(0); } - } private Combo createGridLC(Composite parent, String label) {