From 94114b7d025df8452c96a23554ec96c32e6c459b Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Tue, 13 Oct 2015 20:01:01 +0000 Subject: [PATCH] Authentication working for workbench, web and data. git-svn-id: https://svn.argeo.org/commons/trunk@8478 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../src/org/argeo/cms/util/UserMenu.java | 37 +++++++++------ .../org/argeo/cms/widgets/auth/CmsLogin.java | 34 ++++++++------ .../security/ui/rap/RapLoginEntryPoint.java | 11 +++++ .../security/ui/login/WorkbenchLogin.java | 46 +++++++++---------- 4 files changed, 74 insertions(+), 54 deletions(-) diff --git a/org.argeo.cms/src/org/argeo/cms/util/UserMenu.java b/org.argeo.cms/src/org/argeo/cms/util/UserMenu.java index 40861742f..95c7b2159 100644 --- a/org.argeo.cms/src/org/argeo/cms/util/UserMenu.java +++ b/org.argeo.cms/src/org/argeo/cms/util/UserMenu.java @@ -5,8 +5,8 @@ import org.eclipse.rap.rwt.RWT; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ShellAdapter; import org.eclipse.swt.events.ShellEvent; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; @@ -17,8 +17,14 @@ public class UserMenu extends CmsLogin { public UserMenu(Control source, boolean autoclose) { super(CmsUtils.getCmsView()); - shell = new Shell(source.getDisplay(), SWT.NO_TRIM | SWT.BORDER - | SWT.ON_TOP); + if (source != null) { + shell = new Shell(Display.getCurrent(), SWT.NO_TRIM | SWT.BORDER + | SWT.ON_TOP); + } else { + shell = new Shell(Display.getCurrent(), SWT.NO_TRIM); + shell.setMaximized(true); + shell.setLayout(CmsUtils.noSpaceGridLayout()); + } shell.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU); if (isAnonymous()) { @@ -27,19 +33,20 @@ public class UserMenu extends CmsLogin { userUi(shell); } - shell.pack(); - shell.layout(); - if (autoclose)// popup + if (source != null) {// popup + shell.pack(); + shell.layout(); shell.setLocation(source.toDisplay( source.getSize().x - shell.getSize().x, source.getSize().y)); - else // centered - { - Rectangle shellBounds = Display.getCurrent().getBounds();// RAP - Point dialogSize = shell.getSize(); - int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2; - int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2; - shell.setLocation(x, y); - + } else { // centered + Composite parent = getCredentialsBlock(); + parent.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, + true)); + // Rectangle shellBounds = shell.getBounds();// RAP + // Point dialogSize = parent.getSize(); + // int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2; + // int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2; + // parent.setLocation(x, y); } if (autoclose) shell.addShellListener(new ShellAdapter() { 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 9edc2f06c..d3a14dc80 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 @@ -36,6 +36,8 @@ import org.eclipse.swt.widgets.Text; public class CmsLogin implements CmsStyles, CallbackHandler { private Text username, password; + private Composite credentialsBlock; + private final CmsView cmsView; public CmsLogin(CmsView cmsView) { @@ -46,15 +48,19 @@ public class CmsLogin implements CmsStyles, CallbackHandler { return CurrentUser.isAnonymous(cmsView.getSubject()); } + protected Composite getCredentialsBlock() { + return credentialsBlock; + } + protected void userUi(Composite parent) { parent.setLayout(CmsUtils.noSpaceGridLayout()); - Composite c = new Composite(parent, SWT.NONE); - c.setLayout(new GridLayout()); - c.setLayoutData(CmsUtils.fillAll()); + credentialsBlock = new Composite(parent, SWT.NONE); + credentialsBlock.setLayout(new GridLayout()); + credentialsBlock.setLayoutData(CmsUtils.fillAll()); - specificUserUi(c); + specificUserUi(credentialsBlock); - Label l = new Label(c, SWT.NONE); + Label l = new Label(credentialsBlock, SWT.NONE); l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM); l.setText(CmsMsg.logout.lead()); GridData lData = CmsUtils.fillWidth(); @@ -79,15 +85,15 @@ public class CmsLogin implements CmsStyles, CallbackHandler { parent.setLayout(CmsUtils.noSpaceGridLayout()); // We need a composite for the traversal - Composite c = new Composite(parent, SWT.NONE); - c.setLayout(new GridLayout()); - c.setLayoutData(CmsUtils.fillAll()); + credentialsBlock = new Composite(parent, SWT.NONE); + credentialsBlock.setLayout(new GridLayout()); + credentialsBlock.setLayoutData(CmsUtils.fillAll()); Integer textWidth = 120; parent.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU); // new Label(this, SWT.NONE).setText(CmsMsg.username.lead()); - username = new Text(c, SWT.BORDER); + username = new Text(credentialsBlock, SWT.BORDER); username.setMessage(CmsMsg.username.lead()); username.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_USERNAME); GridData gd = CmsUtils.fillWidth(); @@ -95,7 +101,7 @@ public class CmsLogin implements CmsStyles, CallbackHandler { username.setLayoutData(gd); // new Label(this, SWT.NONE).setText(CmsMsg.password.lead()); - password = new Text(c, SWT.BORDER | SWT.PASSWORD); + password = new Text(credentialsBlock, SWT.BORDER | SWT.PASSWORD); password.setMessage(CmsMsg.password.lead()); password.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_PASSWORD); gd = CmsUtils.fillWidth(); @@ -110,12 +116,12 @@ public class CmsLogin implements CmsStyles, CallbackHandler { login(); } }; - c.addTraverseListener(tl); + credentialsBlock.addTraverseListener(tl); username.addTraverseListener(tl); password.addTraverseListener(tl); - parent.setTabList(new Control[] { c }); - c.setTabList(new Control[] { username, password }); - c.setFocus(); + parent.setTabList(new Control[] { credentialsBlock }); + credentialsBlock.setTabList(new Control[] { username, password }); + credentialsBlock.setFocus(); } protected void login() { diff --git a/org.argeo.security.ui.rap/src/org/argeo/security/ui/rap/RapLoginEntryPoint.java b/org.argeo.security.ui.rap/src/org/argeo/security/ui/rap/RapLoginEntryPoint.java index bb97b6549..bc66b164f 100644 --- a/org.argeo.security.ui.rap/src/org/argeo/security/ui/rap/RapLoginEntryPoint.java +++ b/org.argeo.security.ui.rap/src/org/argeo/security/ui/rap/RapLoginEntryPoint.java @@ -4,6 +4,7 @@ import javax.servlet.http.HttpServletRequest; import org.argeo.security.ui.login.WorkbenchLogin; import org.eclipse.rap.rwt.RWT; +import org.eclipse.rap.rwt.client.service.JavaScriptExecutor; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.PlatformUI; @@ -23,4 +24,14 @@ public class RapLoginEntryPoint extends WorkbenchLogin { protected HttpServletRequest getRequest() { return RWT.getRequest(); } + + @Override + public int createUI() { + JavaScriptExecutor jsExecutor = RWT.getClient().getService( + JavaScriptExecutor.class); + int returnCode = super.createUI(); + jsExecutor.execute("location.reload()"); + return returnCode; + } + } diff --git a/org.argeo.security.ui/src/org/argeo/security/ui/login/WorkbenchLogin.java b/org.argeo.security.ui/src/org/argeo/security/ui/login/WorkbenchLogin.java index 0eee6745c..e36d1d1ae 100644 --- a/org.argeo.security.ui/src/org/argeo/security/ui/login/WorkbenchLogin.java +++ b/org.argeo.security.ui/src/org/argeo/security/ui/login/WorkbenchLogin.java @@ -16,9 +16,11 @@ import org.argeo.cms.auth.AuthConstants; import org.argeo.cms.auth.CurrentUser; import org.argeo.cms.auth.HttpRequestCallbackHandler; import org.argeo.cms.util.UserMenu; +import org.argeo.cms.widgets.auth.CmsLogin; import org.argeo.eclipse.ui.specific.UiContext; import org.eclipse.rap.rwt.application.EntryPoint; import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; @@ -37,11 +39,11 @@ public abstract class WorkbenchLogin implements EntryPoint, CmsView { subject, new HttpRequestCallbackHandler(getRequest())); loginContext.login(); } catch (CredentialNotFoundException e) { - Shell shell = new Shell(display, SWT.NO_TRIM); - shell.setMaximized(true); - //shell.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); - UserMenu userMenu = new UserMenu(shell, false); - shell.open(); + // Shell shell = new Shell(display, SWT.NO_TRIM); + // shell.setMaximized(true); + // shell.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); + UserMenu userMenu = new UserMenu(null, false); + // shell.open(); while (!userMenu.getShell().isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); @@ -62,35 +64,17 @@ public abstract class WorkbenchLogin implements EntryPoint, CmsView { return new Integer(result); } }); + // explicit workbench closing + logout(); } finally { display.dispose(); } - // explicit workbench closing - logout(); return returnCode; } protected abstract int createAndRunWorkbench(Display display, String username); - // private void fullLogout() { - // String username = CurrentUser.getUsername(subject); - // try { - // LoginContext loginContext = new LoginContext( - // AuthConstants.LOGIN_CONTEXT_USER, subject); - // loginContext.logout(); - // HttpServletRequest httpRequest = getRequest(); - // httpRequest.setAttribute(HttpContext.AUTHORIZATION, null); - // HttpSession httpSession = httpRequest.getSession(); - // httpSession.setAttribute(HttpContext.AUTHORIZATION, null); - // httpSession.setMaxInactiveInterval(1); - // log.info("Logged out " + (username != null ? username : "") - // + " (THREAD=" + Thread.currentThread().getId() + ")"); - // } catch (LoginException e) { - // log.error("Error when logging out", e); - // } - // } - protected HttpServletRequest getRequest() { return UiContext.getHttpRequest(); } @@ -134,4 +118,16 @@ public abstract class WorkbenchLogin implements EntryPoint, CmsView { return null; } + // private class WorbenchCmsLogin extends CmsLogin { + // private final Shell shell; + // + // public WorbenchCmsLogin(CmsView cmsView, Shell shell) { + // super(cmsView); + // this.shell = shell; + // shell + // Composite parent = new Composite(shell, SWT.NONE); + // anonymousUi(parent); + // } + // + // } } -- 2.30.2