auto login feature for developers
authorAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Tue, 2 May 2017 13:11:37 +0000 (15:11 +0200)
committerAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Tue, 2 May 2017 13:11:37 +0000 (15:11 +0200)
ide/eclipse/Jetty Launcher/cdm-vaadin - run.launch
src/main/java/eu/etaxonomy/cdm/vaadin/view/LoginPresenter.java

index aab476451374949f8413595b2a0432b38156ae27..bb252acd9e266c7127dc9e12a6342c5a1a16b3af 100644 (file)
@@ -54,5 +54,5 @@
 <stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="net.sourceforge.eclipsejetty.launcher.JettyLaunchClassPathProvider"/>
 <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="net.sourceforge.eclipsejetty.starter.jetty9.Jetty9LauncherMain"/>
 <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="cdm-vaadin"/>
-<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dlog4j.configDebug=false&#10;-Dlog4j.configuration=file:///${system_property:user.home}/.cdmLibrary/log4j.properties&#10;&#10;-Dcdm.forceSchemaUpdate=true"/>
+<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dlog4j.configDebug=false&#10;-Dlog4j.configuration=file:///${system_property:user.home}/.cdmLibrary/log4j.properties&#10;&#10;-Dcdm.forceSchemaUpdate=true&#10;&#10;-Dcdm-vaadin.login.usr=admin&#10;-Dcdm-vaadin.login.pwd=00000"/>
 </launchConfiguration>
index df4329eed8c3d6d651226984965343634931b435..21c773c5ace24ff949867f7ff4cafb96f0e8d4c2 100644 (file)
@@ -8,6 +8,8 @@
 */
 package eu.etaxonomy.cdm.vaadin.view;
 
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.context.event.EventListener;
@@ -30,6 +32,13 @@ import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
  * The {@link LoginView is used as replacement view in the scope of other views.
  * Therefore the LoginPresenter must be in <b>UIScope</b> so that the LoginPresenter
  * is available to all Views.
+ * <p>
+ * The LoginPresenter offers a <b>auto login feature for developers</b>. Tio activate the auto login
+ * you need to provide the <code>user name</code> and <code>password</code> using the environment variables
+ * <code>cdm-vaadin.login.usr</code> and <code>cdm-vaadin.login.pwd</code>, e.g.:
+ * <pre>
+ * -Dcdm-vaadin.login.usr=admin -Dcdm-vaadin.login.pwd=00000
+ * </pre>
  *
  * @author a.kohlbecker
  * @since Apr 25, 2017
@@ -41,6 +50,12 @@ public class LoginPresenter extends AbstractPresenter<LoginView> {
 
     private static final long serialVersionUID = 4020699735656994791L;
 
+    private static final Logger log = Logger.getLogger(LoginPresenter.class);
+
+    private final static String PROPNAME_USER = "cdm-vaadin.login.usr";
+
+    private final static String PROPNAME_PASSWORD = "cdm-vaadin.login.pwd";
+
     @Autowired
     protected ApplicationEventPublisher eventBus;
 
@@ -60,8 +75,10 @@ public class LoginPresenter extends AbstractPresenter<LoginView> {
         Authentication authentication = authenticationManager.authenticate(token);
 
         if(authentication != null && authentication.isAuthenticated()) {
+            log.debug("user '" + userName + "' autheticated");
             currentSecurityContext().setAuthentication(authentication);
             if(NavigationManager.class.isAssignableFrom(getNavigationManager().getClass())){
+                log.debug("reloading current view");
                 getNavigationManager().reloadCurrentView();
                 eventBus.publishEvent(new AuthenticationSuccessEvent(userName));
             }
@@ -70,6 +87,20 @@ public class LoginPresenter extends AbstractPresenter<LoginView> {
     }
 
 
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onViewEnter() {
+        super.onViewEnter();
+        // attempt to auto login
+        if(StringUtils.isNotEmpty(System.getProperty(PROPNAME_USER)) && StringUtils.isNotEmpty(System.getProperty(PROPNAME_PASSWORD))){
+            log.warn("Performing autologin with user " + System.getProperty(PROPNAME_USER));
+            authenticate(System.getProperty(PROPNAME_USER), System.getProperty(PROPNAME_PASSWORD));
+        }
+    }
+
     @EventListener
     protected void onLoginEvent(AuthenticationAttemptEvent e){
         authenticate(e.getUserName(), getView().getLoginDialog().getPassword().getValue());