Make CMS login more robust.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / widgets / auth / CmsLogin.java
index 792471996a81965ebdfb4bca364a202ce2af4414..cc0e766d4c35d162217c736aa6ea809141e7e3f2 100644 (file)
@@ -14,22 +14,21 @@ 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.LocaleUtils;
 import org.argeo.cms.auth.HttpRequestCallback;
-import org.argeo.cms.i18n.LocaleUtils;
 import org.argeo.cms.ui.CmsStyles;
 import org.argeo.cms.ui.CmsView;
 import org.argeo.cms.ui.internal.Activator;
 import org.argeo.cms.util.CmsUtils;
+import org.argeo.eclipse.ui.specific.UiContext;
 import org.argeo.node.NodeConstants;
-import org.eclipse.rap.rwt.RWT;
+import org.argeo.node.NodeState;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.MouseAdapter;
 import org.eclipse.swt.events.MouseEvent;
@@ -60,12 +59,20 @@ 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();
-               List<Locale> locales = Activator.getNodeState().getLocales();
-               if (locales != null)
-                       localeChoice = new LocaleChoice(locales, defaultLocale);
+               NodeState nodeState = Activator.getNodeState();
+               if (nodeState != null) {
+                       defaultLocale = nodeState.getDefaultLocale();
+                       List<Locale> locales = nodeState.getLocales();
+                       if (locales != null)
+                               localeChoice = new LocaleChoice(locales, defaultLocale);
+               } else {
+                       defaultLocale = Locale.getDefault();
+               }
                loginSelectionListener = new SelectionListener() {
                        private static final long serialVersionUID = -8832133363830973578L;
 
@@ -81,7 +88,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
        }
 
        protected boolean isAnonymous() {
-               return CurrentUser.isAnonymous(cmsView.getSubject());
+               return cmsView.isAnonymous();
        }
 
        public final void createUi(Composite parent) {
@@ -117,12 +124,12 @@ 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);
 
                Label l = new Label(credentialsBlock, SWT.NONE);
-               l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
+               CmsUtils.style(l, CMS_USER_MENU_ITEM);
                l.setText(CmsMsg.logout.lead(locale));
                GridData lData = CmsUtils.fillWidth();
                lData.widthHint = 120;
@@ -148,15 +155,16 @@ 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;
-               parent.setData(RWT.CUSTOM_VARIANT, 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));
-               usernameT.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_USERNAME);
+               CmsUtils.style(usernameT, CMS_LOGIN_DIALOG_USERNAME);
                GridData gd = CmsUtils.fillWidth();
                gd.widthHint = textWidth;
                usernameT.setLayoutData(gd);
@@ -164,7 +172,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(locale));
-               passwordT.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_PASSWORD);
+               CmsUtils.style(passwordT, CMS_LOGIN_DIALOG_PASSWORD);
                gd = CmsUtils.fillWidth();
                gd.widthHint = textWidth;
                passwordT.setLayoutData(gd);
@@ -182,7 +190,12 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
                passwordT.addTraverseListener(tl);
                parent.setTabList(new Control[] { credentialsBlock });
                credentialsBlock.setTabList(new Control[] { usernameT, passwordT });
-               // credentialsBlock.setFocus();
+
+               // Button
+               Button loginButton = new Button(credentialsBlock, SWT.PUSH);
+               loginButton.setText(CmsMsg.login.lead(locale));
+               loginButton.setLayoutData(CmsUtils.fillWidth());
+               loginButton.addSelectionListener(loginSelectionListener);
 
                extendsCredentialsBlock(credentialsBlock, locale, loginSelectionListener);
                if (localeChoice != null)
@@ -191,8 +204,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) {
@@ -217,6 +229,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());
 
@@ -236,6 +249,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);
@@ -247,17 +261,28 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
        }
 
        protected boolean login() {
-               Subject subject = cmsView.getSubject();
-               LoginContext loginContext;
+               // TODO use CmsVie in order to retrieve subject?
+               // Subject subject = cmsView.getLoginContext().getSubject();
+               // LoginContext loginContext = cmsView.getLoginContext();
                try {
                        //
                        // LOGIN
                        //
-                       new LoginContext(NodeConstants.LOGIN_CONTEXT_ANONYMOUS, subject).logout();
-                       loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject, 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) {
@@ -265,12 +290,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() {
@@ -281,15 +305,31 @@ public class CmsLogin implements CmsStyles, CallbackHandler {
        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (Callback callback : callbacks) {
-                       if (callback instanceof NameCallback)
+                       if (callback instanceof NameCallback && usernameT != null)
                                ((NameCallback) callback).setName(usernameT.getText());
-                       else if (callback instanceof PasswordCallback)
+                       else if (callback instanceof PasswordCallback && passwordT != null)
                                ((PasswordCallback) callback).setPassword(passwordT.getTextChars());
-                       else if (callback instanceof HttpRequestCallback)
-                               ((HttpRequestCallback) callback).setRequest(RWT.getRequest());
-                       else if (callback instanceof LanguageCallback && localeChoice != null)
-                               ((LanguageCallback) callback).setLocale(localeChoice.getSelectedLocale());
+                       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);
+                               }
+
+                       }
                }
        }
 
+       public void setSubject(Subject subject) {
+               this.subject = subject;
+       }
+
 }