X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.ui%2Fsrc%2Forg%2Fargeo%2Fcms%2Fwidgets%2Fauth%2FCmsLogin.java;h=7560ceb30733faeed948bb189f0cd3e558eb3753;hb=743fc68ed576cb578a9cc6c59e1f423d29213be1;hp=54f0ac395251e08e143d91caf455138d58edc402;hpb=b8da6ff850049dd39531c1e50f2eef38c4e3298e;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms.ui/src/org/argeo/cms/widgets/auth/CmsLogin.java b/org.argeo.cms.ui/src/org/argeo/cms/widgets/auth/CmsLogin.java index 54f0ac395..7560ceb30 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/widgets/auth/CmsLogin.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/widgets/auth/CmsLogin.java @@ -7,20 +7,19 @@ import java.io.IOException; import java.util.List; import java.util.Locale; +import javax.security.auth.Subject; import javax.security.auth.callback.Callback; import javax.security.auth.callback.CallbackHandler; 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.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.cms.CmsMsg; -import org.argeo.cms.auth.CurrentUser; import org.argeo.cms.auth.HttpRequestCallback; import org.argeo.cms.i18n.LocaleUtils; import org.argeo.cms.ui.CmsStyles; @@ -59,6 +58,9 @@ public class CmsLogin implements CmsStyles, CallbackHandler { private final CmsView cmsView; + // optional subject to be set explicitly + private Subject subject = null; + public CmsLogin(CmsView cmsView) { this.cmsView = cmsView; defaultLocale = Activator.getNodeState().getDefaultLocale(); @@ -80,7 +82,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler { } protected boolean isAnonymous() { - return CurrentUser.isAnonymous(cmsView.getLoginContext().getSubject()); + return cmsView.isAnonymous(); } public final void createUi(Composite parent) { @@ -116,7 +118,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler { Locale locale = localeChoice == null ? this.defaultLocale : localeChoice.getSelectedLocale(); credentialsBlock = new Composite(parent, SWT.NONE); credentialsBlock.setLayout(new GridLayout()); - credentialsBlock.setLayoutData(CmsUtils.fillAll()); + // credentialsBlock.setLayoutData(CmsUtils.fillAll()); specificUserUi(credentialsBlock); @@ -147,11 +149,12 @@ public class CmsLogin implements CmsStyles, CallbackHandler { // We need a composite for the traversal credentialsBlock = new Composite(parent, SWT.NONE); credentialsBlock.setLayout(new GridLayout()); - credentialsBlock.setLayoutData(CmsUtils.fillAll()); + // credentialsBlock.setLayoutData(CmsUtils.fillAll()); + CmsUtils.style(credentialsBlock, CMS_LOGIN_DIALOG); Integer textWidth = 120; - CmsUtils.style(parent, CMS_USER_MENU); - + if (parent instanceof Shell) + CmsUtils.style(parent, CMS_USER_MENU); // new Label(this, SWT.NONE).setText(CmsMsg.username.lead()); usernameT = new Text(credentialsBlock, SWT.BORDER); usernameT.setMessage(username.lead(locale)); @@ -190,8 +193,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler { } /** - * To be overridden in order to provide custome login button and other - * links. + * To be overridden in order to provide custom login button and other links. */ protected void extendsCredentialsBlock(Composite credentialsBlock, Locale selectedLocale, SelectionListener loginSelectionListener) { @@ -216,6 +218,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler { protected Composite createLocalesBlock(final Composite parent) { Composite c = new Composite(parent, SWT.NONE); + CmsUtils.style(c, CMS_USER_MENU_ITEM); c.setLayout(CmsUtils.noSpaceGridLayout()); c.setLayoutData(CmsUtils.fillAll()); @@ -235,6 +238,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler { for (Integer i = 0; i < locales.size(); i++) { Locale locale = locales.get(i); Button button = new Button(c, SWT.RADIO); + CmsUtils.style(button, CMS_USER_MENU_ITEM); button.setData(i); button.setText(LocaleUtils.lead(locale.getDisplayName(locale), locale) + " (" + locale + ")"); // button.addListener(SWT.Selection, listener); @@ -246,17 +250,28 @@ public class CmsLogin implements CmsStyles, CallbackHandler { } protected boolean login() { + // TODO use CmsVie in order to retrieve subject? // Subject subject = cmsView.getLoginContext().getSubject(); - LoginContext loginContext = cmsView.getLoginContext(); + // LoginContext loginContext = cmsView.getLoginContext(); try { // // LOGIN // - loginContext.logout(); - loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, this); + // loginContext.logout(); + LoginContext loginContext; + if (subject == null) + loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, this); + else + loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject, this); loginContext.login(); - } catch (FailedLoginException e) { - log.warn(e.getMessage()); + cmsView.authChange(loginContext); + return true; + } catch (LoginException e) { + if (log.isTraceEnabled()) + log.warn("Login failed: " + e.getMessage(), e); + else + log.warn("Login failed: " + e.getMessage()); + try { Thread.sleep(3000); } catch (InterruptedException e2) { @@ -264,12 +279,11 @@ public class CmsLogin implements CmsStyles, CallbackHandler { } // ErrorFeedback.show("Login failed", e); return false; - } catch (LoginException e) { - log.error("Cannot login", e); - return false; } - cmsView.authChange(loginContext); - return true; + // catch (LoginException e) { + // log.error("Cannot login", e); + // return false; + // } } protected void logout() { @@ -284,13 +298,27 @@ public class CmsLogin implements CmsStyles, CallbackHandler { ((NameCallback) callback).setName(usernameT.getText()); else if (callback instanceof PasswordCallback && passwordT != null) ((PasswordCallback) callback).setPassword(passwordT.getTextChars()); - else if (callback instanceof HttpRequestCallback){ + else if (callback instanceof HttpRequestCallback) { ((HttpRequestCallback) callback).setRequest(UiContext.getHttpRequest()); ((HttpRequestCallback) callback).setResponse(UiContext.getHttpResponse()); + } else if (callback instanceof LanguageCallback) { + Locale toUse = null; + if (localeChoice != null) + toUse = localeChoice.getSelectedLocale(); + else if (defaultLocale != null) + toUse = defaultLocale; + + if (toUse != null) { + ((LanguageCallback) callback).setLocale(toUse); + UiContext.setLocale(toUse); + } + } - else if (callback instanceof LanguageCallback && localeChoice != null) - ((LanguageCallback) callback).setLocale(localeChoice.getSelectedLocale()); } } + public void setSubject(Subject subject) { + this.subject = subject; + } + }