Introduce workbench login
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 12 Oct 2015 15:40:40 +0000 (15:40 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 12 Oct 2015 15:40:40 +0000 (15:40 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@8475 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms/src/org/argeo/cms/AbstractCmsEntryPoint.java
org.argeo.cms/src/org/argeo/cms/internal/kernel/demo.ldif
org.argeo.cms/src/org/argeo/cms/util/LoginEntryPoint.java [new file with mode: 0644]
org.argeo.cms/src/org/argeo/cms/util/OpenUserMenu.java
org.argeo.cms/src/org/argeo/cms/util/SimpleErgonomics.java
org.argeo.cms/src/org/argeo/cms/util/UserMenu.java
org.argeo.cms/src/org/argeo/cms/util/UserMenuLink.java
org.argeo.cms/src/org/argeo/cms/widgets/auth/CompositeCallbackHandler.java
org.argeo.security.ui.rap/plugin.xml
org.argeo.security.ui.rap/src/org/argeo/security/ui/rap/RapLoginEntryPoint.java [new file with mode: 0644]
org.argeo.security.ui/src/org/argeo/security/ui/login/WorkbenchLogin.java [new file with mode: 0644]

index 9f3fee857636101bcdc844fa104df5fea408524a..b5078696d4eae35cac32fb40b39ee0b89e50ac50 100644 (file)
@@ -124,7 +124,7 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
                        @Override
                        public Void run() {
                                try {
-                                       createUi(parent);
+                                       initUi(parent);
                                } catch (Exception e) {
                                        throw new CmsException("Cannot create entrypoint contents",
                                                        e);
@@ -135,7 +135,7 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
        }
 
        /** Create UI */
-       protected abstract void createUi(Composite parent);
+       protected abstract void initUi(Composite parent);
 
        /** Recreate UI after navigation or auth change */
        protected abstract void refresh();
index 5c49cbadc729d1b15f514ae9de773a6a41d358ef..5fd33722d1c144b83b1a759d28594f3e33cbb5e8 100644 (file)
@@ -6,7 +6,7 @@ ou: node
 dn: ou=roles,ou=node
 objectClass: organizationalUnit
 objectClass: top
-ou: system
+ou: roles
 
 dn: cn=admin,ou=roles,ou=node
 objectClass: groupOfNames
@@ -30,7 +30,7 @@ member: cn=admin,ou=roles,ou=node
 dn: cn=org.argeo.cms.editor,ou=roles,ou=node
 objectClass: groupOfNames
 objectClass: top
-cn: editor
+cn: org.argeo.cms.editor
 member: cn=admin,ou=roles,ou=node
 member: uid=demo,ou=users,dc=example,dc=com
 
diff --git a/org.argeo.cms/src/org/argeo/cms/util/LoginEntryPoint.java b/org.argeo.cms/src/org/argeo/cms/util/LoginEntryPoint.java
new file mode 100644 (file)
index 0000000..f3597de
--- /dev/null
@@ -0,0 +1,33 @@
+package org.argeo.cms.util;
+
+import javax.security.auth.Subject;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+
+import org.argeo.cms.CmsException;
+import org.argeo.cms.auth.AuthConstants;
+import org.argeo.cms.widgets.auth.CompositeCallbackHandler;
+import org.eclipse.rap.rwt.application.AbstractEntryPoint;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+
+public class LoginEntryPoint extends AbstractEntryPoint {
+       @Override
+       public void createContents(Composite parent) {
+               parent.setLayout(new GridLayout());
+               // CompositeCallbackHandler cch = new CompositeCallbackHandler(parent,
+               // SWT.NONE);
+               UserMenu userMenu = new UserMenu(parent, false);
+
+//             Subject subject = new Subject();
+//             try {
+//                     LoginContext lc = new LoginContext(
+//                                     AuthConstants.LOGIN_CONTEXT_USER, subject, userMenu);
+//                     lc.login();
+//             } catch (LoginException e1) {
+//                     throw new CmsException("Cannot logint", e1);
+//             }
+       }
+
+}
index bb2bb3990f6937f946514505058d0edf686e53e2..61ecebd196f8a61b13491e6ebd8d80898338217c 100644 (file)
@@ -11,7 +11,7 @@ public class OpenUserMenu extends MouseAdapter {
        @Override
        public void mouseDown(MouseEvent e) {
                if (e.button == 1) {
-                       new UserMenu((Control) e.getSource());
+                       new UserMenu((Control) e.getSource(), true);
                }
        }
 }
\ No newline at end of file
index 40e5fecf8238a246a666be4bc88905dce15b438c..d89d54d7c43d7bda782b3f884297776727c82f51 100644 (file)
@@ -42,7 +42,7 @@ public class SimpleErgonomics extends AbstractCmsEntryPoint {
        }
 
        @Override
-       protected void createUi(Composite parent) {
+       protected void initUi(Composite parent) {
                parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
                parent.setLayout(CmsUtils.noSpaceGridLayout());
 
index 801a2611bf9b861bebfdc7021bbe738acfffd7f9..72289cb3e544783d1c6e3f490f15953e4db7e57b 100644 (file)
@@ -33,51 +33,80 @@ import org.eclipse.swt.events.ShellAdapter;
 import org.eclipse.swt.events.ShellEvent;
 import org.eclipse.swt.events.TraverseEvent;
 import org.eclipse.swt.events.TraverseListener;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 
 /** The site-related user menu */
-public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
-       private static final long serialVersionUID = -5788157651532106301L;
-       private Text username, password;
+public class UserMenu implements CmsStyles, CallbackHandler {
+       private final CmsView cmsView;
+       private final Shell shell;
 
-       public UserMenu(Control source) {
-               super(source.getDisplay(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
-               setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
+       private Text username, password;
 
-               String username = CurrentUser.getUsername(CmsUtils.getCmsView().getSubject());
-               if (username.equalsIgnoreCase(AuthConstants.ROLE_ANONYMOUS)) {
-                       username = null;
-                       anonymousUi();
+       public UserMenu(Control source, boolean autoclose) {
+               shell = new Shell(source.getDisplay(), SWT.NO_TRIM | SWT.BORDER
+                               | SWT.ON_TOP);
+               shell.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
+               // cmsView = (CmsView) shell.getDisplay().getData(CmsView.KEY);
+               cmsView = CmsUtils.getCmsView();
+
+               if (cmsView != null) {
+                       String username = CurrentUser.getUsername(CmsUtils.getCmsView()
+                                       .getSubject());
+                       if (username == null
+                                       || username.equalsIgnoreCase(AuthConstants.ROLE_ANONYMOUS)) {
+                               username = null;
+                               anonymousUi(shell);
+                       } else {
+                               userUi(shell);
+                       }
                } else {
-                       userUi();
+                       anonymousUi(shell);
                }
 
-               pack();
-               layout();
-               setLocation(source.toDisplay(source.getSize().x - getSize().x,
-                               source.getSize().y));
+               shell.pack();
+               shell.layout();
+               if (autoclose)// popup
+                       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);
 
-               addShellListener(new ShellAdapter() {
-                       private static final long serialVersionUID = 5178980294808435833L;
+               }
+               if (autoclose)
+                       shell.addShellListener(new ShellAdapter() {
+                               private static final long serialVersionUID = 5178980294808435833L;
+
+                               @Override
+                               public void shellDeactivated(ShellEvent e) {
+                                       closeShell();
+                               }
+                       });
+               shell.open();
 
-                       @Override
-                       public void shellDeactivated(ShellEvent e) {
-                               close();
-                               dispose();
-                       }
-               });
-               open();
        }
 
-       protected void userUi() {
-               setLayout(CmsUtils.noSpaceGridLayout());
-               Composite c = new Composite(this, SWT.NONE);
+       protected void closeShell() {
+               shell.close();
+               shell.dispose();
+       }
+
+       protected void userUi(Composite parent) {
+               parent.setLayout(CmsUtils.noSpaceGridLayout());
+               Composite c = new Composite(parent, SWT.NONE);
                c.setLayout(new GridLayout());
                c.setLayoutData(CmsUtils.fillAll());
 
@@ -104,16 +133,16 @@ public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
 
        }
 
-       protected void anonymousUi() {
-               setLayout(CmsUtils.noSpaceGridLayout());
+       protected void anonymousUi(Composite parent) {
+               parent.setLayout(CmsUtils.noSpaceGridLayout());
 
                // We need a composite for the traversal
-               Composite c = new Composite(this, SWT.NONE);
+               Composite c = new Composite(parent, SWT.NONE);
                c.setLayout(new GridLayout());
                c.setLayoutData(CmsUtils.fillAll());
 
                Integer textWidth = 120;
-               setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
+               parent.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
 
                // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
                username = new Text(c, SWT.BORDER);
@@ -142,14 +171,13 @@ public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
                c.addTraverseListener(tl);
                username.addTraverseListener(tl);
                password.addTraverseListener(tl);
-               setTabList(new Control[] { c });
+               parent.setTabList(new Control[] { c });
                c.setTabList(new Control[] { username, password });
                c.setFocus();
        }
 
        protected void login() {
-               CmsView cmsSession = (CmsView) getDisplay().getData(CmsView.KEY);
-               Subject subject = cmsSession.getSubject();
+               Subject subject = cmsView.getSubject();
                try {
                        //
                        // LOGIN
@@ -178,14 +206,12 @@ public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
                        }
                        throw new CmsException("Cannot authenticate", e1);
                }
-               close();
-               dispose();
-               cmsSession.authChange();
+               closeShell();
+               cmsView.authChange();
        }
 
        protected void logout() {
-               final CmsView cmsSession = (CmsView) getDisplay().getData(CmsView.KEY);
-               Subject subject = cmsSession.getSubject();
+               Subject subject = cmsView.getSubject();
                try {
                        //
                        // LOGOUT
@@ -199,10 +225,9 @@ public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
                } catch (LoginException e1) {
                        throw new CmsException("Cannot authenticate anonymous", e1);
                }
-               close();
-               dispose();
-               cmsSession.navigateTo("~");
-               cmsSession.authChange();
+               closeShell();
+               cmsView.navigateTo("~");
+               cmsView.authChange();
        }
 
        @Override
@@ -212,4 +237,8 @@ public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
                ((PasswordCallback) callbacks[1]).setPassword(password.getTextChars());
        }
 
+       public Shell getShell() {
+               return shell;
+       }
+
 }
index 704f90980af02273efb39f43d57af113296cff18..afab928aa363cdc04d85d37876e4c8633ecf90c9 100644 (file)
@@ -38,7 +38,7 @@ public class UserMenuLink extends MenuLink {
        }
 
        protected UserMenu createUserMenu(Control source) {
-               return new UserMenu(source.getParent());
+               return new UserMenu(source.getParent(), true);
        }
 
        private class UserMenuLinkController implements MouseListener,
@@ -61,7 +61,7 @@ public class UserMenuLink extends MenuLink {
                                        // avoid to reopen the menu, if one has clicked gain
                                        if (durationSinceLastDispose > 200) {
                                                userMenu = createUserMenu(source);
-                                               userMenu.addDisposeListener(this);
+                                               userMenu.getShell().addDisposeListener(this);
                                        }
                                }
                        }
index 30f8308e30f395d0f0a4d5a8e1df0652496e8484..a87a1458cd4881d0f9478f76db932e714b40b091 100644 (file)
@@ -82,15 +82,15 @@ public class CompositeCallbackHandler extends Composite implements
                if (!wasUsedAlready)
                        wasUsedAlready = true;
 
-               while (!isSubmitted() && !isCanceled()) {
-                       try {
-                               wait(1000l);
-                       } catch (InterruptedException e) {
-                               // silent
-                       }
-               }
-
-               cleanCallbacksAfterCancel(callbacks);
+//             while (!isSubmitted() && !isCanceled()) {
+//                     try {
+//                             wait(1000l);
+//                     } catch (InterruptedException e) {
+//                             // silent
+//                     }
+//             }
+
+//             cleanCallbacksAfterCancel(callbacks);
        }
 
        public void checkCallbackSupported(Callback callback)
index 09ca3582620730ea8978e6edee67b15d59b778e6..d383dbf39dcc3ad7ebc278e9984a5e1829886685 100644 (file)
             path="/public"
             brandingId="org.argeo.security.ui.rap.defaultBranding">
       </entrypoint>
+      <entrypoint
+            brandingId="org.argeo.security.ui.rap.defaultBranding"
+            class="org.argeo.security.ui.rap.RapLoginEntryPoint"
+            id="org.argeo.security.ui.rap.secureEntryPoint"
+            path="/login">
+      </entrypoint>
    </extension>
 
        <!-- COMMANDS --> 
             body="branding/empty.html">
        </branding>
        </extension>
-</plugin>
\ No newline at end of file
+</plugin>
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
new file mode 100644 (file)
index 0000000..3fac3eb
--- /dev/null
@@ -0,0 +1,20 @@
+package org.argeo.security.ui.rap;
+
+import org.argeo.security.ui.login.WorkbenchLogin;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PlatformUI;
+
+public class RapLoginEntryPoint extends WorkbenchLogin {
+
+       @Override
+       protected int createAndRunWorkbench(Display display, String username) {
+               RapWorkbenchAdvisor workbenchAdvisor = createRapWorkbenchAdvisor(username);
+               return PlatformUI.createAndRunWorkbench(display, workbenchAdvisor);
+       }
+
+       /** Override to provide an application specific workbench advisor */
+       protected RapWorkbenchAdvisor createRapWorkbenchAdvisor(String username) {
+               return new RapWorkbenchAdvisor(username);
+       }
+
+}
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
new file mode 100644 (file)
index 0000000..6a4e879
--- /dev/null
@@ -0,0 +1,83 @@
+package org.argeo.security.ui.login;
+
+import java.security.PrivilegedAction;
+
+import javax.security.auth.Subject;
+
+import org.argeo.cms.CmsImageManager;
+import org.argeo.cms.CmsView;
+import org.argeo.cms.auth.CurrentUser;
+import org.argeo.cms.util.UserMenu;
+import org.eclipse.rap.rwt.application.EntryPoint;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
+
+public abstract class WorkbenchLogin implements EntryPoint, CmsView {
+       private final Subject subject = new Subject();
+
+       @Override
+       public int createUI() {
+               final Display display = PlatformUI.createDisplay();
+               display.setData(CmsView.KEY, this);
+               Shell shell = new Shell(display, SWT.NO_TRIM);
+               shell.setMaximized(true);
+               UserMenu userMenu = new UserMenu(shell, false);
+               shell.open();
+               while (!userMenu.getShell().isDisposed()) {
+                       if (!display.readAndDispatch()) {
+                               display.sleep();
+                       }
+               }
+               //
+               // RUN THE WORKBENCH
+               //
+               Integer returnCode = null;
+               try {
+                       returnCode = Subject.doAs(subject, new PrivilegedAction<Integer>() {
+                               public Integer run() {
+                                       int result = createAndRunWorkbench(display,
+                                                       CurrentUser.getUsername(subject));
+                                       return new Integer(result);
+                               }
+                       });
+               } finally {
+                       display.dispose();
+               }
+               return returnCode;
+               // display.dispose();
+               // return 0;
+       }
+
+       protected abstract int createAndRunWorkbench(Display display,
+                       String username);
+
+       @Override
+       public void navigateTo(String state) {
+               // TODO Auto-generated method stub
+
+       }
+
+       @Override
+       public void authChange() {
+       }
+
+       @Override
+       public final Subject getSubject() {
+               return subject;
+       }
+
+       @Override
+       public void exception(Throwable e) {
+               // TODO Auto-generated method stub
+
+       }
+
+       @Override
+       public CmsImageManager getImageManager() {
+               // TODO Auto-generated method stub
+               return null;
+       }
+
+}