X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fwidgets%2Fauth%2FCmsLogin.java;h=1051bd7239e0564f408711809c2b40f0ab4603c1;hb=cba35caa1a083661c8e113f5295b2dbfc90a16cd;hp=53a23a3243a995b3ee705f7adce589dc24a921a1;hpb=1d3c6771974fd559382cc6cd97c9d147ba511235;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLogin.java b/org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLogin.java index 53a23a324..1051bd723 100644 --- a/org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLogin.java +++ b/org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLogin.java @@ -17,16 +17,18 @@ import javax.security.auth.callback.LanguageCallback; import javax.security.auth.callback.NameCallback; import javax.security.auth.callback.PasswordCallback; import javax.security.auth.callback.UnsupportedCallbackException; +import javax.security.auth.login.FailedLoginException; import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; -import org.argeo.cms.CmsException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.cms.CmsMsg; import org.argeo.cms.CmsStyles; import org.argeo.cms.CmsView; import org.argeo.cms.auth.CurrentUser; import org.argeo.cms.auth.HttpRequestCallback; -import org.argeo.cms.i18n.Msg; +import org.argeo.cms.i18n.LocaleUtils; import org.argeo.cms.util.CmsUtils; import org.argeo.util.LocaleChoice; import org.eclipse.rap.rwt.RWT; @@ -48,8 +50,12 @@ import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class CmsLogin implements CmsStyles, CallbackHandler { + private final static Log log = LogFactory.getLog(CmsLogin.class); + + private Composite parent; private Text usernameT, passwordT; private Composite credentialsBlock; + private final SelectionListener loginSelectionListener; private final Locale defaultLocale; private LocaleChoice localeChoice = null; @@ -62,13 +68,30 @@ public class CmsLogin implements CmsStyles, CallbackHandler { List locales = getKernelHeader().getLocales(); if (locales != null) localeChoice = new LocaleChoice(locales, defaultLocale); + loginSelectionListener = new SelectionListener() { + private static final long serialVersionUID = -8832133363830973578L; + + @Override + public void widgetSelected(SelectionEvent e) { + login(); + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + } + }; } protected boolean isAnonymous() { return CurrentUser.isAnonymous(cmsView.getSubject()); } - public void createContents(Composite parent) { + public final void createUi(Composite parent) { + this.parent = parent; + createContents(parent); + } + + protected void createContents(Composite parent) { defaultCreateContents(parent); } @@ -94,6 +117,8 @@ public class CmsLogin implements CmsStyles, CallbackHandler { } protected Composite userUi(Composite parent) { + Locale locale = localeChoice == null ? this.defaultLocale + : localeChoice.getSelectedLocale(); credentialsBlock = new Composite(parent, SWT.NONE); credentialsBlock.setLayout(new GridLayout()); credentialsBlock.setLayoutData(CmsUtils.fillAll()); @@ -102,7 +127,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler { Label l = new Label(credentialsBlock, SWT.NONE); l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM); - l.setText(CmsMsg.logout.lead()); + l.setText(CmsMsg.logout.lead(locale)); GridData lData = CmsUtils.fillWidth(); lData.widthHint = 120; l.setLayoutData(lData); @@ -123,6 +148,8 @@ public class CmsLogin implements CmsStyles, CallbackHandler { } protected Composite anonymousUi(Composite parent) { + Locale locale = localeChoice == null ? this.defaultLocale + : localeChoice.getSelectedLocale(); // We need a composite for the traversal credentialsBlock = new Composite(parent, SWT.NONE); credentialsBlock.setLayout(new GridLayout()); @@ -133,7 +160,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler { // new Label(this, SWT.NONE).setText(CmsMsg.username.lead()); usernameT = new Text(credentialsBlock, SWT.BORDER); - usernameT.setMessage(username.lead(defaultLocale)); + usernameT.setMessage(username.lead(locale)); usernameT.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_USERNAME); GridData gd = CmsUtils.fillWidth(); gd.widthHint = textWidth; @@ -141,7 +168,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler { // new Label(this, SWT.NONE).setText(CmsMsg.password.lead()); passwordT = new Text(credentialsBlock, SWT.BORDER | SWT.PASSWORD); - passwordT.setMessage(password.lead(defaultLocale)); + passwordT.setMessage(password.lead(locale)); passwordT.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_PASSWORD); gd = CmsUtils.fillWidth(); gd.widthHint = textWidth; @@ -160,13 +187,40 @@ public class CmsLogin implements CmsStyles, CallbackHandler { passwordT.addTraverseListener(tl); parent.setTabList(new Control[] { credentialsBlock }); credentialsBlock.setTabList(new Control[] { usernameT, passwordT }); - credentialsBlock.setFocus(); + // credentialsBlock.setFocus(); + extendsCredentialsBlock(credentialsBlock, locale, + loginSelectionListener); if (localeChoice != null) createLocalesBlock(credentialsBlock); return credentialsBlock; } + /** + * To be overridden in order to provide custome login button and other + * links. + */ + protected void extendsCredentialsBlock(Composite credentialsBlock, + Locale selectedLocale, SelectionListener loginSelectionListener) { + + } + + protected void updateLocale(Locale selectedLocale) { + // save already entered values + String usernameStr = usernameT.getText(); + char[] pwd = passwordT.getTextChars(); + + for (Control child : parent.getChildren()) + child.dispose(); + createContents(parent); + if (parent.getParent() != null) + parent.getParent().layout(); + else + parent.layout(); + usernameT.setText(usernameStr); + passwordT.setTextChars(pwd); + } + protected Composite createLocalesBlock(final Composite parent) { Composite c = new Composite(parent, SWT.NONE); c.setLayout(CmsUtils.noSpaceGridLayout()); @@ -176,10 +230,12 @@ public class CmsLogin implements CmsStyles, CallbackHandler { private static final long serialVersionUID = 4891637813567806762L; public void widgetSelected(SelectionEvent event) { - localeChoice.setSelectedIndex((Integer) event.widget.getData()); - Locale selectedLocale = localeChoice.getSelectedLocale(); - usernameT.setMessage(username.lead(selectedLocale)); - passwordT.setMessage(password.lead(selectedLocale)); + Button button = (Button) event.widget; + if (button.getSelection()) { + localeChoice.setSelectedIndex((Integer) event.widget + .getData()); + updateLocale(localeChoice.getSelectedLocale()); + } }; }; @@ -188,17 +244,17 @@ public class CmsLogin implements CmsStyles, CallbackHandler { Locale locale = locales.get(i); Button button = new Button(c, SWT.RADIO); button.setData(i); - button.setText(Msg.lead(locale.getDisplayName(locale), locale) - + " (" + locale + ")"); + button.setText(LocaleUtils.lead(locale.getDisplayName(locale), + locale) + " (" + locale + ")"); // button.addListener(SWT.Selection, listener); button.addSelectionListener(selectionListener); - if (i == localeChoice.getDefaultIndex()) + if (i == localeChoice.getSelectedIndex()) button.setSelection(true); } return c; } - protected void login() { + protected boolean login() { Subject subject = cmsView.getSubject(); LoginContext loginContext; try { @@ -208,10 +264,15 @@ public class CmsLogin implements CmsStyles, CallbackHandler { new LoginContext(LOGIN_CONTEXT_ANONYMOUS, subject).logout(); loginContext = new LoginContext(LOGIN_CONTEXT_USER, subject, this); loginContext.login(); - } catch (LoginException e1) { - throw new CmsException("Cannot authenticate", e1); + } catch (FailedLoginException e) { + log.warn(e.getMessage()); + return false; + } catch (LoginException e) { + log.error("Cannot login", e); + return false; } cmsView.authChange(loginContext); + return true; } protected void logout() {